@molejs/mole-jssdk
Version:
mole app jssdk
453 lines (400 loc) • 12.7 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.openTouchIDundefined = factory());
}(this, (function () { 'use strict';
function dingTalkPolyfill () {
return {
openNewPage: function openNewPage(_ref) {
var url = _ref.url;
window.dd.biz.util.openLink({
url: url });
},
get version() {
return window.dd.version;
}
};
}
var ua = window.navigator.userAgent;
function dynamicLoadJs(url, callback) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
if (typeof callback == 'function') {
script.onload = script.onreadystatechange = function () {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
callback();
script.onload = script.onreadystatechange = null;
}
};
}
head.appendChild(script);
}
var platform = {
get iPhoneX() {
return (/iphone/gi.test(ua) && screen.height == 812 && screen.width == 375
);
},
get iPhoneXR() {
return (/iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896
);
},
get iPhoneXSMax() {
return (/iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896
);
},
get os() {
if (ua.match(/(Android);?[\s\/]+([\d.]+)?/)) {
return 'android';
} var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
if (ipad || iphone || ipod) return 'ios';
return '';
},
get container() {
if (ua.toLowerCase().match(/upaasapp/i)) return 'italent';
if (ua.toLowerCase().match(/tita/i)) return 'tita';
if (ua.match(/DingTalk/i)) return 'dingTalk';
if (ua.match(/MicroMessenger/i)) return 'wechart';
if (ua.match(/wxwork/i)) return 'wxwork';
return 'browser';
},
get OS() {
if (ua.match(/(Android);?[\s\/]+([\d.]+)?/)) {
return 'android';
} var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
if (ipad || iphone || ipod) return 'ios';
return '';
},
get ShellName() {
if (ua.toLowerCase().match(/upaasapp/i)) return 'italent';
if (ua.toLowerCase().match(/tita/i)) return 'tita';
if (ua.match(/DingTalk/i)) return 'dingTalk';
if (ua.match(/MicroMessenger/i)) return 'wechart';
if (ua.match(/wxwork/i)) return 'wxwork';
return 'browser';
},
get web() {
if (/mobile|android|iphone|ipad|phone/i.test(ua) && !platform.bsAppAndroid && !platform.bsAppIOS) {
return true;
}
return false;
},
get ios() {
return platform.os === 'ios';
},
get android() {
return platform.os === 'android';
},
get weixin() {
return !!ua.match(/MicroMessenger/i);
},
get weixinWork() {
return !!ua.match(/wxwork/i);
},
get bsAppIOS() {
return !!ua.toLowerCase().match(/beisen/i) && platform.ios && platform.apiCloud;
},
get beisenApp() {
return platform.bsAppIOS || platform.bsAppAndroid;
},
get bsAppAndroid() {
return !!ua.toLowerCase().match(/beisen/i) && platform.android;
},
get apiCloud() {
return !!ua.toLowerCase().match(/apicloud/i);
},
get titaApp() {
return !!ua.toLowerCase().match(/tita/i);
},
get titav2App() {
return !!ua.match(/Titav2/i);
},
get upaasApp() {
return !!ua.toLowerCase().match(/upaasapp/i);
},
get dingTalk() {
return !!ua.match(/DingTalk/i);
},
get bayer() {
return !!ua.match(/Bayer/i);
},
get iTalentIOS() {
return platform.ios && platform.apiCloud && platform.upaasApp;
},
get iTalentAndroid() {
return platform.android && platform.upaasApp;
},
get iTalentWeb() {
return false;
},
get api() {
return platform.bsAppIOS ? window.api || window.top.api || {} : window.BSJSBridge;
},
waitApi: function waitApi() {
var t = void 0;
return new Promise(function (reslove, reject) {
if (!!ua.match(/DingTalk/i)) {
return dynamicLoadJs('//g.alicdn.com/dingding/dingtalk-jsapi/2.3.0/dingtalk.open.js', function () {
window.api = dingTalkPolyfill(window.dd);
return reslove({});
});
}
if (platform.bsAppAndroid) {
if (window.BSJSBridge || window.parent.BSJSBridge) {
return reslove({
api: window.BSJSBridge || window.parent.BSJSBridge
});
}
t = setInterval(function () {
if (window.BSJSBridge || window.parent.BSJSBridge) {
clearInterval(t);
return reslove({
api: window.BSJSBridge || window.parent.BSJSBridge
});
}
}, 16);
} else if (platform.bsAppIOS) {
if (window.api || window.parent.api) {
return reslove({
api: window.api || window.parent.api
});
}
t = setInterval(function () {
if (window.api || window.parent.api) {
clearInterval(t);
return reslove({
api: window.api || window.parent.api
});
}
}, 16);
} else {
return reslove({});
}
});
},
waitApiV2: function waitApiV2() {
var t = void 0;
return new Promise(function (reslove, reject) {
if (platform.dingTalk) {
return dynamicLoadJs('//g.alicdn.com/dingding/dingtalk-jsapi/2.3.0/dingtalk.open.js', function () {
window.api = dingTalkPolyfill(window.dd);
return reslove({});
});
}
if (platform.bsAppAndroid) {
if (window.bsapi || window.parent.bsapi) {
return reslove({
api: window.bsapi || window.parent.bsapi
});
}
t = setInterval(function () {
if (window.bsapi || window.parent.bsapi) {
clearInterval(t);
return reslove({
api: window.bsapi || window.parent.bsapi
});
}
}, 16);
} else if (platform.bsAppIOS) {
if (window.api || window.parent.api) {
return reslove({
api: window.api || window.parent.api
});
}
t = setInterval(function () {
if (window.api || window.parent.api) {
clearInterval(t);
return reslove({
api: window.api || window.parent.api
});
}
}, 16);
}
});
},
select: function select() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var mobile = params.mobile,
androidApp = params.androidApp,
iosApp = params.iosApp,
beisenApp = params.beisenApp,
weixin = params.weixin,
weixinWork = params.weixinWork,
dingTalk = params.dingTalk;
var cb = void 0;
if (platform.bsAppIOS && iosApp) {
cb = iosApp;
} else if (platform.bsAppAndroid && androidApp) {
cb = androidApp;
} else if (platform.dingTalk && dingTalk) {
cb = dingTalk;
} else if (platform.weixin && weixin) {
cb = weixin;
} else if (platform.weixinWork && weixinWork) {
cb = weixinWork;
} else {
cb = mobile;
}
if (platform.beisenApp && beisenApp) {
cb = beisenApp;
}
if (typeof cb === 'function') {
return cb();
}
}
};
function getApi() {
return platform.select({
iosApp: function iosApp() {
return window.api || window.parent.api;
},
androidApp: function androidApp() {
return window.BSJSBridge || window.parent.BSJSBridge;
}
});
}
function notify() {
var param = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var cb = arguments[1];
getApi().callHandler('bs-droid-event', {
param: param
}, function (ret, err) {
return cb && cb(window.JSON.parse(ret), err);
});
}
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
function validateCode(Code) {
var reason = '';
var safeText = '';
if (platform.iPhoneX || platform.iPhoneXSMax || platform.iPhoneXR) {
safeText = '面容 ID';
} else {
safeText = '触控 ID';
}
if (platform.android) {
safeText = '指纹';
}
switch (Code) {
case -8:
reason = '\u6682\u4E0D\u53EF\u7528\uFF0C' + safeText + '\u5DF2\u7ECF\u88AB\u9501\u5B9A';
break;
case 3:
reason = '错误次数过多,请稍后';
break;
case 2:
reason = '指纹不匹配';
break;
case -5:
case -6:
reason = '\u6682\u4E0D\u53EF\u7528\uFF0C\u8BF7\u68C0\u67E5' + safeText + '\u8BBE\u7F6E';
break;
case -7:
reason = '\u60A8\u5C1A\u672A\u8BBE\u7F6E' + safeText + '\uFF0C\u8BF7\u5148\u8BBE\u7F6E';
break;
case -2:
reason = '用户取消';
break;
case 3:
reason = '\u6682\u4E0D\u53EF\u7528\uFF0C\u8BF7\u68C0\u67E5' + safeText + '\u7684\u8BBE\u7F6E';
break;
case 0:
case 1:
reason = '';
break;
default:
reason = '暂不可用';
break;
}
return {
reason: reason,
code: Code
};
}
function openTouchID() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var cb = arguments[1];
var _options$isOnlyFinger = options.isOnlyFingerprint,
isOnlyFingerprint = _options$isOnlyFinger === undefined ? true : _options$isOnlyFinger;
platform.select({
iosApp: function iosApp() {
var BSTouchID = window.api.require('BSTouchID');
BSTouchID.isAvailable({}, function (ret, err) {
var _ref = ret || err,
isAvailable = _ref.isAvailable,
Code = _ref.Code;
if (!isAvailable) {
return cb(undefined, {
code: -10000,
msg: '该设备不支持相关特性'
});
}
var _validateCode = validateCode(Code),
code = _validateCode.code,
reason = _validateCode.reason;
if (reason) {
return cb(undefined, {
code: code,
msg: reason
});
}
BSTouchID.verifyTouchID({
isOnlyFingerprint: isOnlyFingerprint
}, function (ret, err) {
var _ref2 = err || ret,
Code = _ref2.Code;
var _validateCode2 = validateCode(Code),
code = _validateCode2.code,
reason = _validateCode2.reason;
if (reason) {
return cb(undefined, {
code: code,
msg: reason
});
}
return cb({
code: 1,
msg: '验证成功'
}, undefined);
});
});
},
androidApp: function androidApp() {
notify(_extends({}, options, {
action: 86
}), function (ret, err) {
var _ref3 = ret || err,
Code = _ref3.Code;
var _validateCode3 = validateCode(Code),
code = _validateCode3.code,
reason = _validateCode3.reason;
if (reason) {
return cb(undefined, {
code: code,
msg: reason
});
}
return cb({
code: 1,
msg: '验证成功'
}, undefined);
});
}
});
}
return openTouchID;
})));