112 lines
3.0 KiB
JavaScript
112 lines
3.0 KiB
JavaScript
|
||
let baseUrl = '';
|
||
if (process.env.NODE_ENV === 'development') {
|
||
baseUrl = 'http://192.168.0.224:8080'; // 开发环境
|
||
} else {
|
||
baseUrl = 'http://192.168.1.224:8080'; // 生产环境
|
||
}
|
||
|
||
export default function request(url, data = {}, method = "GET") {
|
||
return new Promise((resolve, reject) => {
|
||
// 加载中提示(提升用户体验)
|
||
uni.showLoading({ title: '请求中...', mask: true });
|
||
|
||
uni.request({
|
||
url: baseUrl + url,
|
||
data: data,
|
||
method: method,
|
||
header: {
|
||
// "Authorization": "Bearer " + (uni.getStorageSync("token") || ''),
|
||
"clientid": "resident",
|
||
'content-type': 'application/json'
|
||
},
|
||
// header: {
|
||
// "token": uni.getStorageSync("token") || '',
|
||
// 'content-type': 'application/json'
|
||
// },
|
||
success: (res) => {
|
||
uni.hideLoading(); // 关闭加载提示
|
||
|
||
// 处理HTTP状态码(网络层面)
|
||
if (res.statusCode !== 200) {
|
||
const errMsg = `请求失败(${res.statusCode})`;
|
||
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
|
||
reject(errMsg);
|
||
return;
|
||
}
|
||
|
||
// 处理业务状态码(后端返回的data里)
|
||
const resData = res.data || {}; // 兼容后端返回空的情况
|
||
|
||
// 优先处理token过期(401)
|
||
if (resData.code === 401) {
|
||
uni.showToast({ title: '登录过期,请重新登录', icon: 'none', duration: 2000 });
|
||
uni.clearStorageSync('token');
|
||
setTimeout(() => {
|
||
uni.reLaunch({ url: '/pageSubPack/loginSub/pwd-login' });
|
||
}, 1500);
|
||
reject('token 过期');
|
||
return;
|
||
}
|
||
|
||
// 业务成功(code=200)
|
||
if (resData.code === 200) {
|
||
resolve(resData); // 只返回核心数据,简化页面调用
|
||
}
|
||
// 业务失败(code=500)
|
||
else if (resData.code === 500) {
|
||
const errMsg = resData.msg || '请求失败';
|
||
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
|
||
// resolve(null)
|
||
reject(errMsg);
|
||
}
|
||
else {
|
||
const errMsg = resData.msg || '业务异常';
|
||
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
|
||
reject(errMsg);
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
uni.hideLoading(); // 关闭加载提示
|
||
const errMsg = err.msg || '网络请求失败';
|
||
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
export const get = (url, data) => request(url, data, 'GET');
|
||
export const post = (url, data) => request(url, data, 'POST');
|
||
|
||
// 通用文件上传
|
||
export const upload = (filePath, url) => {
|
||
return new Promise((resolve, reject) => {
|
||
uni.uploadFile({
|
||
url: baseUrl + url,
|
||
filePath: filePath,
|
||
name: 'file',
|
||
header: {
|
||
"Authorization": "Bearer " + (uni.getStorageSync("token") || ''),
|
||
"clientid": "resident"
|
||
},
|
||
success: (res) => {
|
||
if (res.statusCode === 200) {
|
||
const data = JSON.parse(res.data);
|
||
if (data.code === 200) {
|
||
resolve(data);
|
||
} else {
|
||
reject(data.msg || '上传失败');
|
||
}
|
||
} else {
|
||
reject('上传失败');
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
|
||
|