2022-03-14 10:28:35 +00:00
|
|
|
|
// const ENV = process.env.NODE_ENV;
|
|
|
|
|
const ENV = process.env;
|
|
|
|
|
console.log(ENV,2);
|
2022-04-02 09:56:53 +00:00
|
|
|
|
let hostapi = '';
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
let hInfo = wx.getAccountInfoSync();
|
|
|
|
|
// console.log(hInfo.envVersion);//develop:开发版 trial:体验版 release:正式版
|
|
|
|
|
if(hInfo.miniProgram.envVersion == "develop" || hInfo.miniProgram.envVersion == "trial"){
|
|
|
|
|
// (开发版,体验版)域名
|
|
|
|
|
hostapi = 'https://hengmei.scdxtc.cn/api/';
|
2022-04-13 07:00:35 +00:00
|
|
|
|
// hostapi = 'https://hm.hmzfyy.cn/api/';
|
2022-04-02 09:56:53 +00:00
|
|
|
|
} else {
|
|
|
|
|
// 正式版-域名
|
|
|
|
|
hostapi = 'https://hm.hmzfyy.cn/api/';
|
|
|
|
|
}
|
|
|
|
|
// #endif
|
2022-03-14 10:28:35 +00:00
|
|
|
|
// 清理所有缓存并前往授权页
|
2021-08-19 06:40:59 +00:00
|
|
|
|
const goLogin = () => {
|
2022-04-02 09:56:53 +00:00
|
|
|
|
// uni.clearStorageSync();
|
|
|
|
|
// uni.showToast({
|
|
|
|
|
// title:'信息错误请重新登录',
|
|
|
|
|
// icon:'none'
|
|
|
|
|
// })
|
|
|
|
|
// setTimeout(()=>{
|
|
|
|
|
// uni.reLaunch({
|
|
|
|
|
// url: '/pages/login/login'
|
|
|
|
|
// })
|
|
|
|
|
// },1000)
|
2021-08-19 06:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
// 请求错误处理
|
2022-04-29 11:14:21 +00:00
|
|
|
|
let errorFlag = true;
|
2022-03-14 10:28:35 +00:00
|
|
|
|
const checkError = (e) => {
|
2022-04-29 11:14:21 +00:00
|
|
|
|
console.log(e,'接口错误');
|
2021-08-19 06:40:59 +00:00
|
|
|
|
if (e.data) {
|
|
|
|
|
if (e.data.code) {
|
|
|
|
|
switch (Number(e.data.code)) {
|
2022-03-14 10:28:35 +00:00
|
|
|
|
case 4003:
|
|
|
|
|
// 参数错误
|
|
|
|
|
console.log('4003参数错误');
|
2021-08-19 06:40:59 +00:00
|
|
|
|
break;
|
2022-03-14 10:28:35 +00:00
|
|
|
|
case 4004:
|
|
|
|
|
// 记录不存在
|
|
|
|
|
console.log('4004记录不存在');
|
|
|
|
|
break;
|
|
|
|
|
case 5001:
|
|
|
|
|
// xxx错误
|
|
|
|
|
console.log('5001xxx错误');
|
|
|
|
|
break;
|
|
|
|
|
case 5050:
|
|
|
|
|
// 服务器错误,请稍后重试
|
|
|
|
|
console.log('5050服务器错误,请稍后重试');
|
|
|
|
|
break;
|
|
|
|
|
case 5051:
|
|
|
|
|
// 未知错误
|
|
|
|
|
console.log('5051未知错误');
|
|
|
|
|
break;
|
|
|
|
|
case 6001:
|
|
|
|
|
// token验证失败或已失效
|
|
|
|
|
console.log('6001token验证失败或已失效');
|
|
|
|
|
goLogin();
|
|
|
|
|
break;
|
2021-08-19 06:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-29 11:14:21 +00:00
|
|
|
|
} else {
|
|
|
|
|
if(e.errMsg=="request:fail 对应的服务器证书无效。" || e.errno==600001) {
|
|
|
|
|
if(errorFlag) {
|
|
|
|
|
errorFlag = false;
|
|
|
|
|
uni.showModal({
|
|
|
|
|
title:'服务器证书无效',
|
|
|
|
|
confirmText:'确定',
|
|
|
|
|
showCancel:false,
|
|
|
|
|
success:(e)=> {
|
|
|
|
|
if(e.confirm) {
|
|
|
|
|
// 退出小程序
|
|
|
|
|
wx.exitMiniProgram();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-19 06:40:59 +00:00
|
|
|
|
}
|
2022-04-14 10:04:31 +00:00
|
|
|
|
let flag = true;
|
2021-08-19 06:40:59 +00:00
|
|
|
|
// 封装请求
|
2022-04-02 09:56:53 +00:00
|
|
|
|
const request = (method, url, options, ifLogin) => {
|
2021-08-19 06:40:59 +00:00
|
|
|
|
let methods = '';
|
|
|
|
|
let headers = {};
|
|
|
|
|
switch (method) {
|
|
|
|
|
case 'get':
|
|
|
|
|
methods = 'GET'
|
|
|
|
|
headers = {
|
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'post':
|
|
|
|
|
methods = 'POST'
|
|
|
|
|
headers = {
|
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'postForm':
|
|
|
|
|
methods = 'POST'
|
|
|
|
|
headers = {
|
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
|
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-04-08 09:54:56 +00:00
|
|
|
|
// 不需要登录
|
|
|
|
|
if(flag) {
|
2022-04-02 09:56:53 +00:00
|
|
|
|
uni.request({
|
|
|
|
|
url: `${hostapi}${url}`,
|
|
|
|
|
method: methods,
|
|
|
|
|
data: options,
|
|
|
|
|
header: headers,
|
|
|
|
|
success: res => {
|
2022-04-08 09:54:56 +00:00
|
|
|
|
console.log(`${url}接口返的结果===>`,res);
|
2022-04-02 09:56:53 +00:00
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
|
if (res.data.code == 0) {
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
} else {
|
2022-04-13 07:00:35 +00:00
|
|
|
|
if(res.data.code==6001) {
|
|
|
|
|
flag = false;
|
|
|
|
|
uni.login({
|
|
|
|
|
provider: 'weixin',
|
|
|
|
|
success: (res)=> {
|
|
|
|
|
var params = {code:res.code}
|
|
|
|
|
uni.request({
|
|
|
|
|
url: `${uni.getStorageSync('hostapi')}user/login`,
|
|
|
|
|
method: 'post',
|
|
|
|
|
data: params,
|
|
|
|
|
success: res => {
|
|
|
|
|
if(res.data.data.token!=''){
|
2022-04-14 10:04:31 +00:00
|
|
|
|
flag = true;
|
2022-04-13 07:00:35 +00:00
|
|
|
|
uni.setStorageSync('token',res.data.data.token); // 缓存token
|
|
|
|
|
uni.setStorageSync('openid',res.data.data.openid)//缓存openid
|
|
|
|
|
uni.setStorageSync('expire',res.data.data.expire); // 缓存失效时间(时间戳格式)
|
|
|
|
|
uni.setStorageSync('phone_active',res.data.data.phone_active); // 是否绑定手机号
|
|
|
|
|
uni.setStorageSync('is_active',res.data.data.is_active)//是否第一次授权
|
|
|
|
|
uni.reLaunch({ // 重新进入当前页面
|
|
|
|
|
url:uni.getStorageSync('paramsUrl')
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-04-08 09:54:56 +00:00
|
|
|
|
}
|
2022-04-13 07:00:35 +00:00
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
checkError(res)
|
|
|
|
|
}
|
2022-04-08 09:54:56 +00:00
|
|
|
|
// uni.showToast({
|
|
|
|
|
// title:res.data.msg,
|
|
|
|
|
// icon:'none'
|
|
|
|
|
// })
|
2022-04-13 07:00:35 +00:00
|
|
|
|
|
2022-04-02 09:56:53 +00:00
|
|
|
|
}
|
|
|
|
|
} else if(res.statusCode==500) {
|
2022-04-13 07:00:35 +00:00
|
|
|
|
flag = false;
|
|
|
|
|
uni.login({
|
|
|
|
|
provider: 'weixin',
|
|
|
|
|
success: (res)=> {
|
|
|
|
|
var params = {code:res.code}
|
|
|
|
|
uni.request({
|
|
|
|
|
url: `${uni.getStorageSync('hostapi')}user/login`,
|
|
|
|
|
method: 'post',
|
|
|
|
|
data: params,
|
|
|
|
|
success: res => {
|
|
|
|
|
if(res.data.data.token!=''){
|
2022-04-14 10:04:31 +00:00
|
|
|
|
flag = true;
|
2022-04-13 07:00:35 +00:00
|
|
|
|
uni.setStorageSync('token',res.data.data.token); // 缓存token
|
|
|
|
|
uni.setStorageSync('openid',res.data.data.openid)//缓存openid
|
|
|
|
|
uni.setStorageSync('expire',res.data.data.expire); // 缓存失效时间(时间戳格式)
|
|
|
|
|
uni.setStorageSync('phone_active',res.data.data.phone_active); // 是否绑定手机号
|
|
|
|
|
uni.setStorageSync('is_active',res.data.data.is_active)//是否第一次授权
|
|
|
|
|
uni.reLaunch({ // 重新进入当前页面
|
|
|
|
|
url:uni.getStorageSync('paramsUrl')
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-08-19 06:40:59 +00:00
|
|
|
|
} else {
|
2022-03-14 10:28:35 +00:00
|
|
|
|
checkError(res)
|
2021-08-19 06:40:59 +00:00
|
|
|
|
}
|
2022-04-02 09:56:53 +00:00
|
|
|
|
},
|
|
|
|
|
fail: e => {
|
|
|
|
|
checkError(e)
|
|
|
|
|
},
|
|
|
|
|
complete: rest => {
|
|
|
|
|
// console.log(rest);
|
2021-08-19 06:40:59 +00:00
|
|
|
|
}
|
2022-04-02 09:56:53 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2021-08-19 06:40:59 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上传文件 封装请求
|
|
|
|
|
const uploadFile = (url, options) => {
|
|
|
|
|
let tempData = options || {}
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uni.uploadFile({
|
2022-04-02 09:56:53 +00:00
|
|
|
|
url: `${hostapi}${url}`,
|
2021-08-19 06:40:59 +00:00
|
|
|
|
filePath: tempData.file,
|
|
|
|
|
name: 'image',
|
|
|
|
|
fileType:'image',
|
|
|
|
|
formData: tempData,
|
|
|
|
|
header: {
|
|
|
|
|
'Content-Type': 'multipart/form-data;charset=UTF-8',
|
|
|
|
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|
|
|
|
},
|
|
|
|
|
success: res => {
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
|
let temp = JSON.parse(res.data)
|
|
|
|
|
if (temp.code == 0) {
|
|
|
|
|
resolve(temp)
|
|
|
|
|
} else {
|
|
|
|
|
reject(temp)
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: temp.msg || '接口错误(' + temp.code + ')',
|
|
|
|
|
icon: 'none'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: `未知错误(${res.statusCode})`,
|
|
|
|
|
icon: 'none'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail(e) {
|
|
|
|
|
// uni.showToast({
|
|
|
|
|
// title: '接口请求超时',
|
|
|
|
|
// icon: 'none'
|
|
|
|
|
// })
|
|
|
|
|
// reject(e.data)
|
|
|
|
|
},
|
|
|
|
|
complete: () => {
|
|
|
|
|
uni.hideLoading()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
2022-04-02 09:56:53 +00:00
|
|
|
|
get: (url, options, ifLogin=true) => {
|
|
|
|
|
return request('get', url, options, ifLogin)
|
2021-08-19 06:40:59 +00:00
|
|
|
|
},
|
|
|
|
|
// JOSN格式
|
2022-04-02 09:56:53 +00:00
|
|
|
|
post: (url, options, ifLogin=true) => {
|
|
|
|
|
return request('post', url, options, ifLogin)
|
2021-08-19 06:40:59 +00:00
|
|
|
|
},
|
|
|
|
|
// form-data格式
|
2022-04-02 09:56:53 +00:00
|
|
|
|
postForm: (url, options, ifLogin=true) => {
|
|
|
|
|
return request('postForm', url, options, ifLogin)
|
2021-08-19 06:40:59 +00:00
|
|
|
|
},
|
|
|
|
|
// 上传
|
2022-04-02 09:56:53 +00:00
|
|
|
|
upload: (url, options, ifLogin=true) => {
|
|
|
|
|
return uploadFile(url, options, ifLogin)
|
2021-08-19 06:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|