UNPKG

olympus-r-17plugins

Version:

A plugin of Olympus for 17zuoye.

126 lines (125 loc) 4.65 kB
import * as tslib_1 from "tslib"; import { ClientType, Platform } from "./Shell17"; import Shell17Mobile from "./Shell17Mobile"; /** * @author Raykid * @email initial_r@qq.com * @create date 2017-10-25 * @modify date 2017-10-25 * * 针对一起作业IOS外壳 */ var Shell17IOS = /** @class */ (function (_super) { tslib_1.__extends(Shell17IOS, _super); function Shell17IOS() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._triggerName = 'olympus_trigger_'; _this._triggerID = 0; _this._externalID = '_17m_external'; // 记录需要返回值的壳方法 _this._needCallbackDict = { getInitParams: true, localStorageGet: true, }; // TODO 记录需要把多参数转换为 {0:arg1, 1:arg2 ...} 形式 _this._transfromArgumentsDict = {}; return _this; } Object.defineProperty(Shell17IOS.prototype, "platform", { get: function () { return Platform.IOS17; }, enumerable: true, configurable: true }); Object.defineProperty(Shell17IOS.prototype, "clientType", { get: function () { return ClientType.UNKNOWN; }, enumerable: true, configurable: true }); /** * iOS webkit 实现,可能会返回Promise * @param funcName * @param paramsList */ Shell17IOS.prototype.callExternal = function (funcName) { var _this = this; var paramsList = []; for (var _i = 1; _i < arguments.length; _i++) { paramsList[_i - 1] = arguments[_i]; } if (this.isWebkit && window.webkit.messageHandlers[this._externalID] && window.webkit.messageHandlers[this._externalID].postMessage) { var needCallback = !!this._needCallbackDict[funcName]; var msg = { method: funcName, }; // TODO // 单参为json,直接传递 // 多参数要转化成 {0:arg1, 1:arg2 ...} // 此处逻辑漏洞,根据字符串参数是否可以被反序列化确定是否为多参数调用 // 单参但如果是json结构,但是壳认为参数不合理,则无法处理,例 log_b 方法实现 if (paramsList.length === 1) { var param = paramsList[0]; try { if (typeof JSON.parse(param) === 'object') { msg.param = param; } } catch (error) { msg.param = JSON.stringify({ 0: param, }); } } else if (paramsList.length > 1) { var param_1 = {}; paramsList.forEach(function (value, index) { param_1[index] = value; }); msg.param = JSON.stringify(param_1); } if (needCallback) { var type_1 = this._triggerName + this._triggerID; this._triggerID += 1; msg.callback = '_17m.' + type_1; var promise = new Promise(function (resolve, reject) { _this.bindTrigger(type_1, function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } resolve.apply(void 0, args); }, true); }); window.webkit.messageHandlers[this._externalID].postMessage(msg); return promise; } else { window.webkit.messageHandlers[this._externalID].postMessage(msg); return new Promise(function (resolve, reject) { resolve(); }); } } else { return _super.prototype.callExternal.apply(this, [funcName].concat(paramsList)); } }; Shell17IOS.prototype.log_b = function (data) { if (data == null) return; // 调用外壳接口 if (this.isWebkit) { this.callExternal("log_b", JSON.stringify({ 0: '', 1: JSON.stringify(data) })); } else { _super.prototype.log_b.call(this, data); } }; return Shell17IOS; }(Shell17Mobile)); export default Shell17IOS;