vue开发axios请求 用SM2加密,解密

  1 import axios from "axios";
  2 import { Message } from 'element-ui'
  3 // 地址
  4 // let baseUrl = 'http://'
  5 let username = 'sesp1'
  6 //加解密开关
  7 window.ENCRYPT=false
  8 /*公钥*/
  9 var publicKey="";
 10 /*私钥*/
 11 var privateKey=""
 12 
 13 /*分割字符串*/
 14 function splitStr (str) {
 15     var len = Math.ceil(str.length / 500)
 16     var arr = Array(len)
 17     var i = 0
 18     for (; i < len; i++) {
 19         // console.log('index: %s, len: %s, str: %s', i, len, str.length)
 20         arr[i] = str.substring(0, 500)
 21         if (i < len) {
 22             str = str.substring(500, str.length)
 23         }
 24     }
 25     return arr
 26 }
 27 /**
 28  * SM2加密
 29  */
 30 function encryptSM2 (word) {
 31 
 32     var str = btoa(encodeURIComponent(JSON.stringify(word)))
 33     var arrStr = splitStr(str).map(s => window.SG_sm2Encrypt(s, publicKey))
 34     return arrStr.join('|')
 35 }
 36 
 37 /**
 38  * SM3哈希
 39  */
 40  function encryptSM3 (word) {
 41     return window.SG_sm3encrypt(word)
 42 }
 43 /**
 44  * SM2解密
 45  */
 46 function decryptSM2$$2 (word) {
 47     var strs = word.split('|').map(s => window.SG_sm2Decrypt(s, privateKey))
 48     var json=JSON.parse(decodeURIComponent(atob(strs.join(''))))
 49     return json
 50 }
 51 
 52 const service = axios.create({
 53     baseURL: baseUrl, // api 的 base_url
 54 
 55 });
 56 
 57 
 58 //1.添加请求拦截器
 59 service.interceptors.request.use((config) => {
 60     config.url = config.baseURL + config.url;
 61     if (config.data.username && config.data.username == 'sesp1') {
 62         config.data.username = username
 63     }
 64     //请求数据
 65     let baseParams=config.data;
 66     // console.log("基本参数",baseParams)
 67     let savelogParams={};
 68     // console.log("埋点参数",savelogParams)
 69 
 70     //headers封装
 71     config.headers={
 72         y:new Date().getTime(),
 73     };
 74     //埋點加密參數  注:SM3后面的参数是集成参数
 75     let secSavelogParams=window.ENCRYPT?encryptSM2(savelogParams):savelogParams
 76 
 77     if(window.ENCRYPT){
 78       config.data={
 79         iespsm:encryptSM2(baseParams),
 80         timestamp:new Date().getTime()
 81       };
 82       config.headers["SignSM3"]=encryptSM3(JSON.stringify(config.data)+secSavelogParams)
 83     }
 84     config.headers["buried"]=(window.ENCRYPT)?secSavelogParams:encodeURIComponent(JSON.stringify(savelogParams))
 85       if (window.sessionStorage.getItem("JWT")) {
 86           config.headers.Authorization = window.sessionStorage.getItem("JWT")
 87       }
 88     return config;
 89     }, (error) => {
 90     Message({
 91         showClose: true,
 92         message: '网络错误,请稍后重试!',
 93         type: "warning"
 94     });
 95     return Promise.reject(error);
 96 });
 97 //2.添加响应拦截器
 98 service.interceptors.response.use((res) => {
 99     if(window.ENCRYPT){
100         /*判断解密*/
101         if(null!=res.data.iespData&&undefined!=res.data.iespData){
102             res.data=decryptSM2$$2(res.data.iespData)
103         }    
104     }
105     // console.log("响应数据",res) 
106     return res;
107 }, (error) => {
108     Message({
109         showClose: true,
110         message: '网络错误,请稍后重试!',
111         type: "warning"
112     });
113     return Promise.reject(error);
114 });
115 
116 
117 export default service;

在main.js中引入sm.js;静态文件注入需放在public下的static目录下

posted on 2020-08-26 16:35  一往而深のcode  阅读(3846)  评论(0编辑  收藏  举报