From c5558ae0a6152b56b2b487b94f3c616648104edd Mon Sep 17 00:00:00 2001
From: shuishen <1109946754@qq.com>
Date: Wed, 30 Oct 2024 18:10:32 +0800
Subject: [PATCH] 页面登录,请求拦截,响应拦截,持久化存储,路由拦截等处理

---
 src/utils/http.js |   50 ++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/src/utils/http.js b/src/utils/http.js
index 7c162c2..8ffb783 100644
--- a/src/utils/http.js
+++ b/src/utils/http.js
@@ -19,6 +19,11 @@
 
 import NProgress from 'nprogress' // progress bar
 import 'nprogress/nprogress.css' // progress bar style
+import { Base64 } from 'js-base64'
+import { baseUrl } from '@/config/env'
+import crypto from '@/utils/crypto'
+
+import { useLogin } from '@/store/login'
 
 // 全局未授权错误提示状态,只提示一次
 let isErrorShown = false
@@ -38,7 +43,12 @@
 //http request拦截
 axios.interceptors.request.use(
   config => {
-    console.log(config, '906456456465')
+    const store = useLogin()
+    //地址为已经配置状态则不添加前缀
+    if (!isURL(config.url) && !config.url.startsWith(baseUrl)) {
+      config.url = baseUrl + config.url
+    }
+
     // start progress bar
     NProgress.start()
     // 初始化错误提示状态
@@ -46,23 +56,39 @@
 
     //安全请求header
     config.headers['Blade-Requested-With'] = 'BladeHttpRequest'
+    //headers判断是否需要
+    const authorization = config.authorization === false
+    if (!authorization) {
+      config.headers['Authorization'] = `Basic ${Base64.encode(
+        `zhyq:zhyq_secret`
+      )}`
+    }
 
     //headers判断请求是否携带token
     const meta = config.meta || {}
+    const isToken = meta.isToken === false
     //headers传递token是否加密
+    const cryptoToken = config.cryptoToken === true
     //判断传递数据是否加密
+    const cryptoData = config.cryptoData === true
+    const token = store.token
 
+    if (token && !isToken) {
+      config.headers[website.tokenHeader] = cryptoToken
+        ? 'crypto ' + crypto.encryptAES(token, crypto.cryptoKey)
+        : 'bearer ' + token
+    }
     // 开启报文加密
-    // if (cryptoData) {
-    //   if (config.params) {
-    //     const data = crypto.encryptAES(JSON.stringify(config.params), crypto.aesKey);
-    //     config.params = { data };
-    //   }
-    //   if (config.data) {
-    //     config.text = true;
-    //     config.data = crypto.encryptAES(JSON.stringify(config.data), crypto.aesKey);
-    //   }
-    // }
+    if (cryptoData) {
+      if (config.params) {
+        const data = crypto.encryptAES(JSON.stringify(config.params), crypto.aesKey)
+        config.params = { data }
+      }
+      if (config.data) {
+        config.text = true
+        config.data = crypto.encryptAES(JSON.stringify(config.data), crypto.aesKey)
+      }
+    }
     //headers中配置text请求
     if (config.text === true) {
       config.headers['Content-Type'] = 'text/plain'
@@ -79,7 +105,7 @@
   res => {
     NProgress.done()
     const status = res.data.error_code || res.data.code || res.status
-    const statusWhiteList = website.statusWhiteList || []
+    const statusWhiteList = []
     const message = res.data.msg || res.data.error_description || '系统错误'
     const config = res.config
     const cryptoData = config.cryptoData === true

--
Gitblit v1.9.3