From 1681cdaa1074f57d49579a6ae2adb0140a166851 Mon Sep 17 00:00:00 2001
From: shuishen <1109946754@qq.com>
Date: Mon, 29 Sep 2025 16:33:40 +0800
Subject: [PATCH] feat:移动端基础配置调整、登录页基础配置调整

---
 src/utils/request/interceptors.js |  203 +++++++++++++++++++++++---------------------------
 1 files changed, 95 insertions(+), 108 deletions(-)

diff --git a/src/utils/request/interceptors.js b/src/utils/request/interceptors.js
index 5984c15..0a45b3b 100644
--- a/src/utils/request/interceptors.js
+++ b/src/utils/request/interceptors.js
@@ -1,8 +1,17 @@
-import { useUserStore } from "@/store"
-import { getToken } from "@/utils/auth"
+import {
+  useUserStore
+} from "@/store"
+import {
+  getToken
+} from "@/utils/auth"
 import storage from "@/utils/storage"
-import { showMessage } from "./status"
-
+import {
+  showMessage
+} from "./status"
+import website from '@/config/website'
+import {
+  Base64
+} from 'js-base64'
 // 重试队列,每一项将是一个待执行的函数形式
 let requestQueue = []
 
@@ -10,10 +19,8 @@
 const repeatSubmit = config => {
   const requestObj = {
     url: config.url,
-    data:
-      typeof config.data === "object"
-        ? JSON.stringify(config.data)
-        : config.data,
+    data: typeof config.data === "object" ?
+      JSON.stringify(config.data) : config.data,
     time: new Date().getTime()
   }
   const sessionObj = storage.getJSON("sessionObj")
@@ -66,106 +73,86 @@
   })
 }
 
-function requestInterceptors (http) {
-  /**
-   * 请求拦截
-   * @param {object} http
-   */
-  http.interceptors.request.use(
-    config => {
-      // 可使用async await 做异步操作
-      // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
-      config.data = config.data || {}
-      // 自定义参数
-      const custom = config?.custom
-
-      // 是否需要设置 token
-      const isToken = custom?.auth === false
-      if (getToken() && !isToken && config.header) {
-        // token设置
-        config.header.token = getToken()
-      }
-
-      // 是否显示 loading
-      if (custom?.loading) {
-        uni.showLoading({
-          title: "加载中",
-          mask: true
-        })
-      }
-
-      // 是否需要防止数据重复提交
-      const isRepeatSubmit = custom?.repeatSubmit === false
-      if (
-        !isRepeatSubmit &&
-        (config.method === "POST" || config.method === "UPLOAD")
-      ) {
-        repeatSubmit(config)
-      }
-      return config
-    },
-    (
-      config // 可使用async await 做异步操作
-    ) => Promise.reject(config)
-  )
-}
-function responseInterceptors (http) {
-  /**
-   * 响应拦截
-   * @param {object} http
-   */
-  http.interceptors.response.use(
-    response => {
-      /* 对响应成功做点什么 可使用async await 做异步操作 */
-      const data = response.data
-      // 配置参数
-      const config = response.config
-      // 自定义参数
-      const custom = config?.custom
-
-      // 登录状态失效,重新登录
-      if (data.code === 401) {
-        return refreshToken(http, config)
-      }
-
-      // 隐藏loading
-      if (custom?.loading) {
-        uni.hideLoading()
-      }
-
-      // 请求成功则返回结果
-      if (data.code === 200) {
-        return response || {}
-      }
-
-      // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
-      if (custom?.toast !== false) {
-        uni.$u.toast(data.message)
-      }
-
-      // 请求失败则抛出错误
-      return Promise.reject(data)
-    },
-    response => {
-      // 自定义参数
-      const custom = response.config?.custom
-
-      // 隐藏loading
-      if (custom?.loading !== false) {
-        uni.hideLoading()
-      }
-
-      // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
-      if (custom?.toast !== false) {
-        const message = response.statusCode
-          ? showMessage(response.statusCode)
-          : "网络连接异常,请稍后再试!"
-        uni.$u.toast(message)
-      }
-
-      return Promise.reject(response)
+function requestInterceptors(http) {
+  http.interceptors.request.use((config) => { // 可使用async await 做异步操作
+    // 假设有token值需要在头部需要携带
+    let accessToken = uni.getStorageSync('accessToken');
+    if (accessToken) {
+      config.header['Blade-Auth'] = 'bearer ' + accessToken;
     }
-  )
+
+    // 安全请求header
+    config.header['Blade-Requested-With'] = 'BladeHttpRequest';
+    // 客户端认证参数
+    config.header['Authorization'] = 'Basic ' + Base64.encode(website.clientId + ':' + website.clientSecret);
+
+    // #ifndef H5
+    let url = config.url
+    if (process.env.NODE_ENV == 'development' && !url.startsWith("http")) {
+      url = url.substring(url.indexOf('/api') + 4)
+      config.url = url
+    }
+    // #endif
+
+    // 额外参数
+    // config.data = config.data || {};
+    // config.data.pf = uni.getSystemInfoSync().platform;
+    // config.data.sys = uni.getSystemInfoSync().system;
+
+    // 演示custom 用处
+    // if (config.custom.auth) {
+    //   config.header.token = 'token'
+    // }
+    // if (config.custom.loading) {
+    //  uni.showLoading()
+    // }
+    /**
+     /* 演示
+     if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
+        return Promise.reject(config)
+      }
+     **/
+    return config
+  }, config => { // 可使用async await 做异步操作
+    return Promise.reject(config)
+  })
 }
 
-export { requestInterceptors, responseInterceptors }
+function responseInterceptors(http) {
+  http.interceptors.response.use((response) => {
+    console.log(response, 2222)
+    // 若有数据返回则通过
+    if (response.data.access_token || response.data.key) {
+      return response.data
+    }
+    // 服务端返回的状态码不等于200,则reject()
+    if (response.data.code !== 200) {
+      uni.showToast({
+        title: response.data.msg,
+        icon: 'none'
+      });
+      return Promise.reject(response);
+    }
+    return response.data;
+  }, (response) => {
+    console.log(response, 111)
+    /*  对响应错误做点什么 (statusCode !== 200)*/
+    uni.showToast({
+      title: response.data.msg,
+      icon: 'none'
+    });
+    if (response.statusCode == 401) {
+      const pages = getCurrentPages()
+      const currentPage = pages[pages.length - 1]
+      uni.redirectTo({
+        url: `/pages/login/login-account?redirect=/${currentPage.route}`
+      })
+    }
+    return Promise.reject(response)
+  })
+}
+
+export {
+  requestInterceptors,
+  responseInterceptors
+}

--
Gitblit v1.9.3