Browse Source

等保加密

chengxiaowen 2 years ago
parent
commit
d79761d4cb
4 changed files with 105 additions and 2 deletions
  1. 1 0
      package.json
  2. 3 2
      src/api/index.js
  3. 30 0
      src/utils/aes.js
  4. 71 0
      src/utils/encryptRequest.js

+ 1 - 0
package.json

@@ -11,6 +11,7 @@
   "dependencies": {
     "@amap/amap-jsapi-loader": "^1.0.1",
     "axios": "^0.27.2",
+    "crypto-js": "^4.1.1",
     "core-js": "^3.6.5",
     "echarts": "^4.9.0",
     "image-conversion": "^2.1.1",

+ 3 - 2
src/api/index.js

@@ -1,8 +1,9 @@
-import request from '@/utils/request'
+import encryptRequest from '@/utils/encryptRequest'
 export function loginApp(data) {
-    return request({
+    return encryptRequest({
         url: '/pmadmin/mini/employee/loginApp',
         method: 'post',
+        encrypt:true,
         data
     })
 }

+ 30 - 0
src/utils/aes.js

@@ -0,0 +1,30 @@
+import CryptoJS from 'crypto-js';
+
+export default {
+  //随机生成指定数量的16进制key
+  generatekey(num) {
+  let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+  let key = "";
+  for (var i = 0; i < num; i++) {
+  let randomPoz = Math.floor(Math.random() * library.length);
+  key += library.substring(randomPoz, randomPoz + 1);
+  }
+  return key;
+  },
+
+  //加密
+  encrypt(word) {
+  var keyStr ="Hbg584782648!@#b";
+  var key = CryptoJS.enc.Utf8.parse(keyStr);
+  var srcs = CryptoJS.enc.Utf8.parse(word);
+  var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
+  return encrypted.toString();
+  },
+  //解密
+  decrypt(word) {
+  var keyStr ="Hbg584782648!@#b";
+  var key = CryptoJS.enc.Utf8.parse(keyStr);
+  var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
+  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
+  }
+}

+ 71 - 0
src/utils/encryptRequest.js

@@ -0,0 +1,71 @@
+import axios from 'axios'
+import AES from '@/utils/aes'
+
+const service = axios.create({
+    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
+})
+import {
+    Dialog
+} from 'vant';
+// request interceptor
+service.interceptors.request.use(
+    config => {
+        if (sessionStorage.getItem('x-token')) {
+            config.headers['x-token'] = sessionStorage.getItem('x-token')
+        }
+        if (config.type === 'change') {
+            config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
+        } else if (config.type === 'formData') {
+            config.headers['Content-Type'] = 'multipart/form-data;charset=utf-8;'
+        } else {
+            config.headers['Content-Type'] = 'application/json'
+        }
+        if (config.encrypt){
+          var encrypted = AES.encrypt(JSON.stringify(config.data))
+          config.data={encryptData: encrypted}
+          config.headers['toEncrypt'] = 'true'
+        }
+        return config
+    },
+    error => {
+        // do something with request error
+        console.log(error) // for debug
+        return Promise.reject(error)
+    }
+)
+
+// response interceptor
+service.interceptors.response.use(
+    response => {
+        const res = response.data
+        const config = response.config
+        if (config != null) {
+            const blob = response.config.responseType
+            if (blob == 'blob') {
+                return res
+            }
+        }
+        console.log(res)
+        if (res.code === '0001') {
+            Dialog({
+                message: res.message || 'Error'
+            });
+            return res
+        }
+        if (res.code !== '0000') {
+            Dialog({
+                message: res.message || 'Error'
+            });
+            return Promise.reject(new Error(res.message || 'Error'))
+        } else {
+          res.data = JSON.parse(AES.decrypt(res.data))
+          return res
+        }
+    },
+    error => {
+        console.log('err' + error) // for debug
+        return Promise.reject(error)
+    }
+)
+
+export default service