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.
50 lines
1.1 KiB
50 lines
1.1 KiB
import functions from '@/utils/functions.js' |
|
import {host} from "@/config/host"; |
|
const service = (params) => { |
|
return new Promise((relove, reject) => { |
|
let token=uni.getStorageSync('token') |
|
let user=uni.getStorageSync('user') |
|
// try{ |
|
if (!/^http/.test(params.url)) { |
|
// const rExp = new RegExp("(?<!:)\\/{2,}", 'g'); |
|
params.url = host+ '/' +(params.url.replace('//', '/')) |
|
} |
|
// }catch (e){ |
|
// console.log(e) |
|
// } |
|
|
|
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; |
|
default: |
|
relove(data); |
|
} |
|
} else { |
|
functions.error('连接服务器失败') |
|
} |
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
export default service;
|
|
|