基本重构完成

This commit is contained in:
lik
2026-10-07 14:55:06 +08:00
parent f655c906f5
commit f0adf77a41
23 changed files with 1284 additions and 685 deletions
+7 -4
View File
@@ -1,11 +1,14 @@
const request = require('./request.js')
// user 服务与 health 服务是两个独立进程(本地 :9010 / :9004,线上才由 nginx 同域分流),
// 所以 user 段必须用单独实例,否则本地调试时 user 接口会打到 health 端口上走不通
const requestUser = new request.Request(request.USER_BASE_URL)
const API = {
user: {
wxGetPhoneNumber: (data) => request.post('/user/wxgetphonenumber', data),
wxSignin: (data) => request.post('/user/wxsignin', data),
signout: (data) => request.post('/user/signout', data),
update: (data) => request.post('/user/update', data),
wxGetPhoneNumber: (data) => requestUser.post('/user/wxgetphonenumber', data),
wxSignin: (data) => requestUser.post('/user/wxsignin', data),
signout: (data) => requestUser.post('/user/signout', data),
update: (data) => requestUser.post('/user/update', data),
},
escort: {
+46 -5
View File
@@ -1,14 +1,50 @@
// 本地调试开关:置 true 时直连本机进程(health :9004、user :9010)
// 线上由 nginx 按路径分流(/user → 9010、/health → 9004),两个服务共用同一域名
const DEBUG = false
const BASE_URL = DEBUG ? 'http://127.0.0.1:9004' : 'https://api.huashengtec.com'
const USER_BASE_URL = DEBUG ? 'http://127.0.0.1:9010' : 'https://api.huashengtec.com'
class Request {
constructor(baseURL = 'https://api.huashengtec.com') {
//constructor(baseURL = 'http://127.0.0.1:9004') {
constructor(baseURL = BASE_URL) {
this.baseURL = baseURL
// 401 兜底回调,由 app.js 注入(静默重登)。
// request 层不直接写登录逻辑:utils/api.js → request.js 是单向依赖,
// 反过来引 api.js 会成环
this.onUnauthorized = null
}
setUnauthorizedHandler(handler) {
this.onUnauthorized = handler
}
request(options) {
return new Promise((resolve, reject) => {
const { url, method = 'GET', data = {}, header = {}, ...rest } = options
return this.send(options).then((body) => this.recoverUnauthorized(body, options))
}
// token 失效时后端返回 HTTP 200 + code 401(api/health/utils/responseUtil.js),
// 这里静默重登一次再重试:token 会被别处登录轮换,小程序长时间停在前台
// 也不会再触发 app.onShow 的重登,本机 token 因此会变陈旧
recoverUnauthorized(body, options) {
if (!body || body.code !== 401 || options._retried || !this.onUnauthorized) {
return body
}
// 重登失败不当成网络错误抛出去:回一句可执行的提示,
// 调用方现有的「res.msg 直接弹 toast」就能显示出来(「登录已失效」是死胡同)
const notLoggedIn = { code: 401, msg: '请先登录', data: null }
return Promise.resolve()
.then(() => this.onUnauthorized())
.then((ok) => (ok ? this.request({ ...options, _retried: true }) : notLoggedIn))
.catch(() => notLoggedIn)
}
send(options) {
const { url, method = 'GET', data = {}, header = {}, _retried, ...rest } = options
return new Promise((resolve, reject) => {
const app = getApp()
const token = app?.globalData?.user?.security?.token || ''
@@ -61,4 +97,9 @@ class Request {
const request = new Request()
// 默认导出 health 实例(保持既有 require('./request.js') 用法不变);
// 类与 user 侧基址一并挂出,供 api.js 派生 user 服务专用实例
request.Request = Request
request.USER_BASE_URL = USER_BASE_URL
module.exports = request