You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

  1. //跳转app方法
  2. export function useAppBridge() {
  3. const fullClose = (n, m) => {
  4. let result = Math.random() * (m + 1 - n) + n
  5. while (result > m) {
  6. result = Math.random() * (m + 1 - n) + n
  7. }
  8. return Math.floor(result)
  9. }
  10. const packageFun = (funName, fun = () => {}, platform, data = {}) => {
  11. const JWrandom = fullClose(10000, 99999)
  12. data.JWrandom = JWrandom
  13. window[funName + JWrandom] = fun
  14. try {
  15. const params = {
  16. name: funName,
  17. extra: { data }
  18. }
  19. switch (platform) {
  20. case 2: // app apicloud
  21. window.api.sendEvent(params)
  22. break
  23. case 3: // app ios
  24. window.webkit.messageHandlers.getTouJiaoData.postMessage(JSON.stringify(params))
  25. break
  26. case 4: // app android
  27. window.android.getTouJiaoData(JSON.stringify(params))
  28. break
  29. case 5: // app uniapp
  30. window.uni.postMessage({
  31. data: {
  32. val: JSON.stringify(params)
  33. }
  34. })
  35. break
  36. }
  37. } catch (e) {
  38. console.error('Error in packageFun:', e)
  39. }
  40. }
  41. return {
  42. packageFun,
  43. fullClose
  44. }
  45. }