From 67d4bf09d8d40aa5121e61dde63030fc958e2a14 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Wed, 13 Oct 2021 09:40:54 +0800 Subject: [PATCH] 优化列表筛选器组件显示兼容性 --- common/Bridge.js | 57 +++++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 39 insertions(+), 18 deletions(-) diff --git a/common/Bridge.js b/common/Bridge.js index d5cdfc2..9729423 100644 --- a/common/Bridge.js +++ b/common/Bridge.js @@ -22,6 +22,9 @@ * window.linking('{method:\'methodName\', param:{key2:\'value2\'}, marker:\'mk222at1541994536008\'}'); */ +import { Fetcher } from '@components/bases/Fetcher'; +import { Tools } from '@components/common/Tools'; + export class Bridge { constructor() { @@ -42,7 +45,7 @@ */ _checkLinking() { // 安卓注入 - if (window.linking) { + if (window.aisim && window.aisim.linking) { return 'android'; } // 没有注入 @@ -84,16 +87,20 @@ */ _sendLinking(method, param, callback) { // 数据检查 - if (Object.prototype.toString.call(param) !== '[object Object]') { + if (!Tools.isObject(param)) { console.error('$bridge.invoking 需要接受 JSON 对象!'); return; } + // 转换发送参数键名为下划线 + param = this.transKeyName('underline', param); // 如果有回调,转存回调 const name = 'cb' + this._data.count++ + 'at' + Date.now(); this[name] = (res) => { - if (callback && Object.prototype.toString.call(callback) === '[object Function]') { + if (callback && Tools.isFunction(callback)) { if (res) { - const data = typeof res === 'string' ? JSON.parse(res) : res; + let data = typeof res === 'string' ? JSON.parse(res) : res; + // 转换接收参数键名为驼峰 + data = this.transKeyName('camel', data); callback(data); } else { callback(); @@ -102,7 +109,7 @@ delete this[name]; }; // 发送 - window.linking(JSON.stringify({ + window.aisim.linking(JSON.stringify({ method, param, callback: 'bridge.' + name, @@ -117,12 +124,9 @@ */ invoking(method, param, callback) { // param 为函数时 - const trans = param; - if (trans && Object.prototype.toString.call(trans) === '[object Function]') { - callback = trans; + if (param && Tools.isFunction(param)) { + callback = param; param = {}; - } else { - callback = null; } if (this._checkLinking()) { this._sendLinking(method, param, callback); @@ -141,20 +145,26 @@ window.telling = (res) => { const data = typeof res === 'string' ? JSON.parse(res) : res; const { method, param, marker } = data; + // 转换接收参数键名为驼峰 + const param2 = this.transKeyName('camel', param); if (this._receives[method]) { + // 有通知回调 if (marker) { - this._receives[method](param, (param2) => { - this._sendTelling(method, param2, marker); + this._receives[method](param2, (param2) => { + debugger; + this._sendTelling(method, param2 || {}, marker); }); - } else { - this._receives[method](param); + } + // 无通知回调 + else { + this._receives[method](param2); } } }; } /** - * 发送响应 + * 回发App通知的响应 * @param {string} method * @param {object} param * @param {string} marker @@ -162,12 +172,14 @@ */ _sendTelling(method, param, marker) { // 数据检查 - if (Object.prototype.toString.call(param) !== '[object Object]') { + if (!Tools.isObject(param)) { console.error('$bridge.register 注册的函数需要接受 JSON 对象!'); return; } + // 转换发送参数键名为下划线 + param = this.transKeyName('underline', param); // 发送 - window.linking(JSON.stringify({ + window.aisim.linking(JSON.stringify({ method, param, marker, @@ -175,7 +187,7 @@ } /** - * 注册接收指令,可供 app 查询 + * 注册接收指令,可接收 app 通知 * @param method * @param callback */ @@ -191,6 +203,15 @@ return platform === 'android' || platform === 'iOS'; } + /** + * 键名转换 + * @param type + * @param json + */ + transKeyName(type, json) { + return Fetcher.prototype.transKeyName(type, json); + } + } // 全局服务实例 -- Gitblit v1.9.1