mall_client/zyhs_app_java/zyhs3_uniapp/common/http/index.js

242 lines
7.6 KiB
JavaScript
Raw Normal View History

2026-03-13 07:50:35 +00:00
import Request from './request.js'
import { throttle } from './throttle.js'
import { officialAccountAuthorize,setMiniOpenIdByCode } from '@/common/utils/weChatPay.js'
import { shopApiDomain, inviteUrlDomain } from '@/config/env.js';
//静态资源访问路径,有些资源不宜放太多到项目中影响微信小程序打包大小
export const staticUrl = "https://zyhs3.oss-cn-beijing.aliyuncs.com/static_resources/"
//后台服务接口请求地址如果是本地运行可以用ip+端口
// http://123.57.178.78:6001 测试
// http://api.zyihs.com 正式
export const baseUrl = shopApiDomain;
// export const baseUrl = "http://ltrq.natapp1.cc/api/"
// export const baseUrl = 'http://5b2ck3.natappfree.cc'
//即时通讯连接地址https时用wsshttp时用ws如果是本地运行可以用ip+端口
// export const imUrl = "wss://[中亿华商-im后台服务请求域名]?imToken="
export const imUrl = "ws://123.57.178.78:6000?imToken="
// export const baseUrl = "https://[redmall-boot后台服务请求域名]/api/"
// export const baseUrl = "http://123.57.178.78:6001/api/"
// export const baseUrl = "http://localhost:6001/api/"
//即时通讯连接地址https时用wsshttp时用ws如果是本地运行可以用ip+端口
// export const imUrl = "wss://[redmall-im后台服务请求域名]?imToken="
//用户推广邀请注册h5网页网站地址如果是本地运行可以用ip+端口
// export const inviteUrl = "https://[中亿华商-uniapp-register用户推广注册h5网站地址]/#/pages/public/reg?code="
// http://prod.zyihs.com 正式站
// http://zyhs3_re.jinanluke.com 测试站
export const inviteUrl = inviteUrlDomain
const config = {
baseUrl: baseUrl
}
const reqInterceptor = async (options) => {
//TODO do your request interceptor, such as url/header config, token refresh...
let token = uni.getStorageSync("token");
options.header = Object.assign({}, options.header, {
"Authorization": token
})
_requestLog(options, "成功通过")
// return false will abort the request, and then reject a blank object {}
return options
}
const resInterceptor = (response, conf = {}) => {
// console.log('2222222222222222222')
// TODO do your response
const statusCode = response.statusCode
const errCode = response.data.code || response.data.status || ''
if (errCode.toString() === '401') {
response.statusCode = 401
_responseLog(response, conf, "response 401")
return {
// 根据当前字段来判断是否reject
wakaryReqToReject: true,
msg: "请登录",
res: response
}
}
if (errCode.toString() === '500') {
response.statusCode = 500
_responseLog(response, conf, "response 500")
return {
// 根据当前字段来判断是否reject
wakaryReqToReject: true,
msg: response.data.msg,
res: response
}
}
// response interceptor
if (statusCode >= 200 && statusCode < 300) { //成功
if (response.data.code !== 200) { // 233抛异常
response.statusCode = response.data.code
_responseLog(response, conf, "response 222")
return {
// 根据当前字段来判断是否reject
wakaryReqToReject: true,
// 下面可以配置您的其它返回信息方便您更加统一的处理reject的内容。
// 以下内容会被作为reject的返回根据您的需要处理比如返回您的具体错误信息
msg: response.data.msg,
res: response
}
}
_responseLog(response, conf, "response 200-299")
return response.data
} else if (statusCode === 500) {
_responseLog(response, conf, "response 500")
// 为了对reject的内容更加可控我们增加了一个控制字段 wakaryReqToReject
return {
// 根据当前字段来判断是否reject
wakaryReqToReject: true,
// 下面可以配置您的其它返回信息方便您更加统一的处理reject的内容。
// 以下内容会被作为reject的返回根据您的需要处理比如返回您的具体错误信息
msg: "服务器错误",
res: response
}
} else {
console.log('response服务器正在维护***************')
_responseLog(response, conf, "response 300-499")
// 为了对reject的内容更加可控我们增加了一个控制字段 wakaryReqToReject
return {
// 根据当前字段来判断是否reject
wakaryReqToReject: true,
// 下面可以配置您的其它返回信息方便您更加统一的处理reject的内容。
// 以下内容会被作为reject的返回根据您的需要处理比如返回您的具体错误信息
msg: "这里是提示信息",
res: response
}
}
}
const req = new Request(config, reqInterceptor, resInterceptor)
// request log
function _requestLog(req, describe = null) {
// if (process.env.NODE_ENV === 'development') {
// console.log("地址:" + req.url)
// if (describe) {
// console.log("描述:" + describe)
// }
// console.log("详细:" + JSON.stringify(req))
// }
//TODO into log server
}
// response log
function _responseLog(res, conf = {}, describe = null) {
let _statusCode = res.statusCode;
// alert(1)
if (process.env.NODE_ENV === 'development') {
// console.log("地址: " + conf.url)
// if (describe) {
// console.log("描述:" + describe)
// }
// console.log("结果: " + JSON.stringify(res.data))
}
//TODO into log server
console.log(res,'11')
if (_statusCode === 500) {
console.log(res)
let duration = 1500
let msg = res.data.msg
if (msg.length > 5 && msg.length <= 10) {
duration = 3000
} else if (msg.length > 10 && msg.length <=20) {
duration = 5000
} else if (msg.length > 20) {
duration = 8000
}
//关闭loading
uni.hideLoading();
setTimeout(()=> {
if(res.data.msg == "已领取,数据正在处理中!") return
uni.showToast({
title: msg,
duration: duration,
icon: "none"
})
}, 100)
} else if (_statusCode === 401 || _statusCode === 100) {
// debugger
throttle.canDoFunction({
key: "is401",
time: 1000,
success:()=>{
uni.removeStorageSync("token")
let routes = getCurrentPages()
let curRoute = routes[routes.length - 1].route
//排除的路径
let excludeRoutes = "pages/login/index,pages/login/login,pages/login/register,pages/public/reg,pages/login/forget";
if (excludeRoutes.indexOf(curRoute) < 0) { // 如果不在登录页面,账号密码登录页面、注册页面、忘记密码页面
uni.showModal({
content: '该功能需要登录后才能访问哟~',
confirmText: '马上登录',
success: function (res) {
if (res.confirm) {
uni.reLaunch({
url: "/pages/login/login"
})
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
/* uni.showToast({
title: '请登录',
icon: "none"
})
uni.reLaunch({
url: "/pages/login/index"
}) */
}
}
})
} else if(_statusCode === 81001){//微信公众号授权登入
//#ifdef H5
officialAccountAuthorize();
//#endif
//#ifndef H5
//关闭loading
uni.hideLoading();
setTimeout(()=> {
uni.showToast({
title: "该平台不支持微信公众号登入",
duration: 1500,
icon: "none"
})
}, 100)
//#endif
} else if(_statusCode === 81002){//微信小程序授权登入
//#ifdef MP-WEIXIN
setMiniOpenIdByCode();
//#endif
//#ifndef MP-WEIXIN
//关闭loading
uni.hideLoading();
setTimeout(()=> {
uni.showToast({
title: "该平台不支持微信小程序登入",
duration: 1500,
icon: "none"
})
}, 100)
//#endif
} else if(_statusCode === 404) {
//关闭loading
uni.hideLoading();
setTimeout(()=> {
uni.showToast({
title: res.data.msg,
duration: 1500,
icon: "none"
})
}, 100)
}
}
export default req