/** * 发送消息给 iframe */ const sendToIframe = function (type, data, requestId) { if (!this.iframeWindow) { console.warn('[Parent] Cannot send message: iframe not initialized'); return; } const message = { type, data, requestId }; console.log('[Parent] Sending message to iframe:', message); this.iframeWindow.postMessage(JSON.stringify(message), this.origin); } /** * 转换login信息 */ const translateLoginData = function(info){ return { uid: info.uid, is_first_login: info.is_first_login, is_real_name: info.is_real_name, login_time: info.login_time, reg_time: info.reg_time, user_name: info.user_name, } } var INDEX = { init: 0, // 初始化 login: 1, // 登录 logout: 2, // 登出 pay: 3, // 支付 report: 4, // 角色上报 onLogout: 5, // 浮标个人/账号中心切换账号 } window.jsBridge = function (data){ const {callback, param, token} = data switch(callback){ case INDEX.init: sendToIframe('INIT_REQUEST', {success:true, message:"初始化成功"}, token) break; case INDEX.login: sendToIframe('LOGIN_REQUEST',{success:true, message:"登录成功", data:translateLoginData(param)}, token) break; case INDEX.logout: sendToIframe('LOGOUT_REQUEST', {success:true, message:"登出成功"}, token) break; case INDEX.pay: sendToIframe('PAY_REQUEST', {success:true, message:"支付页面打开成功"}, token) break; case INDEX.report: sendToIframe('REPORT_REQUEST', {success:true, message:"角色上报成功"}, token) break; case INDEX.onLogout: sendToIframe('ONLOGOUT_REQUEST', {success:true, message:"SDK内退出账号成功"}, token) break; } }