瀏覽代碼

开启参数加密

lifanagju_citu 6 月之前
父節點
當前提交
628cbfe93a
共有 10 個文件被更改,包括 108 次插入11 次删除
  1. 1 0
      api/common.js
  2. 1 0
      api/resume.js
  3. 1 0
      api/sign.js
  4. 3 0
      api/user.js
  5. 1 0
      package.json
  6. 19 10
      unpackage/dist/cache/.vite/deps/_metadata.json
  7. 1 1
      utils/config.js
  8. 30 0
      utils/index.js
  9. 46 0
      utils/openEncryption.js
  10. 5 0
      utils/request.js

+ 1 - 0
api/common.js

@@ -227,6 +227,7 @@ export const getConversationSync = async (data) => {
     method: 'POST',
     data,
     custom: {
+      openEncryption: true,
       showLoading: false,
       auth: true
     }

+ 1 - 0
api/resume.js

@@ -7,6 +7,7 @@ export const saveResumeBasicInfo = async (data) => {
     method: 'POST',
     data,
     custom: {
+      openEncryption: true,
       showLoading: false,
       auth: true
     }

+ 1 - 0
api/sign.js

@@ -68,6 +68,7 @@ export const getUserAccount = () => {
     url: '/app-api/menduner/system/mde-user/get/account',
     method: 'GET',
     custom: {
+      openEncryption: true,
       showLoading: false,
       auth: true
     }

+ 3 - 0
api/user.js

@@ -7,6 +7,7 @@ export const getBaseInfo = (params) => {
     method: 'GET',
     params,
     custom: {
+      openEncryption: true,
       showLoading: false,
       auth: true
     }
@@ -20,6 +21,7 @@ export const getUserInfo = (params) => {
     method: 'GET',
     params,
     custom: {
+      openEncryption: true,
       showLoading: false,
       auth: true
     }
@@ -262,6 +264,7 @@ export const saveBaseInfo = (data) => {
     method: 'POST',
     data,
     custom: {
+      openEncryption: true,
       auth: true,
       showLoading: false
     }

+ 1 - 0
package.json

@@ -8,6 +8,7 @@
     "pinia-plugin-persist-uni": "^1.3.1",
     "pinia-plugin-persistedstate": "^4.0.1",
     "qs": "^6.13.0",
+    "js-sha256": "^0.11.0",
     "wukongimjssdk": "^1.2.10"
   }
 }

+ 19 - 10
unpackage/dist/cache/.vite/deps/_metadata.json

@@ -1,53 +1,62 @@
 {
-  "hash": "74ab89c9",
+  "hash": "008e73d0",
   "configHash": "fe45daba",
-  "lockfileHash": "c6d0d79b",
-  "browserHash": "5a8fd706",
+  "lockfileHash": "3ee825c8",
+  "browserHash": "aed7b5e8",
   "optimized": {
     "pinia-plugin-persistedstate": {
       "src": "../../../../../node_modules/pinia-plugin-persistedstate/dist/index.js",
       "file": "pinia-plugin-persistedstate.js",
-      "fileHash": "4d00ca43",
+      "fileHash": "9743c773",
       "needsInterop": false
     },
     "luch-request": {
       "src": "../../../../../node_modules/luch-request/src/lib/luch-request.js",
       "file": "luch-request.js",
-      "fileHash": "0d0c89a8",
+      "fileHash": "baa40d46",
       "needsInterop": false
     },
     "qs": {
       "src": "../../../../../node_modules/qs/lib/index.js",
       "file": "qs.js",
-      "fileHash": "9722eddf",
+      "fileHash": "0c9f25d9",
       "needsInterop": true
     },
     "lodash-es": {
       "src": "../../../../../node_modules/lodash-es/lodash.js",
       "file": "lodash-es.js",
-      "fileHash": "0db85755",
+      "fileHash": "ca082a84",
       "needsInterop": false
     },
     "js-base64": {
       "src": "../../../../../node_modules/js-base64/base64.mjs",
       "file": "js-base64.js",
-      "fileHash": "ea9b3cd0",
+      "fileHash": "730cbf16",
       "needsInterop": false
     },
     "wukongimjssdk": {
       "src": "../../../../../node_modules/wukongimjssdk/lib/wukongimjssdk.esm.js",
       "file": "wukongimjssdk.js",
-      "fileHash": "a4133a94",
+      "fileHash": "0afec14a",
       "needsInterop": false
     },
     "dayjs": {
       "src": "../../../../../node_modules/dayjs/dayjs.min.js",
       "file": "dayjs.js",
-      "fileHash": "1915cf4f",
+      "fileHash": "7d401321",
+      "needsInterop": true
+    },
+    "js-sha256": {
+      "src": "../../../../../node_modules/js-sha256/src/sha256.js",
+      "file": "js-sha256.js",
+      "fileHash": "f4d7511d",
       "needsInterop": true
     }
   },
   "chunks": {
+    "chunk-VKT5GLMM": {
+      "file": "chunk-VKT5GLMM.js"
+    },
     "chunk-BQWMX7FD": {
       "file": "chunk-BQWMX7FD.js"
     }

+ 1 - 1
utils/config.js

@@ -4,7 +4,7 @@ const baseUrls = {
   httpTest: 'http://menduner.citupro.com:7878',
   httpsTest: 'https://menduner.citupro.com:2443',
 }
-export const baseUrl = baseUrls.produce
+export const baseUrl = baseUrls.httpsTest
 
 // 租户id
 export const tenantId = '155'

+ 30 - 0
utils/index.js

@@ -125,3 +125,33 @@ export const checkIsImage = (url) => {
 
   return null
 }
+
+export const generateUUID = (len = 32, firstU = true, radix = null) => {
+  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
+  const uuid = [];
+  radix = radix || chars.length;
+
+  if (len) {
+    // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
+    for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
+  } else {
+    let r;
+    // rfc4122标准要求返回的uuid中,某些位为固定的字符
+    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
+    uuid[14] = '4';
+
+    for (let i = 0; i < 36; i++) {
+      if (!uuid[i]) {
+        r = 0 | (Math.random() * 16);
+        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
+      }
+    }
+  }
+  // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
+  if (firstU) {
+    uuid.shift();
+    return `u${uuid.join('')}`;
+  }
+  return uuid.join('');
+}
+

+ 46 - 0
utils/openEncryption.js

@@ -0,0 +1,46 @@
+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)
+  const initSign = {
+    appId: 'web_client', // 与后端协商一致使用
+    nonce: generateUUID(), // 前端生成唯一参数
+    timestamp: new Date().getTime(),
+  }
+  // 固定的参数初始化成字符串
+  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(sortObjectByKey(config.data))) : config.params && Object.keys(config.params).length ?
+    decodeURIComponent(qs.stringify(sortObjectByKey(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).header[key] = initSign[key] })
+}
+
+function sortObjectByKey(obj) {
+  return obj
+  // return Object.keys(obj)
+  //   .sort()
+  //   .reduce((sortedObj, key) => {
+  //     sortedObj[key] = obj[key];
+  //     return sortedObj;
+  //   }, {});
+}

+ 5 - 0
utils/request.js

@@ -10,6 +10,8 @@ import { baseUrl, tenantId, apiPath } from './config'
 import { showAuthModal } from '@/hooks/useModal'
 import { rewardEventTrackClick } from '@/api/integral'
 import { getSuffixAfterPrefix } from '@/utils/prefixUrl'
+import { encryptionFun } from '@/utils/openEncryption'
+
 import qs from 'qs'
 
 const RETURNED_API = '/admin-api/menduner/reward/event-track/click'
@@ -113,6 +115,9 @@ http.interceptors.request.use(
 				});
 		}
 
+    // 开启参数加密
+    if (config.custom?.openEncryption) encryptionFun(config)
+
     // 增加 token 令牌、terminal 终端、tenant 租户的请求头
 		const token = getAccessToken();
 		if (token) {