瀏覽代碼

开启参数加密

lifanagju_citu 7 月之前
父節點
當前提交
8d59fe23ba
共有 4 個文件被更改,包括 50 次插入35 次删除
  1. 1 0
      components.d.ts
  2. 13 2
      src/config/axios/service.js
  3. 36 32
      src/utils/openEncryption.js
  4. 0 1
      src/views/login/index.vue

+ 1 - 0
components.d.ts

@@ -17,6 +17,7 @@ declare module 'vue' {
     Combobox: typeof import('./src/components/FormUI/combobox/index.vue')['default']
     ComboboxZhAndEn: typeof import('./src/components/FormUI/comboboxZhAndEn/index.vue')['default']
     ConfirmPaymentDialog: typeof import('./src/components/pay/confirmPaymentDialog.vue')['default']
+    copy: typeof import('./src/components/CtForm/index copy.vue')['default']
     CtBtn: typeof import('./src/components/CtVuetify/CtBtn/index.vue')['default']
     CtDialog: typeof import('./src/components/CtDialog/index.vue')['default']
     CtForm: typeof import('./src/components/CtForm/index.vue')['default']

+ 13 - 2
src/config/axios/service.js

@@ -76,8 +76,7 @@ service.interceptors.request.use(
       (config).headers.Authorization = 'Bearer ' + getToken(tokenIndex) // 让每个请求携带自定义token
     }
 
-    // 开启参数加密
-    if (config.openEncryption) encryptionFun(config)
+
 
     // 设置租户
     if (tenantEnable && tenantEnable === 'true') {
@@ -101,6 +100,18 @@ service.interceptors.request.use(
         config.url = config.url + '?' + paramsStr
       }
     }
+
+    // 开启参数加密
+    if (config.openEncryption) {
+      console.log(config)
+      const raw = config.url.split('?')[1]
+      const body = {
+        ...config.data,
+        ...config.params
+      }
+      const header = encryptionFun({raw, body, appId: 'web_client', AppSecret: 'fa0fc0b5098b974b'})
+      Object.assign(config.headers, header)
+    }
     
     if (isToken) {
       // 截取request url

+ 36 - 32
src/utils/openEncryption.js

@@ -1,38 +1,42 @@
+/**
+ * encryptionFun()
+  示例1 http://xxx.com?id=123
+  encryptionFun('id=123')
+
+  示例2 http://xxx.com   {"id":123}
+  encryptionFun('{"id":123}')
+
+  示例3 http://xxx.com?id=123&name=张三   {"id":123}
+  encryptionFun('id=123&name=张三{"id":123}')
+*/
+
 import { generateUUID } from "@/utils/index" 
 import { sha256 } from 'js-sha256'
-import qs from 'qs'
-
-// 加密方式:请求头加参数: appId + nonce + timestamp + sign
-// (sign为: queryJsonData+paramsToStrSort+appSecret拼接后sha256加密字符串)
 
-// 开启参数加密
-export const encryptionFun = (config) => {
-  // console.log('加密内容用完请注释->config', config)
+/**
+ * 
+ * @param { str } raw 参数用&隔开
+ * @param { Object } body
+ * @returns 
+*/
+export const encryptionFun = ({raw, body, appId, AppSecret}) => {
   const initSign = {
-    appId: 'web_client', // 与后端协商一致使用
-    nonce: generateUUID(), // 前端生成唯一参数
-    timestamp: new Date().getTime() + 3000, // 多加两秒时间
+    appId,
+    nonce: generateUUID(),
+    timestamp: new Date().getTime() + 3000,
+  }
+  const _initSign = Object.keys(initSign).reduce((str, key) => str += `&${key}=${initSign[key]}`, '')
+  const paramsStr = _initSign.slice(1, _initSign.length) + AppSecret
+  let str = ''
+  if (raw) {
+    str += raw
+  }
+  if (body && Object.keys(body).length) {
+    str += decodeURIComponent(JSON.stringify(body))
+  }
+  // console.log('str:', str, 'paramsStr:', paramsStr)
+  return {
+    ...initSign,
+    sign: sha256(str + paramsStr)
   }
-  // 固定的参数初始化成字符串
-  const paramsToStrSort = ['appId', 'nonce', 'timestamp'] // 顺序不能变
-  let paramsStr = paramsToStrSort.reduce((str, key) => {
-    if (initSign[key]) str = str ? str + `&${key}=${initSign[key]}` : `${key}=${initSign[key]}`
-    return str
-  }, '')
-  const appSecret = 'fa0fc0b5098b974b' // 与后端协商一致使用(拼接在paramsStr后面,且拼接时不加key)
-  paramsStr = paramsStr + appSecret
-  // console.log('加密内容用完请注释->paramsStr', paramsStr)
-
-  // 请求的参数json // 携带的参数json一下,url拼接参数的直接用,没有参数不拼接
-  const queryJsonData = config.data && Object.keys(config.data).length ?
-    decodeURIComponent(JSON.stringify(config.data)) :
-    config.params && Object.keys(config.params).length ?
-    decodeURIComponent(qs.stringify(config.params, { allowDots: true }) ) :
-    config.url.split('?')?.length>1 ?
-    config.url.split('?')[1] : ''
-  // sha256加密字符串
-  if (paramsStr) initSign.sign = sha256(queryJsonData + paramsStr)
-  // console.log('加密内容用完请注释->queryJsonData', queryJsonData)
-  // 请求头加参数initSign,请求头加参数: appId + nonce + timestamp + sign
-  if (initSign &&  Object.keys(initSign).length) Object.keys(initSign).forEach(key => { (config).headers[key] = initSign[key] })
 }

+ 0 - 1
src/views/login/index.vue

@@ -102,7 +102,6 @@ import navBar from '@/layout/personal/navBar.vue'
 import about from '@/views/about/index.vue'
 import { useRoute } from 'vue-router'; const route = useRoute()
 import Verify from '@/components/Verifition'
-console.log(1, '23456', 789)
 
 const isMobile = ref(false)
 onMounted(() => {