去掉协会 WebView 和魔方账密页,微信登录与咨询接口共用同一 token 和拦截器。 Co-authored-by: Cursor <cursoragent@cursor.com>
201 lines
5.3 KiB
JavaScript
201 lines
5.3 KiB
JavaScript
import {
|
||
sysConsts
|
||
} from '../common/sysConsts.js';
|
||
import request from "./request";
|
||
import { YXG_API_BASE } from './yxgConfig.js'
|
||
|
||
const DEVICE_KEY = 'yxg_device_key'
|
||
const baseUrl = YXG_API_BASE
|
||
|
||
|
||
//可以new多个request来支持多个域名请求
|
||
let $http = new request({
|
||
//接口请求地址
|
||
baseUrl: baseUrl,
|
||
//服务器本地上传文件地址
|
||
fileUrl: baseUrl,
|
||
// 服务器上传图片默认url
|
||
defaultUploadUrl: "api/common/v1/upload_image",
|
||
//设置请求头(如果使用报错跨域问题,可能是content-type请求类型和后台那边设置的不一致)
|
||
header: {
|
||
'content-type': 'application/json;charset=UTF-8'
|
||
},
|
||
// 请求超时时间(默认6000)
|
||
timeout: 20000,
|
||
// 默认配置(可不写)
|
||
config: {
|
||
// 是否自动提示错误
|
||
isPrompt: true,
|
||
// 是否显示加载动画
|
||
load: true,
|
||
// 是否使用数据工厂
|
||
isFactory: true,
|
||
// 加载动画提示文字
|
||
loadingText: '加载中',
|
||
}
|
||
});
|
||
|
||
//当前接口请求数
|
||
let requestNum = 0;
|
||
//请求开始拦截器
|
||
$http.requestStart = function(options) {
|
||
if (options.load) {
|
||
if (requestNum <= 0) {
|
||
//打开加载动画
|
||
uni.showLoading({
|
||
title: options.loadingText,
|
||
mask: true
|
||
});
|
||
}
|
||
requestNum += 1;
|
||
}
|
||
// 图片上传大小限制
|
||
if (options.method == "FILE" && options.maxSize) {
|
||
// 文件最大字节: options.maxSize 可以在调用方法的时候加入参数
|
||
const maxSize = options.maxSize;
|
||
for (let item of options.files) {
|
||
if (item.size > maxSize) {
|
||
setTimeout(() => {
|
||
uni.showToast({
|
||
title: "图片过大,请重新上传",
|
||
icon: "none"
|
||
});
|
||
}, 500);
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
//请求前加入token
|
||
let myToken = uni.getStorageSync('token') || uni.getStorageSync('yxg_token')
|
||
if (myToken) {
|
||
myToken = String(myToken).replace(/\"/g, "");
|
||
options.header['Authorization'] = 'Bearer ' + myToken;
|
||
}
|
||
const device = uni.getStorageSync(DEVICE_KEY)
|
||
if (device) {
|
||
options.header['X-Device-Key'] = device
|
||
}
|
||
|
||
return options; // return false 表示请求拦截,不会继续请求
|
||
}
|
||
//请求结束
|
||
$http.requestEnd = function(options) {
|
||
//判断当前接口是否需要加载动画
|
||
if (options.load) {
|
||
requestNum = requestNum - 1;
|
||
if (requestNum <= 0) {
|
||
uni.hideLoading();
|
||
}
|
||
}
|
||
}
|
||
//所有接口数据处理(可在接口里设置不调用此方法)
|
||
//此方法需要开发者根据各自的接口返回类型修改,以下只是模板
|
||
$http.dataFactory = async function(res) {
|
||
// console.log("接口请求数据", {
|
||
// url: res.url,
|
||
// resolve: res.response,
|
||
// header: res.header,
|
||
// data: res.data,
|
||
// method: res.method,
|
||
// });
|
||
const respHeader = res.response.header || res.response.headers || {}
|
||
const deviceOut = respHeader['X-Device-Key'] || respHeader['x-device-key']
|
||
if (deviceOut) {
|
||
uni.setStorageSync(DEVICE_KEY, deviceOut)
|
||
}
|
||
let httpData = res.response.data
|
||
if (typeof(httpData) == "string") {
|
||
try {
|
||
httpData = JSON.parse(httpData)
|
||
} catch (e) {
|
||
httpData = null
|
||
}
|
||
}
|
||
const errText = (httpData && (httpData.info || httpData.msg || httpData.message)) || ''
|
||
if (res.response.statusCode && res.response.statusCode == 200) {
|
||
/*********以下只是模板(及共参考),需要开发者根据各自的接口返回类型修改*********/
|
||
|
||
if (httpData && (httpData.success || httpData.code == 0)) {
|
||
// 返回正确的结果(then接受数据)
|
||
return Promise.resolve(httpData.data);
|
||
} else if (httpData.code == "401" || httpData.code == "1001" || httpData.code == 1100 || httpData.code == 40100 || httpData.code == 40112) {
|
||
// let content = '此时此刻需要您登录喔~';
|
||
// if (!uni.getStorageSync('loginPageAlive')) {
|
||
// await gotoLogin().then(()=>{
|
||
|
||
// })
|
||
// }
|
||
|
||
gotoLogin(res)
|
||
// 返回错误的结果(catch接受数据)
|
||
return Promise.reject({
|
||
statusCode: 0,
|
||
errMsg: "【request】" + errText
|
||
});
|
||
} else { //其他错误提示
|
||
if (res.isPrompt) {
|
||
setTimeout(() => {
|
||
uni.showToast({
|
||
title: errText || '请求失败',
|
||
icon: "none",
|
||
duration: 3000
|
||
});
|
||
}, 100)
|
||
}
|
||
// 返回错误的结果(catch接受数据)
|
||
return Promise.reject({
|
||
statusCode: 0,
|
||
errMsg: "【request】" + errText
|
||
});
|
||
}
|
||
} else if (res.response.statusCode && res.response.statusCode == 401) {
|
||
gotoLogin(res)
|
||
} else {
|
||
const text = errText || ("HTTP " + res.response.statusCode)
|
||
if (res.isPrompt) {
|
||
setTimeout(() => {
|
||
uni.showToast({
|
||
title: text,
|
||
icon: "none",
|
||
duration: 3000
|
||
});
|
||
}, 100)
|
||
}
|
||
return Promise.reject({
|
||
statusCode: 0,
|
||
errMsg: "【request】" + text
|
||
});
|
||
}
|
||
};
|
||
// 错误回调
|
||
$http.requestError = function(e) {
|
||
// e.statusCode === 0 是参数效验错误抛出的
|
||
if (e.statusCode === 0) {
|
||
throw e;
|
||
} else {
|
||
setTimeout(() => {
|
||
uni.showToast({
|
||
title: "网络错误 请检查一下网络",
|
||
icon: "none"
|
||
});
|
||
}, 100)
|
||
}
|
||
}
|
||
|
||
//token过期,退出登录
|
||
function gotoLogin(res) {
|
||
uni.removeStorageSync("token")
|
||
uni.removeStorageSync("yxg_token")
|
||
uni.removeStorageSync("userinfo")
|
||
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
|
||
const cur = pages.length ? pages[pages.length - 1] : null
|
||
const route = (cur && (cur.route || cur.__route__)) || ''
|
||
if (String(route).indexOf('login-pop') !== -1) {
|
||
return
|
||
}
|
||
uni.navigateTo({
|
||
url: "/pages/login-pop/login-pop?type=2",
|
||
animationType: "pop-in"
|
||
})
|
||
}
|
||
export default $http; |