From 00ea64ccf296f32a0cc94dcddeaf9f34baccebbb Mon Sep 17 00:00:00 2001 From: zhengyiming <540361168@qq.com> Date: 星期日, 28 九月 2025 16:49:09 +0800 Subject: [PATCH] fix: 新增日志 --- apps/cMiniApp/src/utils/request/index.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 46 insertions(+), 8 deletions(-) diff --git a/apps/cMiniApp/src/utils/request/index.ts b/apps/cMiniApp/src/utils/request/index.ts index 339d938..2ebbed6 100644 --- a/apps/cMiniApp/src/utils/request/index.ts +++ b/apps/cMiniApp/src/utils/request/index.ts @@ -3,13 +3,14 @@ import qs from 'qs'; import Taro from '@tarojs/taro'; import { getToken } from '@/utils'; -import { tokenIsExpired, Message, SensitiveManage } from '@12333/utils'; +import { tokenIsExpired, Message, SensitiveManage, httpLoggerRecord } from '@12333/utils'; import { useUserStoreWithOut } from '@/stores/modules/user'; const RefreshTokenUrl = '/gettokenbyrefreshtoken'; /** 璇锋眰鐧藉悕鍗曪紝鏀剧疆涓�浜涗笉闇�瑕乼oken鐨勬帴鍙o紙閫氳繃璁剧疆璇锋眰鐧藉悕鍗曪紝闃叉token杩囨湡鍚庡啀璇锋眰閫犳垚鐨勬寰幆闂锛� */ const whiteList = [RefreshTokenUrl]; +const RefreshTokenBlackList = ['wxmpLogin']; export function startLoading(showNavigationBarLoading: boolean) { if (showNavigationBarLoading) { @@ -62,6 +63,7 @@ */ msg?: string; showType?: ErrorShowType; + traceId?: string; } interface ErrorResponse { @@ -102,6 +104,19 @@ // 閿欒鎺ユ敹鍙婂鐞� errorHandler: (error, opts) => { console.log('error: ', error); + + const url = opts.url ?? ''; + httpLoggerRecord.error({ + message: `[${url}] 璇锋眰閿欒`, + httpParams: { + url: url, + //@ts-ignore + traceId: error?.info?.traceId, + stackTrace: error.stack, + }, + args: [{ data: opts.data, params: opts.params, headers: opts.headers }], + }); + if (opts?.skipErrorHandler) throw error; if (opts?.customErrorHandler) { @@ -115,6 +130,9 @@ const errorInfo: ErrorInfo | undefined = (error as any).info; if (errorInfo) { const { errorMessage, errorCode } = errorInfo; + if (Number(errorCode) === 401) { + handleLogout(); + } switch (errorInfo.showType) { case ErrorShowType.SILENT: // do nothing @@ -138,7 +156,7 @@ } else if ((error as AxiosError<ResponseStructure, IRequestOptions>).response) { // Axios 鐨勯敊璇� // 璇锋眰鎴愬姛鍙戝嚭涓旀湇鍔″櫒涔熷搷搴斾簡鐘舵�佺爜锛屼絾鐘舵�佷唬鐮佽秴鍑轰簡 2xx 鐨勮寖鍥� - handleAxiosResponseError(error as AxiosError<ErrorResponse, IRequestOptions>); + handleAxiosResponseError(error as AxiosError<ResponseStructure, IRequestOptions>); // Message.errorMessage(`Response status:${(error as AxiosError).response.status}`); } else if ((error as AxiosError).request) { // 璇锋眰宸茬粡鎴愬姛鍙戣捣锛屼絾娌℃湁鏀跺埌鍝嶅簲 @@ -167,6 +185,14 @@ [ (config) => { const $config = config; + + httpLoggerRecord.info({ + message: `[${$config.url}] 璇锋眰寮�濮媊, + httpParams: { + url: $config.url, + }, + args: [{ data: $config.data, params: $config.params, headers: $config.headers }], + }); const token = getToken(); @@ -212,12 +238,24 @@ (response) => { const $config = response.config as IRequestOptions; + httpLoggerRecord.info({ + message: `[${$config.url}] 璇锋眰缁撴潫`, + httpParams: { + url: $config.url, + traceId: response.data?.traceId, + }, + args: [{ data: $config.data, params: $config.params, headers: $config.headers }], + }); + const { needNProcess, getResponse = false } = $config; const userStore = useUserStoreWithOut(); - if (response.headers['x-access-token']) { - const tokenInfo: API.PasswordLoginCommandCallback = { + if ( + response.headers['x-access-token'] && + RefreshTokenBlackList.every((url) => !response.config.url?.includes(url)) + ) { + const tokenInfo: API.LoginCommandCallback = { accessToken: response.headers['access-token'], refreshToken: response.headers['x-access-token'], }; @@ -251,17 +289,17 @@ [505]: 'HTTP鐗堟湰涓嶅彈鏀寔', }; -function handleAxiosResponseError(error: AxiosError<ErrorResponse, IRequestOptions>) { +function handleAxiosResponseError(error: AxiosError<ResponseStructure, IRequestOptions>) { if (error.response.config.url.toLowerCase().includes(RefreshTokenUrl.toLowerCase())) { handleLogout(); return; } if (error && error.response) { let message = ErrorMessageMap[error.response?.status] ?? '璇锋眰閿欒'; - if (error.response.data?.error?.message) { - message = error.response.data?.error?.message; + if (error.response.data?.msg) { + message = error.response.data?.msg; } - if (error.response?.status === 401) { + if (error.response?.status === 401 || error.response.data.code === 401) { handleLogout(); } -- Gitblit v1.9.1