cultivationApp/utils/syhttp.js

83 lines
2.0 KiB
JavaScript

import {
config
} from '@/config/app';
function baseRequest(url, method, data, {
noAuth = false,
noVerify = false,
onReLogin = false
}) {
let Url = config.HTTP_REQUEST_URL,
// config.HEADER.token = token
header = config.HEADER;
return new Promise((reslove, reject) => {
uni.request({
url: Url + 'api' + url,
method: method || 'GET',
header: {
...header
},
data: method != 'GET' ? data || {} : {},
params: method == 'GET' ? data : {},
success: (res) => {
// console.log(res.data.show, "全局")
if (res.data.show) {
uni.showToast({
title: res.data.msg || '操作成功',
icon: 'success',
})
}
if (noVerify)
reslove(res.data);
else if (res.data.code == -1) {
if (onReLogin) {
// store.commit('LOGOUT');
return reject();
}
} else if (res.data.code == 0) {
if (res.data.msg != '无登录信息') {
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none',
})
}
reject(res.data);
} 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) => {
console.log(message, '错误')
// 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;