mtl-js-sdk
Version:
218 lines (184 loc) • 7.74 kB
JavaScript
;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
define([], function () {
var __NCC = "NCC";
var FAIL_CODE = 1;
var unsupportFailRes = {
code: FAIL_CODE,
message: "当前平台不支持"
};
function unsupportMethod() {
var object = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
object.fail && object.fail(unsupportFailRes);
object.complete && object.complete(unsupportFailRes);
}
var unsupportMethods = ["stopBarcodeService", "startBarcodeScan", "stopBarcodeScan", "controlBarcodeScan"]; // 0是默认,1全界面展示webview
function relayoutCustomWebview(object) {
var _obj = object || {};
var _obj$type = _obj.type,
type = _obj$type === void 0 ? 0 : _obj$type;
var data = Object.assign({
type: type
}, handleResponse(_obj));
execEsnBridge("relayoutCustomWebview", data);
}
function customScanQRCode(object) {
var _obj = object || {};
var _obj$type2 = _obj.type,
type = _obj$type2 === void 0 ? 1 : _obj$type2,
url = _obj.url,
callback = _obj.callback,
_obj$scanLeftRatio = _obj.scanLeftRatio,
scanLeftRatio = _obj$scanLeftRatio === void 0 ? 0.25 : _obj$scanLeftRatio,
_obj$scanTopRatio = _obj.scanTopRatio,
scanTopRatio = _obj$scanTopRatio === void 0 ? 0.1 : _obj$scanTopRatio,
_obj$scanWidthRatio = _obj.scanWidthRatio,
scanWidthRatio = _obj$scanWidthRatio === void 0 ? 0.5 : _obj$scanWidthRatio,
_obj$scanHeightRatio = _obj.scanHeightRatio,
scanHeightRatio = _obj$scanHeightRatio === void 0 ? 0.2 : _obj$scanHeightRatio,
_obj$isHiddenLight = _obj.isHiddenLight,
isHiddenLight = _obj$isHiddenLight === void 0 ? 0 : _obj$isHiddenLight,
customNavi = _obj.customNavi,
leftItems = _obj.leftItems,
rightItems = _obj.rightItems,
centerItems = _obj.centerItems,
scanViewCloseCallback = _obj.scanViewCloseCallback;
if (!url || !callback || scanWidthRatio === 0) {
var err = {
code: FAIL_CODE,
message: scanWidthRatio === 0 ? 'scanWidthRatio is zero' : "".concat(!url ? "url" : "callback", " is null")
};
_obj.fail && object.fail(err);
_obj.complete && object.complete(err);
return;
}
var data = Object.assign({
type: type,
url: url,
callback: callback,
scanLeftRatio: scanLeftRatio,
scanTopRatio: scanTopRatio,
scanWidthRatio: scanWidthRatio,
scanHeightRatio: scanHeightRatio,
isHiddenLight: isHiddenLight,
customNavi: customNavi,
leftItems: leftItems,
rightItems: rightItems,
centerItems: centerItems,
scanViewCloseCallback: scanViewCloseCallback
}, handleResponse(_obj));
execEsnBridge("customScanQRCode", data);
}
function registerCallback() {
var object = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var obj = {};
var mtlRegisterCallbackName = object.mtlRegisterCallbackName,
registerCallback = object.registerCallback;
obj[mtlRegisterCallbackName] = function (data, callback) {
var backData = data;
if (typeof data == 'string') {
backData = JSON.parse(data);
}
if (_typeof(backData) == 'object') {
var index = Object.keys(backData).findIndex(function (item) {
return item == 'qrString';
});
if (index >= 0) {
backData.resultStr = backData.qrString;
delete backData.qrString;
}
}
registerCallback(backData, callback);
};
YYEsnBridge.ready(function () {
YYEsnBridge.registerHandler(obj);
});
}
function execEsnBridge(method, source) {
var platform = mtl.platform;
var __start = platform === 'QYAndroid' || platform === 'QYIos' ? '' : source.closeNcc ? "" : __NCC;
var data = _objectSpread({}, source, {
fail: function fail(error) {
console.log('error:', error);
source.fail(getErrormsg(method, error, __start));
}
});
console.log("exec YYEsnBridge.do method :", __start + method);
console.log("data: ", data);
YYEsnBridge["do"](__start + method, data);
}
/**
* 统一处理API的回调
* @param {*} object
* @param {*} callback
*/
function handleResponse(object, callback) {
return {
success: function success(res) {
var data = callback && callback(res) || res;
object.success && object.success(data);
object.complete && object.complete({
code: 200,
data: data
});
},
fail: function fail(res) {
console.log("handleResponse:fail", res);
object.fail && object.fail(res);
object.complete && object.complete(res);
}
};
}
/**
* 400 参数不合法
401 用户无权限访问
405 APP无权限进行此项操作,如打开照相机,APP未取得授权
406 文件大小超出限制
500 http请求错误
501 http服务器错误
1001 无网络
1002 解析错误
1003 用户取消
-1 未知错误
-2 界面超时
-1000 未找到对应方法
* @param {*} error
*/
function getErrormsg(method, error, __start) {
var esnError = {
code: -1,
message: errmsg
};
if (_typeof(error) === "object") {
var code = error && error.errCode;
var msg = "";
if (code == 1003) {
msg = "用户取消";
} else if (code == -1000) {
msg = "未找到对应方法";
} else if (code == 405) {
msg = "APP无权限进行此项操作,如打开照相机,APP未取得授权";
} else if (code == 401) {
msg = "用户无权限访问";
}
esnError.code = code;
esnError.message = msg || JSON.stringify(error);
}
var errmsg = "\u8C03\u7528\u53CB\u7A7A\u95F4 ".concat(__start + method, " \u65B9\u6CD5,\u8FD4\u56DE\u9519\u8BEF\u4FE1\u606F\uFF1A ").concat(esnError.message);
esnError.message = errmsg;
console.error(errmsg);
return esnError;
}
var exports = {
customScanQRCode: customScanQRCode,
registerCallback: registerCallback,
relayoutCustomWebview: relayoutCustomWebview
};
unsupportMethods.forEach(function (pop) {
exports[pop] = unsupportMethod;
});
return exports;
});