native-sdk.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * 发送消息给 iframe
  3. */
  4. const sendToIframe = (type, data, requestId) {
  5. if (!this.iframeWindow) {
  6. console.warn('[Parent] Cannot send message: iframe not initialized');
  7. return;
  8. }
  9. const message = { type, data, requestId };
  10. console.log('[Parent] Sending message to iframe:', message);
  11. this.iframeWindow.postMessage(JSON.stringify(message), this.origin);
  12. }
  13. var INDEX = {
  14. init: 0, // 初始化
  15. login: 1, // 登录
  16. logout: 2, // 登出
  17. pay: 3, // 支付
  18. report: 4, // 角色上报
  19. onLogout: 5, // 浮标个人/账号中心切换账号
  20. }
  21. window.jsBridge({callback:1, param:参数, token:'我给你的原样给我'})
  22. window.jsBridge = function (data){
  23. const {callback, param, token} = data
  24. switch(callback){
  25. case INDEX.init:
  26. sendToIframe('INIT_REQUEST', param, token)
  27. break;
  28. case INDEX.login:
  29. sendToIframe('LOGIN_REQUEST', param, token)
  30. break;
  31. case INDEX.logout:
  32. sendToIframe('LOGOUT_REQUEST', param, token)
  33. break;
  34. case INDEX.pay:
  35. sendToIframe('PAY_REQUEST', param, token)
  36. break;
  37. case INDEX.report:
  38. sendToIframe('REPORT_REQUEST', param, token)
  39. break;
  40. case INDEX.onLogout:
  41. sendToIframe('ONLOGOUT_REQUEST', param, token)
  42. break;
  43. }
  44. }