zhengyiming
2025-09-28 00ea64ccf296f32a0cc94dcddeaf9f34baccebbb
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';
/** 请求白名单,放置一些不需要token的接口(通过设置请求白名单,防止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) {
@@ -171,6 +186,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();
        const userStore = useUserStoreWithOut();
@@ -215,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'],
          };