UNPKG

mtl-js-sdk

Version:

ynf-fw-mtl-api

263 lines (249 loc) 7.56 kB
"use strict"; function Plugin(options, exports = {}) { const { platform } = options || { platform: window.mtl.platform }; const upesnVersion = window.mtl.upesnVersion || 0 const FAIL_CODE = 1; const __NCC = "NCC"; /** 内部实现 start*/ const unsupportFailRes = { code: FAIL_CODE, message: "The current platform does not support" }; function unsupportMethod(object = {}) { object.fail && object.fail(unsupportFailRes); object.complete && object.complete(unsupportFailRes); } function invokeSuccess(object, ret) { object.success && object.success(ret); object.complete && object.complete(ret); } function invokeFail(object, err) { if (err) { if (!err.message) { err.message = err.msg || err.errMsg || err.errDescription; } if (typeof err.code == 'undefined') { err.code = err.errCode; } } object.fail && object.fail(err); object.complete && object.complete(err); } function handleMessage(handlerName, data, responseCallback) { window.MTLWebViewJavascriptBridge.handleMessage(handlerName, data, responseCallback); } /** 内部实现 end*/ function settingNavBar(object={}) { if ( platform === "upesn" && upesnVersion <= 0) { let _obj = object || {}; let data = { hide: _obj.hide, backgroundColor: _obj.backgroundColor, hideShadowImage: _obj.hideShadowImage, statusBarStyle: _obj.statusBarStyle, fullScreen: _obj.fullScreen, leftItems: _obj.leftItems, centerItems: _obj.centerItems, rightItems: _obj.rightItems, success: function(res) { _obj.success && object.success(res); }, fail: function(res) { _obj.fail && object.fail(res); }, complete: function(res) { _obj.complete && object.complete(res); } }; execEsnBridge("settingNavBar", data); } else { mtl.settingNavBar(object) } } function backIntercept(object={}) { if ( platform === "upesn" && upesnVersion <= 0) { let _obj = object || {}; let data = { callback: _obj.callback, success: function(res) { _obj.success && object.success(res); }, fail: function(res) { _obj.fail && object.fail(res); }, complete: function(res) { _obj.complete && object.complete(res); } }; execEsnBridge("backIntercept", data); } else { mtl.backIntercept(object) } } function changeScreenOrientation(object={}) { if ( platform === "upesn" && upesnVersion <= 0) { let _obj = object || {}; let data = { orientation: _obj.orientation, success: function(res) { _obj.success && object.success(res); }, fail: function(res) { _obj.fail && object.fail(res); }, complete: function(res) { _obj.complete && object.complete(res); } }; execEsnBridge("changeScreenOrientation", data); } else { mtl.changeScreenOrientation(object) } } function downloadFile(object={}) { if ( platform === "upesn" && upesnVersion <= 0) { let _obj = object || {}; let data = { url: _obj.url, success: function(res) { let returnRes = { localId: res.filePath, ...res }; _obj.success && object.success(returnRes); }, fail: function(res) { _obj.fail && object.fail(res); }, complete: function(res) { _obj.complete && object.complete(res); } }; execEsnBridge("downloadFile", data); } else { mtl.downloadFile(object) } } function openNewWebview(object={}) { if ( platform === "upesn" && upesnVersion <= 0) { let _obj = object || {}; let data = { url: _obj.url, orientation: _obj.orientation, navShow:_obj.navShow, success: function(res) { _obj.success && object.success(res); }, fail: function(res) { _obj.fail && object.fail(res); }, complete: function(res) { _obj.complete && object.complete(res); } }; execEsnBridge("openNewWebview", data); } else { mtl.openNewWebview(object) } } function startPDAScan(object={}) { if ( platform === "upesn" && upesnVersion <= 0) { let _obj = object || {}; let data = { callback: object.callback, success: function(res) { _obj.success && object.success(res); }, fail: function(res) { _obj.fail && object.fail(res); }, complete: function(res) { _obj.complete && object.complete(res); } }; execEsnBridge("startPDAScan", data); } else if (platform == "APIAndroid" || (platform == 'upesn' && upesnVersion > 0 && upesnVersion % 2 == 0)) { mtl.startPDAScan(object) } else { unsupportMethod(object) } } function stopPDAScan(object={}) { if ( platform === "upesn" && upesnVersion <= 0) { let _obj = object || {}; let data = { success: function(res) { _obj.success && object.success(res); }, fail: function(res) { _obj.fail && object.fail(res); }, complete: function(res) { _obj.complete && object.complete(res); } }; execEsnBridge("stopPDAScan", data); } else if (platform == "APIAndroid" || (platform == 'upesn' && upesnVersion > 0 && upesnVersion % 2 == 0)) { mtl.stopPDAScan(object) } else { unsupportMethod(object) } } /** 老版本处理 */ function execEsnBridge(method, source) { let __start = source.closeNcc ? "" : __NCC; let data = { ...source, fail: function(error) { source.fail(_getErrormsg(method, error, __start)); } }; console.log("exec YYEsnBridge.do method :", __start + method); console.log("data: ", data); YYEsnBridge.do(__start + method, data); } /** * 400 参数不合法 401 用户无权限访问 405 APP无权限进行此项操作,如打开照相机,APP未取得授权 406 文件大小超出限制 500 http请求错误 501 http服务器错误 1001 无网络 1002 解析错误 1003 用户取消 -1 未知错误 -2 界面超时 -1000 未找到对应方法 * @param {*} error */ function _getErrormsg(method, error, __start) { let esnError = { code: -1, message: errmsg }; if (typeof error === "object") { let code = error.errCode; let msg = ""; if (code == 1003) { msg = "by user canceled"; } else if (code == -1000) { msg = "not found function"; } else if (code == 405) { msg = "Unauthorized and unauthorized to perform this operation"; } else if (code == 401) { msg = "User does not have permission to access"; } esnError.code = code; esnError.message = msg || JSON.stringify(error); } let errmsg = `YYZone api ${__start + method}, error message: ${ esnError.message }`; esnError.message = errmsg; (errmsg); return esnError; } exports.module = {settingNavBar, backIntercept, changeScreenOrientation, downloadFile, openNewWebview, startPDAScan, stopPDAScan}; exports.symbolPath = exports.symbolPath || "mobile"; exports.strategy = "m"; return exports; } export default Plugin;