openEncryption.js 804 B

123456789101112131415161718192021222324252627282930313233
  1. import { generateUUID } from "@/utils/index"
  2. import { sha256 } from 'js-sha256'
  3. import { decodeURIComponentSafe } from '@/utils/index'
  4. /**
  5. * @param { str } raw 参数用&隔开
  6. * @param { Object } body
  7. * @returns
  8. */
  9. export const encryptionFun = ({raw, body, appId, AppSecret, timestamp}) => {
  10. const initSign = {
  11. appId,
  12. nonce: generateUUID(),
  13. timestamp,
  14. }
  15. const _initSignArr = Object.keys(initSign).map(key => {
  16. return `${key}=${initSign[key]}`
  17. })
  18. const _initSign = _initSignArr.join('&')
  19. const paramsStr = _initSign + AppSecret
  20. let str = ''
  21. if (raw) {
  22. str += decodeURIComponentSafe(raw)
  23. }
  24. if (body && Object.keys(body).length) {
  25. str += decodeURIComponentSafe(JSON.stringify(body))
  26. }
  27. return {
  28. ...initSign,
  29. sign: sha256(str + paramsStr)
  30. }
  31. }