mtl-js-sdk
Version:
704 lines (638 loc) • 19.8 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; }
define(["axios", "qrious"], function (axios, QRious) {
var baseUrl = 'https://build.yyuap.com';
var DEFAULT_STORAGE_DOMAIN = "domain.default";
var SUCCESS_CODE = 200;
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 = ["dail", "voiceToText", "changeScreenOrientation", "restoreScreenOrientation", "uploadFile", "downloadFile", "isWebviewCanGoBack", "onWebviewGoBack", "startSpeechSyn", "stopSpeechSyn", "showToast", "openShare", "closeScanQRCode", "backIntercept", "getAppInfo", "openSchema", "getConfig"];
function generateQRCode(object) {
if (!!!object.str) {
var _res = {
code: -1,
message: "str is null"
};
object.fail && object.fail(_res);
object.complete && object.complete(_res);
return;
}
var qr = new QRious({
value: object.str,
size: object.size || 100
});
var imgSrc = qr.toDataURL("image/jpeg");
var res = {
imgSrc: imgSrc
};
object.success && object.success(res);
object.complete && object.complete(res);
}
function getLocalImgSrc(object) {
var localId = object.localId;
mtl.getLocalImgData({
localId: localId,
complete: function complete(res) {
var code = res.code,
message = res.message,
data = res.data;
if (code === SUCCESS_CODE) {
message = "getLocalImgSrc:ok!";
data = {
imgSrc: data.localData
};
object.success && object.success(data);
} else {
object.fail && object.fail({
code: code,
message: message
});
}
object.complete && object.complete({
code: code,
message: message,
data: data
});
}
});
}
function scanInvoice(object) {
mtl.chooseImage({
count: 1,
// 默认9
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
success: function success(res) {
var localIds = res.localIds;
mtl.recognizeInvoice(_objectSpread({}, object, {
image: localIds[0]
}));
},
fail: object.fail
});
}
function recognizeInvoice(object) {
var appCode = object.appCode,
image = object.image,
_object$url = object.url,
url = _object$url === void 0 ? baseUrl : _object$url;
new Promise(function (resolve, reject) {
if (image.startsWith("wxLocalResource://") || image.startsWith("weixin://")) {
mtl.getLocalImgData({
localId: image,
success: resolve,
fail: reject
});
} else {
resolve({
localData: image
});
}
}).then(function (res) {
var imgBase64 = res.localData;
var index = imgBase64.indexOf("base64,");
if (index !== -1) {
imgBase64 = imgBase64.substring(index + 7, imgBase64.count);
}
var path = "".concat(url, "/mobile-app/rest/v1/api/apilink/ocr/invoice/vat/base64");
var params = {
image: imgBase64,
apiCode: appCode
};
return axios({
method: "post",
url: path,
params: null,
data: params,
headers: {
"Content-Type": "application/json;charset=UTF-8"
}
});
}).then(function (res) {
object.success && object.success(res.data);
object.complete && object.complete(res);
})["catch"](function (err) {
var result = {
code: FAIL_CODE,
message: err.toString()
};
object.fail && object.fail(result);
object.complete && object.complete(result);
});
}
function scanIDCard(object) {
mtl.chooseImage({
count: 1,
// 默认9
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
success: function success(res) {
console.log('scanIDCard success', res);
var localIds = res.localIds;
mtl.recognizeIDCard(_objectSpread({}, object, {
image: localIds[0]
}));
},
fail: object.fail
});
}
function recognizeIDCard(object) {
var appCode = object.appCode,
side = object.side,
image = object.image,
_object$url2 = object.url,
url = _object$url2 === void 0 ? baseUrl : _object$url2;
new Promise(function (resolve, reject) {
if (image.startsWith("wxLocalResource://") || image.startsWith("weixin://")) {
mtl.getLocalImgData({
localId: image,
success: resolve,
fail: reject
});
} else {
resolve({
localData: image
});
}
}).then(function (res) {
var imgBase64 = res.localData;
var index = imgBase64.indexOf("base64,");
if (index !== -1) {
imgBase64 = imgBase64.substring(index + 7, imgBase64.count);
}
var path = "".concat(url, "/mobile-app/rest/v1/api/apilink/ocr/card/id/base64");
var params = {
image: imgBase64,
apiCode: appCode,
isFront: side === 'back' ? false : true
};
return axios({
method: "post",
url: path,
params: null,
data: params,
headers: {
"Content-Type": "application/json;charset=UTF-8"
}
});
}).then(function (res) {
object.success && object.success(res.data);
object.complete && object.complete(res);
})["catch"](function (err) {
var result = {
code: FAIL_CODE,
message: err.toString()
};
object.fail && object.fail(result);
object.complete && object.complete(result);
});
}
function scanBankCard(object) {
mtl.chooseImage({
count: 1,
// 默认9
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
success: function success(res) {
var localIds = res.localIds;
mtl.recognizeBankCard(_objectSpread({}, object, {
image: localIds[0]
}));
},
fail: object.fail
});
}
function recognizeBankCard(object) {
var appCode = object.appCode,
image = object.image,
_object$url3 = object.url,
url = _object$url3 === void 0 ? baseUrl : _object$url3;
new Promise(function (resolve, reject) {
if (image.startsWith("wxLocalResource://") || image.startsWith("weixin://")) {
mtl.getLocalImgData({
localId: image,
success: resolve,
fail: reject
});
} else {
resolve({
localData: image
});
}
}).then(function (res) {
var imgBase64 = res.localData;
var index = imgBase64.indexOf("base64,");
if (index !== -1) {
imgBase64 = imgBase64.substring(index + 7, imgBase64.count);
}
var path = "".concat(url, "/mobile-app/rest/v1/api/apilink/ocr/card/bank/base64");
var params = {
image: imgBase64,
apiCode: appCode
};
return axios({
method: "post",
url: path,
params: null,
data: params,
headers: {
"Content-Type": "application/json;charset=UTF-8"
}
});
}).then(function (res) {
object.success && object.success(res.data);
object.complete && object.complete(res);
})["catch"](function (err) {
var result = {
code: FAIL_CODE,
message: err.toString()
};
object.fail && object.fail(result);
object.complete && object.complete(result);
});
}
function request(object) {
var url = object.url,
_object$method = object.method,
method = _object$method === void 0 ? 'GET' : _object$method,
_object$headers = object.headers,
headers = _object$headers === void 0 ? {
'content-type': 'application/json'
} : _object$headers,
params = object.params;
var obj = {
url: url,
method: method,
headers: headers,
params: method.toUpperCase() == 'GET' ? params : {},
data: method.toUpperCase() == 'POST' ? params : {},
timeout: 3 * 1000,
responseType: 'json'
};
axios(obj).then(function (response) {
var code = response.status,
message = response.statusText,
data = response.data;
if (code === 200) {
object.success && object.success(data);
} else {
object.fail && object.fail({
code: code,
message: message,
data: data
});
}
object.complete && object.complete({
code: code,
message: message,
data: data
});
})["catch"](function (err) {
var result = {
code: FAIL_CODE,
message: err.message
};
object.fail && object.fail(result);
object.complete && object.complete(result);
});
}
function setStorage(obj) {
var _obj$domain = obj.domain,
domain = _obj$domain === void 0 ? DEFAULT_STORAGE_DOMAIN : _obj$domain,
key = obj.key,
data = obj.data;
if (!!!key || !!!data) {
var error = {
code: FAIL_CODE,
message: "".concat(!!key ? 'data' : 'key', " is null")
};
obj.fail && obj.fail(error);
obj.complete && obj.complete(error);
return;
}
if (typeof key != "string") {
throw new TypeError("key is not a string");
}
var structs = localStorage.getItem(domain);
structs = structs && JSON.parse(structs) || {};
if (data) {
structs[key] = data;
} else {
delete structs[key];
}
localStorage.setItem(domain, JSON.stringify(structs));
obj.success && obj.success();
obj.complete && obj.complete({
code: SUCCESS_CODE,
message: "setStorage:ok"
});
}
function getStorage(obj) {
var res = null;
var data = null;
var _obj$domain2 = obj.domain,
domain = _obj$domain2 === void 0 ? DEFAULT_STORAGE_DOMAIN : _obj$domain2,
key = obj.key;
if (!!!key) {
var error = {
code: FAIL_CODE,
message: "key is null."
};
obj.fail && obj.fail(error);
obj.complete && obj.complete(error);
return;
}
if (typeof key != "string") {
res = {
code: FAIL_CODE,
message: new TypeError("key is not a string").toString()
};
obj.fail && obj.fail(res);
} else {
var structs = localStorage.getItem(domain);
structs = structs && JSON.parse(structs);
data = structs && structs.hasOwnProperty(key) && structs[key] || null;
if (!!data) {
res = {
code: SUCCESS_CODE,
message: "getStorage:ok",
data: {
data: data
}
};
obj.success && obj.success(res.data);
} else {
res = {
code: FAIL_CODE,
message: new TypeError("data is null").toString()
};
obj.fail && obj.fail(res);
}
}
obj.complete && obj.complete(res);
return data;
}
function removeStorage() {
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _obj$domain3 = obj.domain,
domain = _obj$domain3 === void 0 ? DEFAULT_STORAGE_DOMAIN : _obj$domain3,
key = obj.key;
var data = null;
if (!!!key) {
var error = {
code: FAIL_CODE,
message: "key is null"
};
obj.fail && obj.fail(error);
obj.complete && obj.complete(error);
return;
}
if (typeof key != "string") {
throw new TypeError("key is not a string");
}
var structs = localStorage.getItem(domain);
structs = structs && JSON.parse(structs) || {};
if (data) {
structs[key] = data;
} else {
delete structs[key];
}
localStorage.setItem(domain, JSON.stringify(structs));
obj.success && obj.success();
obj.complete && obj.complete({
code: SUCCESS_CODE,
message: "removeStorage:ok"
});
}
function clearStorage(obj) {
var domain = obj && obj.domain || DEFAULT_STORAGE_DOMAIN;
localStorage.removeItem(domain);
obj.success && obj.success();
obj.complete && obj.complete({
code: SUCCESS_CODE,
message: "clearStorage:ok"
});
}
function getAppCode(obj) {
var urlSuffix = window.location.search;
if (urlSuffix) {
var query = window.location.search.substring(1);
var vars = query.split("&");
var result = vars.find(function (element) {
return element.indexOf("appCode") != -1;
});
if (result) {
var data = {
code: 200,
message: "",
data: {
appCode: result.substring(result.lastIndexOf("=") + 1)
}
};
obj.complete && obj.complete(data);
obj.success && obj.success(data.data);
} else {
var _data = {
code: -1,
message: "appCode 获取失败"
};
obj.fail && obj.fail(_data);
obj.complete && obj.complete(_data);
}
} else {
throw "appCode is null";
}
}
function getOAuthCode() {
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var url = obj.url,
accessToken = obj.accessToken,
tenantId = obj.tenantId;
if (!!!url || !!!tenantId) {
var error = {
code: FAIL_CODE,
message: "".concat(url ? 'tenantId' : 'url', "is null")
};
obj.fail && obj.fail(error);
obj.complete && obj.complete(error);
} else {
mtl.yht.getUCGAuthCode(obj);
}
}
function getAuthCode(obj) {
var urlSuffix = window.location.search;
if (urlSuffix) {
var query = window.location.search.substring(1);
var vars = query.split("&");
var result = vars.find(function (element) {
return element.indexOf("authCode") != -1;
});
if (result) {
var data = {
code: 200,
message: "",
data: {
authCode: result.substring(result.lastIndexOf("=") + 1)
}
};
obj.complete && obj.complete(data);
obj.success && obj.success(data.data);
} else {
var _data2 = {
code: -1,
message: "authCode 获取失败"
};
obj.fail && obj.fail(_data2);
obj.complete && obj.complete(_data2);
}
return;
} else {
throw "authCode is null";
}
}
function getUserInfo(obj) {
var urlSuffix = window.location.search;
if (urlSuffix) {
var query = window.location.search.substring(1);
var vars = query.split("&");
var result = vars.find(function (element) {
return element.indexOf("userInfo") != -1;
});
if (result && result.substring(result.lastIndexOf("=") + 1)) {
var userInfo = result.substring(result.lastIndexOf("=") + 1);
var data = {
code: 200,
message: "",
data: {
userInfo: JSON.parse(decodeURI(userInfo))
}
};
obj.complete && obj.complete(data);
obj.success && obj.success(data.data);
} else {
var _data3 = {
code: -1,
message: "userInfo 获取失败"
};
obj.complete && obj.complete(_data3);
obj.fail && obj.fail(_data3);
}
return;
} else {
throw "userInfo is null";
}
}
function getMac(obj) {
if (obj) {
mtl.yht.getOpenId({
onsuccess: function onsuccess(res) {
var openId = res.data;
obj.success && obj.success({
macAddress: openId
});
},
fail: function fail(error) {
obj.fail && obj.fail(error);
},
complete: function complete(res) {
obj.complete && obj.complete(res);
}
});
} else {
console.log('obj is null in getMac API.');
}
}
function weChatBindByPassword(obj) {
var path = object.url ? object.url : baseUrl;
obj.url = "".concat(path, "/mobile-app/rest/v1/mobile/user/weChatBind");
obj.method = 'POST';
var data = {
username: obj.username,
password: obj.password,
appId: obj.appId,
openId: obj.openId,
validateCodeType: 'username'
};
obj.params = data;
request(obj);
}
function weChatBindByValidateCode(obj) {
var path = object.url ? object.url : baseUrl;
obj.url = "".concat(path, "/mobile-app/rest/v1/mobile/user/weChatBind");
obj.method = 'POST';
var data = {
username: obj.username,
validateCode: obj.validateCode,
appId: obj.appId,
openId: obj.openId,
validateCodeType: 'mobile'
};
obj.params = data;
request(obj);
}
function weChatUnBind(obj) {
var path = object.url ? object.url : baseUrl;
obj.url = "".concat(path, "/mobile-app/rest/v1/mobile/user/weChatUnbind");
obj.method = 'POST';
var data = {
yhtToken: obj.yhtToken
};
obj.params = data;
request(obj);
}
function doShare(obj) {
var urlEncode = encodeURIComponent(obj.link);
var title = obj.title ? obj.title : '';
var url = "/pages/home/home?title=".concat(title, "&url=").concat(urlEncode);
if (obj.type == 9) {
url = "/pages/mtlShare/mtlShare?title=".concat(title, "&url=").concat(urlEncode);
}
mtl.navigateTo({
url: url,
success: function success(res) {
obj.success && obj.success(res);
},
fail: function fail(err) {
obj.fail && obj.fail(err);
}
});
}
var exports = {
generateQRCode: generateQRCode,
getLocalImgSrc: getLocalImgSrc,
scanInvoice: scanInvoice,
recognizeInvoice: recognizeInvoice,
scanIDCard: scanIDCard,
recognizeIDCard: recognizeIDCard,
scanBankCard: scanBankCard,
recognizeBankCard: recognizeBankCard,
request: request,
setStorage: setStorage,
getStorage: getStorage,
removeStorage: removeStorage,
clearStorage: clearStorage,
getAppCode: getAppCode,
getAuthCode: getAuthCode,
getUserInfo: getUserInfo,
getMac: getMac,
getOAuthCode: getOAuthCode,
weChatBindByPassword: weChatBindByPassword,
weChatBindByValidateCode: weChatBindByValidateCode,
weChatUnBind: weChatUnBind,
doShare: doShare
};
unsupportMethods.forEach(function (pop) {
exports[pop] = unsupportMethod;
});
return exports;
});