feat(mobile-h5): adapt portal RSA login and split db HTTP client

Wire login to /web/auth/login with JSEncrypt, unwrap portal envelopes, and add a direct-JSON db client for weighbridge APIs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-08-03 22:33:46 +08:00
1 parent a4b13fa2bc
commit c3f183cfba
6 files changed
+208 -95

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
/**
* 地磅(db)模块 HTTP 客户端
* 协议:直接 JSON 响应(非门户 code/data 信封)
*/
import axios, { type AxiosRequestConfig, type InternalAxiosRequestConfig } from 'axios'
import { showToast } from 'vant'
import { useUserStore } from '@/store/modules/user'
type RetryableConfig = InternalAxiosRequestConfig & {
_retry?: boolean
showErrorMessage?: boolean
}
/** 地磅集合根路径须带尾斜杠,否则 307 会丢 Authorization */
const DB_COLLECTION_ROOT = /^\/(plans|inventory|weighing)$/
function normalizeDbUrl(url?: string): string | undefined {
if (!url || url.endsWith('/')) return url
return DB_COLLECTION_ROOT.test(url) ? `${url}/` : url
}
const getBaseURL = () => {
if (import.meta.env.DEV) return '/api'
return import.meta.env.VITE_API_URL || '/api'
}
const dbAxios = axios.create({
baseURL: getBaseURL(),
timeout: 60000,
headers: { 'Content-Type': 'application/json' }
})
dbAxios.interceptors.request.use((config: InternalAxiosRequestConfig) => {
if (config.url) {
config.url = normalizeDbUrl(config.url)
}
const store = useUserStore()
const token = store.accessToken || sessionStorage.getItem('accessToken') || ''
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
})
dbAxios.interceptors.response.use(
(res) => res.data,
async (err) => {
const status = err.response?.status
const config = err.config as RetryableConfig | undefined
if (status === 401) {
const userStore = useUserStore()
userStore.logOut()
showToast({ type: 'fail', message: '登录已过期,请重新登录' })
setTimeout(() => {
window.location.hash = '#/login'
}, 1500)
return Promise.reject(err)
}
if (config?.showErrorMessage !== false) {
const detail = err.response?.data?.detail
const message =
typeof detail === 'string'
? detail
: Array.isArray(detail)
? detail.map((x: { msg?: string }) => x.msg).filter(Boolean).join(';')
: err.response?.data?.message || err.response?.data?.msg
showToast({ type: 'fail', message: message || err.message || '网络异常' })
}
return Promise.reject(err)
}
)
function request<T = unknown>(config: AxiosRequestConfig) {
return dbAxios.request<any, T>(config)
}
const db = {
get<T = unknown>(config: AxiosRequestConfig): Promise<T> {
return request<T>({ ...config, method: 'GET' })
},
post<T = unknown>(config: AxiosRequestConfig): Promise<T> {
return request<T>({ ...config, method: 'POST' })
},
put<T = unknown>(config: AxiosRequestConfig): Promise<T> {
return request<T>({ ...config, method: 'PUT' })
},
patch<T = unknown>(config: AxiosRequestConfig): Promise<T> {
return request<T>({ ...config, method: 'PATCH' })
},
del<T = unknown>(config: AxiosRequestConfig): Promise<T> {
return request<T>({ ...config, method: 'DELETE' })
}
}
export default db