实现部分页面前后端相通

This commit is contained in:
2026-07-16 17:29:27 +08:00
parent 7e45ecd144
commit a9ccdab421
16 changed files with 778 additions and 184 deletions
+63
View File
@@ -0,0 +1,63 @@
const luchRequest = require('luch-request')
const Request = luchRequest.default || luchRequest
const { generateSignatureHeaders } = require('./signature')
const store = require('../store/index')
const BASE_URL = 'http://localhost:8084/api'
const http = new Request({
baseURL: BASE_URL,
timeout: 15000,
header: {
'Content-Type': 'application/json'
}
})
// 请求拦截器
http.interceptors.request.use(
(config) => {
// 注入 JWT token
const token = store.getToken()
if (token) {
config.header = config.header || {}
config.header.Authorization = 'Bearer ' + token
}
// 注入签名头
const method = (config.method || 'GET').toUpperCase()
const url = config.url || ''
const body = config.data
const signatureHeaders = generateSignatureHeaders(method, url, body)
config.header = config.header || {}
Object.assign(config.header, signatureHeaders)
return config
},
(error) => Promise.reject(error)
)
// 响应拦截器
http.interceptors.response.use(
(response) => {
// 直接返回 data,统一解包
if (response.statusCode === 200) {
return response.data
}
return response
},
(error) => {
// 401 处理:清除登录态,跳转登录页
if (error.statusCode === 401) {
store.clearLogin()
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
if (currentPage && currentPage.route !== 'pages/login/login') {
uni.reLaunch({ url: '/pages/login/login' })
}
}
return Promise.reject(error)
}
)
module.exports = http