openEncryption.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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, timestamp}) => {
  19. const initSign = {
  20. appId,
  21. nonce: generateUUID(),
  22. timestamp,
  23. }
  24. const _initSignArr = Object.keys(initSign).map(key => {
  25. return `${key}=${initSign[key]}`
  26. })
  27. const _initSign = _initSignArr.join('&')
  28. const paramsStr = _initSign + AppSecret
  29. let str = ''
  30. if (raw) {
  31. str += decodeURIComponent(raw)
  32. }
  33. if (body && Object.keys(body).length) {
  34. str += decodeURIComponent(JSON.stringify(body))
  35. }
  36. const noSign = str + paramsStr
  37. return {
  38. ...initSign,
  39. noSign: noSign,
  40. sign: sha256(noSign)
  41. }
  42. }