openEncryption.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. import { decodeURIComponentSafe } from '@/utils/index'
  13. /**
  14. *
  15. * @param { str } raw 参数用&隔开
  16. * @param { Object } body
  17. * @returns
  18. */
  19. export const encryptionFun = ({raw, body, appId, AppSecret, timestamp}) => {
  20. const initSign = {
  21. appId,
  22. nonce: generateUUID(),
  23. timestamp,
  24. }
  25. const _initSignArr = Object.keys(initSign).map(key => {
  26. return `${key}=${initSign[key]}`
  27. })
  28. const _initSign = _initSignArr.join('&')
  29. const paramsStr = _initSign + AppSecret
  30. let str = ''
  31. if (raw) {
  32. str += decodeURIComponentSafe(raw)
  33. }
  34. if (body && Object.keys(body).length) {
  35. str += decodeURIComponentSafe(JSON.stringify(body))
  36. }
  37. return {
  38. ...initSign,
  39. sign: sha256(str + paramsStr)
  40. }
  41. }