TraceabilityAPP/utils/syhttp.js

95 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2023-10-17 16:02:55 +08:00
import {
config
} from '@/config/app';
2023-10-25 19:55:18 +08:00
2023-11-27 18:23:17 +08:00
import store from "@/store/index.js"
2023-10-17 16:02:55 +08:00
function baseRequest(url, method, data, {
noAuth = false,
noVerify = false,
onReLogin = false
}) {
let Url = config.HTTP_REQUEST_URL,
header = config.HEADER;
2023-11-30 17:51:39 +08:00
if (store.state.userInfo) {
header.TOKEN = store.state.userInfo.token
2023-11-29 18:11:21 +08:00
}
2023-11-27 18:23:17 +08:00
2023-10-17 16:02:55 +08:00
return new Promise((reslove, reject) => {
uni.request({
2023-10-25 19:55:18 +08:00
url: Url + 'api' + url,
2023-10-17 16:02:55 +08:00
method: method || 'GET',
header: {
...header
},
data: method != 'GET' ? data || {} : {},
params: method == 'GET' ? data : {},
success: (res) => {
2023-10-28 18:28:10 +08:00
// console.log(res.data.show, "全局")
if (res.data.show) {
uni.showToast({
title: res.data.msg || '操作成功',
icon: 'success',
})
}
2023-10-17 16:02:55 +08:00
if (noVerify)
reslove(res.data);
else if (res.data.code == -1) {
2023-12-07 09:34:55 +08:00
if (res.data.msg == "登录超时,请重新登录") {
// store.commit('LOGOUT');
uni.showToast({
title: res.data.msg ,
icon: 'none',
})
uni.reLaunch({
url:'/pages/Login/login'
})
}
2023-10-17 16:02:55 +08:00
} else if (res.data.code == 0) {
2023-11-29 18:11:21 +08:00
if (res.data.msg != '用户信息不存在') {
2023-10-17 16:02:55 +08:00
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none',
})
}
2023-11-30 17:51:39 +08:00
reslove(res.data);
2023-10-17 16:02:55 +08:00
} else if (res.data.code == 1) {
// store.commit("SET_REQUEST");
reslove(res.data);
} else if (res.data.code == 200) {
// store.commit("SET_REQUEST");
reslove(res.data.data);
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
reject(res.data);
} else if (res.data.code == 501) {
reject(res.data);
} else {
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none'
})
reject(res.data.msg || '请检查网络');
}
},
fail: (message) => {
2023-10-30 19:09:40 +08:00
console.log(message, '错误')
2023-10-17 16:02:55 +08:00
// uni.hideLoading()
uni.showToast({
title: '网络错误',
icon: 'none'
})
reject('请求失败');
}
})
});
}
const syhttp = {};
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
syhttp[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});
export default syhttp;