feat: 小程序统一走 Go 后台,咨询与登录切到 /api/v1
去掉协会 WebView 和魔方账密页,微信登录与咨询接口共用同一 token 和拦截器。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+52
-55
@@ -2,13 +2,10 @@ import {
|
||||
sysConsts
|
||||
} from '../common/sysConsts.js';
|
||||
import request from "./request";
|
||||
// 全局配置的请求域名
|
||||
// const baseUrl = 'http://192.168.100.19:8922'; //线上正式环境888
|
||||
// const baseUrl = 'https://api-qa.scyuelai.com/'; //qa测试环境
|
||||
// const baseUrl = 'https://api-zsh-dev.scyuelai.com/' //dev开发环境
|
||||
// const baseUrl = 'https://local.scyuelai.com/'; //本地测试环境
|
||||
const baseUrl = "https://miniapp.yuxingu.com.cn" //https://miniapp.yuxingu.com.cn
|
||||
// const baseUrl = "http://8.137.99.227:8922"
|
||||
import { YXG_API_BASE } from './yxgConfig.js'
|
||||
|
||||
const DEVICE_KEY = 'yxg_device_key'
|
||||
const baseUrl = YXG_API_BASE
|
||||
|
||||
|
||||
//可以new多个request来支持多个域名请求
|
||||
@@ -69,11 +66,15 @@ $http.requestStart = function(options) {
|
||||
}
|
||||
}
|
||||
//请求前加入token
|
||||
let myToken = uni.getStorageSync('token')
|
||||
let myToken = uni.getStorageSync('token') || uni.getStorageSync('yxg_token')
|
||||
if (myToken) {
|
||||
myToken = myToken.replace(/\"/g, "");
|
||||
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 表示请求拦截,不会继续请求
|
||||
}
|
||||
@@ -97,19 +98,27 @@ $http.dataFactory = async function(res) {
|
||||
// data: res.data,
|
||||
// method: res.method,
|
||||
// });
|
||||
if (res.response.statusCode && res.response.statusCode == 200) {
|
||||
let httpData = res.response.data;
|
||||
|
||||
if (typeof(httpData) == "string") {
|
||||
httpData = JSON.parse(httpData);
|
||||
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.success || httpData.code == 0) {
|
||||
if (httpData && (httpData.success || httpData.code == 0)) {
|
||||
// 返回正确的结果(then接受数据)
|
||||
return Promise.resolve(httpData.data);
|
||||
} else if (httpData.code == "401" || httpData.code == "1001" || httpData.code == 1100) {
|
||||
} 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(()=>{
|
||||
@@ -121,13 +130,13 @@ $http.dataFactory = async function(res) {
|
||||
// 返回错误的结果(catch接受数据)
|
||||
return Promise.reject({
|
||||
statusCode: 0,
|
||||
errMsg: "【request】" + (httpData.info || httpData.msg)
|
||||
errMsg: "【request】" + errText
|
||||
});
|
||||
} else { //其他错误提示
|
||||
if (res.isPrompt) {
|
||||
setTimeout(() => {
|
||||
uni.showToast({
|
||||
title: httpData.info || httpData.msg,
|
||||
title: errText || '请求失败',
|
||||
icon: "none",
|
||||
duration: 3000
|
||||
});
|
||||
@@ -136,16 +145,25 @@ $http.dataFactory = async function(res) {
|
||||
// 返回错误的结果(catch接受数据)
|
||||
return Promise.reject({
|
||||
statusCode: 0,
|
||||
errMsg: "【request】" + (httpData.info || httpData.msg)
|
||||
errMsg: "【request】" + errText
|
||||
});
|
||||
}
|
||||
} else if (res.response.statusCode && res.response.statusCode == 401) {
|
||||
gotoLogin(res)
|
||||
} else {
|
||||
// 返回错误的结果(catch接受数据)
|
||||
const text = errText || ("HTTP " + res.response.statusCode)
|
||||
if (res.isPrompt) {
|
||||
setTimeout(() => {
|
||||
uni.showToast({
|
||||
title: text,
|
||||
icon: "none",
|
||||
duration: 3000
|
||||
});
|
||||
}, 100)
|
||||
}
|
||||
return Promise.reject({
|
||||
statusCode: res.response.statusCode,
|
||||
errMsg: "【request】数据工厂验证不通过"
|
||||
statusCode: 0,
|
||||
errMsg: "【request】" + text
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -167,38 +185,17 @@ $http.requestError = function(e) {
|
||||
//token过期,退出登录
|
||||
function gotoLogin(res) {
|
||||
uni.removeStorageSync("token")
|
||||
uni.removeStorageSync("yxg_token")
|
||||
uni.removeStorageSync("userinfo")
|
||||
console.log('res', res)
|
||||
// uni.navigateTo({
|
||||
// "url": "/pages/login-pop/login-pop?type=1",
|
||||
// "animationType": "pop-in"
|
||||
// })
|
||||
// return new Promise((resolve, rejict)=>{
|
||||
|
||||
|
||||
|
||||
|
||||
// // uni.switchTab({
|
||||
// // "url":"/pages/login-pop/login-pop",
|
||||
// // success:(res)=>{
|
||||
// // // uni.showToast({
|
||||
// // // title: "登录已失效,请重新登录",
|
||||
// // // icon: "none"
|
||||
// // // });
|
||||
// // uni.showModal({
|
||||
// // title: '提示',
|
||||
// // content: '登录已失效,是否重新登录',
|
||||
// // success: function (res) {
|
||||
// // if (res.confirm) {
|
||||
// // uni.navigateTo({
|
||||
// // "url":"/pageLogin/user-detail/user-detail"
|
||||
// // })
|
||||
// // }
|
||||
// // }
|
||||
// // });
|
||||
// // }
|
||||
// // })
|
||||
// resolve()
|
||||
// })
|
||||
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;
|
||||
Reference in New Issue
Block a user