Blame view

ceres-uniapp-master/utils/jiami.js 2.39 KB
ff75800c   杨鑫   'zuix'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  // 文本加密
  import { sm4 } from 'sm-crypto';
  const DEFAULT_KEY ="cda4442f102f6396eea76902e37ad7cb";
  const DEFAULT_IV = "8bd8a83221742111c7532b7275a7fe9c";
  export const encrypt = (str) => {
      if(!str) {
          return ''
      }
      const encryptStr = sm4.encrypt(str, DEFAULT_KEY, {
          iv: DEFAULT_IV,
          mode: 'cbc',
          padding: 'pkcs#7'
          })
      return encryptStr
  }
  
  // 文本解密
  export const decrypt = (str) => {
      if(!str) {
          return ''
      }
  //     if(str.indexOf('ENC(') === -1) {
  //         return str
  //     }
  //     const str_ = str.replace('ENC(', '').replace(')', '')
      try {        
          const decryptStr = sm4.decrypt(str, DEFAULT_KEY, {
              iv: DEFAULT_IV,
              mode: 'cbc',
              padding: 'pkcs#7'
          })
          return decryptStr
      } catch (error) {
          return '解密失败!'
      }
  }
b12ba7ef   杨鑫   '最新'
37
  // 批量加密
ff75800c   杨鑫   'zuix'
38
  export const decryptall = (obj,isjmlist) => {
b12ba7ef   杨鑫   '最新'
39
40
  
  
ff75800c   杨鑫   'zuix'
41
42
43
44
45
46
47
48
49
      let list  = []
      let objnew = {}
      for (let key in obj) {
          let item = {
              typeis:'2',//是否加密 1加密 2不加密
              keyname:key,//变量名
              value:obj[key],//值
          }
          if(isjmlist.length> 0) {
b12ba7ef   杨鑫   '最新'
50
51
52
53
  
              // let found = isjmlist.find(function(element) {
              //     return key == element;
              // });
ff75800c   杨鑫   'zuix'
54
55
56
57
58
59
60
              console.log(isjmlist.indexOf(key) != -1)
              if(isjmlist.indexOf(key) != -1) {
                  item.typeis = '1'
              }
          } else {
              item.typeis = '1'
          }
b12ba7ef   杨鑫   '最新'
61
  
ff75800c   杨鑫   'zuix'
62
63
          list.push(item)
      }
b12ba7ef   杨鑫   '最新'
64
65
  
  
ff75800c   杨鑫   'zuix'
66
67
68
69
70
71
72
      list.forEach(element => {
          if(element.typeis == '1' && element.value) {
              console.error(element)
              element.value = encrypt(element.value)
          }
          objnew[element.keyname] = element.value
      });
b12ba7ef   杨鑫   '最新'
73
  
ff75800c   杨鑫   'zuix'
74
      return objnew
b12ba7ef   杨鑫   '最新'
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  }
  // 批量解密
  export const encryptall = (list,isjmlist) => {
  
  
      let newlist =  list.map(element => {
          let c1 =  Object.keys(element)
          c1.forEach(key => {
              if(isjmlist.length> 0) {
                  console.log(isjmlist.indexOf(key) != -1)
                  if(isjmlist.indexOf(key) != -1) {
                      element[key] = decrypt(element[key])
                  }
              } else {
                  element[key] = decrypt(element[key])
              }
          });
          return element
      });
  
      return newlist
  }