let BASE_URL = "";
if (process.env.NODE_ENV === 'development') {
// 开发环境 示例
BASE_URL = "http://10.13.70.240:9091";
} else if (process.env.NODE_ENV === 'production') {
// 生产环境 示例
BASE_URL = "https://twin-ui.com/demo/";
}
// 封装网络请求
const request = async (url, data = {}, method,type, loading = true) => {
var token = uni.getStorageSync('token');
let header = {
"Content-Type": "application/json;charset=UTF-8",
"Accept": "application/json",
}
if(type == '1'){
header = {
"Content-Type": "application/json;charset=UTF-8"
}
}
else if (type == '2'){
// 文件上传时的请求头格式
header = {
"Content-Type": "multipart/form-data;"
}
}else {
// 国际化中英文的请求头
// if(uni.getStorageSync('language')) {
// let yy = uni.getStorageSync('language')
// header.language = yy
// console.log(header);
// }
}
if (token) {
header.accessToken = token;
}
return new Promise((reslove, reject) => {
uni.showLoading({
// title: _that.$t('index.logging-in'), //正在登录
title: "正在加载", //正在登录
mask: true
});
uni.request({
url: BASE_URL + url,
method: method || 'GET',
header: header,
// timeout: 1000000000,
data: data || {},
success: (res) => {
uni.hideLoading();
if (res.statusCode == 200) {
reslove(res.data)
} else {
// toast('网络连接失败,请稍后重试')
reject(res)
}
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: '请求失败',
icon: 'none',
duration: 2000,
})
reject(err)
}
})
})
}
export default request
使用:
// 引入请求
import request from '@/config/request.js'
//测试用例
export const logins = data => request('/custom/access/h5/wxLogin', data, 'post')