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.
49 lines
1.0 KiB
49 lines
1.0 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') |
|
if (!/^http/.test(params.url)){ |
|
params.url = `${host}/${params.url}`.replace(/(?<!:)\/{2,}/, '/') |
|
} |
|
|
|
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: |
|
functions.error(data.msg); |
|
reject(data); |
|
break; |
|
default: |
|
relove(data); |
|
} |
|
} else { |
|
functions.error('连接服务器失败') |
|
} |
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
export default service;
|
|
|