wlzboy
2025-12-26 c10b1e130ccbc94e6481a43e8e2d35cfc8fcf83b
app/utils/request.js
@@ -11,8 +11,10 @@
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  config.header = config.header || {}
  if (getToken() && !isToken) {
    config.header['Authorization'] = 'Bearer ' + getToken()
  const token = getToken()
  // 只有当token存在且不为空字符串时才添加到header
  if (token && token.trim() !== '' && !isToken) {
    config.header['Authorization'] = 'Bearer ' + token
  }
  // get请求映射params参数
  if (config.params) {
@@ -37,6 +39,10 @@
        }
        const code = res.data.code || 200
        const msg = errorCode[code] || res.data.msg || errorCode['default']
        // 特殊处理:checkDuplicate 接口即使返回错误码也要进入 then 分支
        const isCheckDuplicateApi = config.url && config.url.includes('/task/checkDuplicate')
        if (code === 401) {
          showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
            if (res.confirm) {
@@ -47,10 +53,26 @@
          })
          reject('无效的会话,或者会话已过期,请重新登录。')
        } else if (code === 500) {
          toast(msg)
          // checkDuplicate 接口不显示 toast,让业务代码自己处理
          if (!isCheckDuplicateApi) {
            toast(msg)
          }
          // checkDuplicate 接口返回完整响应,不 reject
          if (isCheckDuplicateApi) {
            resolve(res.data)
            return
          }
          reject('500')
        } else if (code !== 200) {
          toast(msg)
          // checkDuplicate 接口不显示 toast,让业务代码自己处理
          if (!isCheckDuplicateApi) {
            toast(msg)
          }
          // checkDuplicate 接口返回完整响应,不 reject
          if (isCheckDuplicateApi) {
            resolve(res.data)
            return
          }
          reject(code)
        }
        resolve(res.data)