You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 lines
1.3 KiB

import functions from '@/utils/functions.js'
import {host} from "@/config/host";
const service = (params,showToast = true) => {
return new Promise((relove, reject) => {
let token=uni.getStorageSync('token')
let user=uni.getStorageSync('user')
// console.log(params.url)
if (!/^http/.test(params.url)){
// const rExp = new RegExp("(?<!:)\\/{2,}", 'g');
params.url = 'https://'+(host+ '/' +params.url).replace('https://', '').replace('//', '/')
}
// console.log(params.url)
uni.request({
url: params.url,
data: params.data,
method: params.method,
dataType: "json",
header: {
"content-type": "application/json;charset=utf-8",
"Authorization":"Bearer "+ token
}
}).then(res => {
if (res.statusCode === 200) {
let data = res.data;
switch (data.code) {
case 1001:
uni.setStorageSync('token',data.data);
service(params);
break;
case 1002:
// todo 让用户重新登录
reject(data);
break;
case 202:
if(showToast){
functions.error(data.msg);
}
reject(data);
break;
default:
relove(data);
}
} else {
if(showToast){
functions.error('连接服务器失败')
}
reject();
}
})
})
}
export default service;