UNPKG

olympus-r-17plugins

Version:

A plugin of Olympus for 17zuoye.

463 lines (462 loc) 17.6 kB
import * as tslib_1 from "tslib"; import { core } from "olympus-r/core/Core"; import { audioManager } from "olympus-r/engine/audio/AudioManager"; import { extendObject } from "olympus-r/utils/ObjectUtil"; import Audio17MobileImpl from "../audio/Audio17MobileImpl"; import PauseMessage from "../message/PauseMessage"; import { stringify } from "../utils/JSONUtil"; import { environment17 } from "./Environment17"; import Shell17 from "./Shell17"; /** * @author Raykid * @email initial_r@qq.com * @create date 2017-10-25 * @modify date 2017-10-25 * * */ var Shell17Mobile = /** @class */ (function (_super) { tslib_1.__extends(Shell17Mobile, _super); function Shell17Mobile() { var _this = _super.call(this) || this; // 这里要调用一下homeworkHTMLLoaded以通知一起作业移动端外壳js已经加载完毕 _this.homeworkHTMLLoaded(); // 监听load事件 _this.bindTrigger("load", function () { // 派发事件 core.dispatch(PauseMessage.APP_RESUME); }); // 监听unload事件 _this.bindTrigger("unload", function () { // 派发事件 core.dispatch(PauseMessage.APP_PAUSE); }); // 添加外壳必须的方法引用 _this.initExternalNeedFuncs(); return _this; } /** 下面为了防止一起作业外壳报错而特意加的全局转发方法 */ Shell17Mobile.prototype.initExternalNeedFuncs = function () { var vox = {}; try { Object.defineProperty(window, "vox", { get: function () { return vox; }, set: function (value) { extendObject(vox, value); } }); } catch (err) { window["vox"] = vox; } var task = {}; try { Object.defineProperty(vox, "task", { get: function () { return task; }, set: function (value) { extendObject(task, value); } }); } catch (err) { vox.task = task; } // 设置task方法 setTaskFuncs.call(this, { loadAudioProgress: function (url, state, currentTime, duration) { core.dispatch("loadAudioProgress", url, state, currentTime, duration); }, playAudioProgress: function (url, state, currentTime, duration) { core.dispatch("playAudioProgress", url, state, currentTime, duration); }, uploadPhotoCallback: function (photoId, pictures) { core.dispatch("uploadPhotoCallback", photoId, pictures); }, uploadVoiceCallback: function (recordId, records) { core.dispatch("uploadVoiceCallback", recordId, records); }, pauseHTML: function (isPaused) { core.dispatch("pauseHTML", isPaused); }, refreshData: function (json) { core.dispatch("refreshData", json); }, setTopBarInfoCallBack: function (json) { core.dispatch("setTopBarInfoCallBack", json); }, shareMethodClickCallBack: function (json) { core.dispatch("shareMethodClickCallBack", json); }, shareMethodCallBack: function (json) { core.dispatch("shareMethodCallBack", json); }, datetimeCallback: function (timestamp) { core.dispatch("datetimeCallback", timestamp); }, commentMethodCallback: function (json) { core.dispatch("commentMethodCallback", json); }, selectSchoolCallback: function (json) { core.dispatch("selectSchoolCallback", json); }, selectDataWidgeCallback: function (json) { core.dispatch("selectDataWidgeCallback", json); }, loadNativeADCompleteCallback: function (json) { core.dispatch("loadNativeADCompleteCallback", json); }, setLocation: function (json) { core.dispatch("setLocation", json); }, selectRegionCallback: function (json) { core.dispatch("selectRegionCallback", json); }, refreshPage: function (params) { core.dispatch("refreshPage", params); }, updateUserAvatarCallback: function (json) { core.dispatch("updateUserAvatarCallback", json); }, scoreComplete: function (json) { core.dispatch("scoreComplete", json); }, onScoreComplete: function (json) { core.dispatch("onScoreComplete", json); }, onPlaybackComplete: function (json) { core.dispatch("onPlaybackComplete", json); }, pageUnload: function (json) { core.dispatch("pageUnload", json); }, pageResumeShow: function (json) { core.dispatch("pageResumeShow", json); } }); // 同时存在vox.task和trigger机制,trigger优先执行 // 如果两种方式都监听,会执行两次 // 如果trigger首次执行后,证明此版本支持trigger,则不走vox.task回调 var isTrigger = false; function setTaskFunc(name, callback) { try { var callbacks = [callback]; Object.defineProperty(task, name, { get: function () { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } // 判断trigger if (isTrigger) { return; } // 遍历回调 for (var _a = 0, callbacks_1 = callbacks; _a < callbacks_1.length; _a++) { var func = callbacks_1[_a]; func.apply(this, args); } }; }, set: function (value) { // 添加回调,这里只能添加,无法删除 if (callbacks.indexOf(value) < 0) { callbacks.push(value); } } }); // 添加trigger,据说优先trigger执行 this.bindTrigger(name, function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } isTrigger = true; callback.apply(void 0, args); }, false); } catch (err) { task[name] = callback; } } function setTaskFuncs(values) { var _loop_1 = function () { var cb = values[key]; var packCallback = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } cb.call.apply(cb, [this].concat(args)); }; setTaskFunc.call(this_1, key, packCallback); }; var this_1 = this; for (var key in values) { _loop_1(); } } }; /*************************** 下面是本地存储接口 ***************************/ /** * 获取本地存储 * * @param {string} key 要获取值的键 * @returns {string} 获取的值 * @memberof Shell17Mobile */ Shell17Mobile.prototype.localStorageGet = function (key) { var _this = this; return new Promise(function (resolve) { _this.callExternal("localStorageGet", JSON.stringify({ category: environment17.env + "." + environment17.app, key: key })).then(function (data) { if (typeof data === 'string') { data = JSON.parse(data); } resolve(data && data.value); }); }); }; /** * 设置本地存储 * * @param {string} key 要设置的键 * @param {string} value 要设置的值 * @memberof Shell17Mobile */ Shell17Mobile.prototype.localStorageSet = function (key, value) { this.callExternal("localStorageSet", JSON.stringify({ category: environment17.env + "." + environment17.app, key: key, value: value })); }; /** * 移除本地存储 * * @param {string} key 要移除的键 * @memberof Shell17Mobile */ Shell17Mobile.prototype.localStorageRemove = function (key) { this.callExternal("localStorageRemove", JSON.stringify({ category: environment17.env + "." + environment17.app, key: key })); }; /** * 清空本地存储 * * @memberof Shell17Mobile */ Shell17Mobile.prototype.localStorageClear = function () { this.callExternal("localStorageClear", JSON.stringify({ category: environment17.env + "." + environment17.app })); }; /*************************** 下面是音频接口 ***************************/ Shell17Mobile.prototype.initAudio = function () { // 一起作业外壳环境下的音频实现都使用外壳音频 var audio = new Audio17MobileImpl(); // 注册Sound类型音频实现 audioManager.registerSoundImpl(audio); // 注册Music类型音频实现 audioManager.registerMusicImpl(audio); }; /** * 开始下载音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17Mobile.prototype.loadAudio = function (url) { this.callExternal("loadAudio", url); }; /** * 开始播放音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17Mobile.prototype.playAudio = function (url) { this.callExternal("playAudio", url); }; /** * 暂停播放音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17Mobile.prototype.pauseAudio = function (url) { this.callExternal("pauseAudio", url); }; /** * 音频跳转到指定位置 * * @param {string} url 音频地址 * @param {number} time 跳转位置(毫秒) * @memberof Shell17Mobile */ Shell17Mobile.prototype.seekAudio = function (url, time) { this.callExternal("seekAudio", url, time * 0.001); }; /** * 停止播放音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17Mobile.prototype.stopAudio = function (url) { this.callExternal("stopAudio", url); }; /*************************** 下面是视频接口 ***************************/ Shell17Mobile.prototype.playerVideo = function (params) { this.callExternal("playerVideo", JSON.stringify(params)); }; /*************************** 下面是一起作业移动版专属接口 ***************************/ /** * 移动端壳扩展方法 * @param funcName * @param paramsList */ Shell17Mobile.prototype.callExternal = function (funcName) { var paramsList = []; for (var _i = 1; _i < arguments.length; _i++) { paramsList[_i - 1] = arguments[_i]; } var func = window.external && window.external[funcName]; var result; if (typeof func === "function") { result = func.apply(window.external, paramsList); } return new Promise(function (resolve) { resolve(result); }); }; /** * 获取初始化信息 * @param refresh */ Shell17Mobile.prototype.getInitParams = function (refresh) { var _this = this; return new Promise(function (resolve, reject) { if (refresh || _this._initParams == null) { _this.callExternal("getInitParams").then(function (result) { _this._initParams = _this.mergeInitParams(result); resolve(_this._initParams); }); } else { resolve(_this._initParams); } }); }; Shell17Mobile.prototype.innerJump = function (name) { this.callExternal("innerJump", JSON.stringify({ name: name })); }; Shell17Mobile.prototype.homeworkHTMLLoaded = function () { this.callExternal("homeworkHTMLLoaded"); }; /** * 更新标题栏 * @param titile 文本内容 * @param txtColor 文字颜色代码(6位hex string,如"ffff00") * @param bgColor 背景颜色代码(hex string) */ Shell17Mobile.prototype.updateTitle = function (titile, txtColor, bgColor) { this.callExternal("updateTitle", titile, (txtColor && txtColor.toString(16)), (bgColor && bgColor.toString(16))); }; Shell17Mobile.prototype.redirectToLogin = function (from) { this.callExternal("redirectLogin", from || ""); }; Shell17Mobile.prototype.openFairylandPage = function (params) { var temp = params; if (temp.initParams != null) temp.initParams = stringify(params.initParams); this.callExternal("openFairylandPage", JSON.stringify(temp)); }; Shell17Mobile.prototype.pageQueueNew = function (params) { var temp = params; if (temp.initParams != null) temp.initParams = stringify(params.initParams); this.callExternal("pageQueueNew", JSON.stringify(temp)); }; Shell17Mobile.prototype.pageQueueBack = function (params) { var temp = params; if (temp.initParams != null) temp.initParams = stringify(params.initParams); this.callExternal("pageQueueBack", JSON.stringify(temp)); }; Shell17Mobile.prototype.pageQueueRefresh = function (params) { var temp = params || {}; // 移动端必须传url,否则不响应 if (temp.url == null) temp.url = window.location.href; // 将initParams字符串化 if (temp.initParams != null) temp.initParams = stringify(params.initParams); this.callExternal("pageQueueRefresh", JSON.stringify(temp)); }; Shell17Mobile.prototype.pageQueueQuit = function (params) { this.callExternal("pageQueueQuit", JSON.stringify(params)); }; Shell17Mobile.prototype.disMissView = function () { this.callExternal("disMissView"); }; Shell17Mobile.prototype.showlog = function (msg) { // 调用外壳接口 this.callExternal("showlog", msg); }; Shell17Mobile.prototype.log_b = function (data) { if (data == null) return; // 调用外壳接口 this.callExternal("log_b", "", JSON.stringify(data)); }; Shell17Mobile.prototype.payOrder = function (params) { this.callExternal("payOrder", JSON.stringify(params)); }; Shell17Mobile.prototype.startRecord = function (params) { this.callExternal("oralStartRecord", JSON.stringify(params)); //后两个值是张羽定的 }; Shell17Mobile.prototype.stopRecord = function (params) { this.callExternal("oralStopRecord", JSON.stringify(params)); }; Shell17Mobile.prototype.startPlayback = function (params) { this.callExternal("oralStartPlayback", JSON.stringify(params)); }; Shell17Mobile.prototype.stopPlayback = function (params) { this.callExternal("oralStopPlayback", JSON.stringify(params)); }; /** * 显示toast弹窗 * @param msg 显示的信息,默认"" */ Shell17Mobile.prototype.showToast = function (msg) { // 调用外壳接口 this.callExternal("showToast", msg); }; /** * 设置音频音量大小 * @param url 音频地址 * @param volume 音量大小(0.0-1.0) */ Shell17Mobile.prototype.setAudioVolume = function (url, volume) { // 调用外壳接口 this.callExternal("setAudioVolume", url, volume); }; /** * 设置webview顶部栏信息 * @param params */ Shell17Mobile.prototype.setTopBarInfo = function (params) { this.callExternal("setTopBarInfo", JSON.stringify(params)); }; return Shell17Mobile; }(Shell17)); export default Shell17Mobile;