import { encrypt, decrypt1 } from '@/utlis/aes.js' //对数据的加密解密
export const request = ({ url, method, data, baseUrl = '', content = 'application/x-www-form-urlencoded' }) => {
//console.log('3333333333333333333333', global);
const provider = uni.getStorageSync('provider')
const token = uni.getStorageSync('token') || ''
let source = ''
//抖音source 1008 其他未知
if (provider == 'toutiao' || provider == 'baidu') { //环境判断
source = '1008'
} else if (provider == 'H5' || provider == 'wxh5') {
source = '1009'
}
if (provider == 'H5' || provider == 'wxh5') { //h5环境下走服务器代理
baseUrl = ''
}
// uni.showLoading({
// title: '数据加载中...',
// })
//异步封装接口,使用Promise处理异步请求
return new Promise((resolve, reject) => {
// 发送请求
uni.request({
url: baseUrl + url || '', //接收请求的API
method: method || 'GET', //接收请求的方式,如果不传默认为GET
data: data || {}, //接收请求的data,不传默认为空
header: { //设置请求头
//'access_token': uni.getStorageSync('token'), headers = 'application/x-www-form-urlencoded'
'access-token': token,
"source": source,
'content-type': content,
//'xo': xxoo({ 'ua': navigator.userAgent, 'now': new Date().getTime() })
},
}).then(data => {
let [err, res] = data;
// 对一些响应数据处理 例如token过期之类
if (res.statusCode == '202' || res.data.code == '104' || res.data.code == '103') {
uni.$Msg('登录已过期,请重新登录')
setTimeout(() => {
uni.navigateTo({
url: '/pages/login/login'
})
}, 1000)
}
// if (res.data.code == 0) {
// console.log('进入失误');
// return
// }
// uni.hideLoading()
if (res.statusCode == '200' && url.includes('/qeg-api')) {
// 对php加密数据进行解密
res.data.data = decrypt1(res.data.data)
}
resolve(res.data);
}).catch(error => {
reject(error);
})
})
}
vue.conflg.js前端配置跨域
module.exports = {
configureWebpack: {
resolve: {
alias: {
'@': '@/'
}
}
},
devServer: {
proxy: {
'/rpa_api': {
target: '', // 代理地址
changeOrigin: true,
// pathRewrite: {
// '^/rpa_api': ''
// }
},
}
}
}