openEncryption.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * encryptionFun()
  3. 示例1 http://xxx.com?id=123
  4. encryptionFun('id=123')
  5. 示例2 http://xxx.com {"id":123}
  6. encryptionFun('{"id":123}')
  7. 示例3 http://xxx.com?id=123&name=张三 {"id":123}
  8. encryptionFun('id=123&name=张三{"id":123}')
  9. */
  10. import { generateUUID } from "@/utils/index"
  11. import { sha256 } from 'js-sha256'
  12. /**
  13. *
  14. * @param { str } raw 参数用&隔开
  15. * @param { Object } body
  16. * @returns
  17. */
  18. export const encryptionFun = ({raw, body, appId, AppSecret}) => {
  19. const initSign = {
  20. appId,
  21. nonce: generateUUID(),
  22. timestamp: new Date().getTime() + 3000,
  23. }
  24. const _initSign = Object.keys(initSign).reduce((str, key) => str += `&${key}=${initSign[key]}`, '')
  25. const paramsStr = _initSign.slice(1, _initSign.length) + AppSecret
  26. let str = ''
  27. if (raw) {
  28. str += decodeURIComponent(raw)
  29. }
  30. if (body && Object.keys(body).length) {
  31. str += decodeURIComponent(JSON.stringify(body))
  32. }
  33. // console.log('str:', str, 'paramsStr:', paramsStr)
  34. return {
  35. ...initSign,
  36. sign: sha256(str + paramsStr)
  37. }
  38. }