uniapp配置request请求,以及跨域

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': ''
				// }
			},
			

		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值