UNPKG

@molejs/mole-jssdk

Version:

mole app jssdk

362 lines (320 loc) 9.46 kB
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 dynamicLoadJs$1(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); } function getJSBridge() { var t = void 0; var count = 0; return new Promise(function (reslove, reject) { if (platform.dingTalk) { return dynamicLoadJs$1('//g.alicdn.com/dingding/dingtalk-jsapi/2.3.0/dingtalk.open.js', function () { return reslove({}); }); } if (platform.bsAppAndroid) { if (window.BSJSBridge) { return reslove({ api: window.BSJSBridge }); } t = setInterval(function () { count++; if (window.BSJSBridge) { clearInterval(t); count = 0; return reslove({ api: window.BSJSBridge }); } if (count <= 300) { clearInterval(t); count = 0; return reslove({ api: window.BSJSBridge }); } }, 20); } else if (platform.bsAppIOS) { if (window.api) { count = 0; return reslove({ api: window.api }); } t = setInterval(function () { count++; if (window.api) { count = 0; clearInterval(t); return reslove({ api: window.api }); } if (count >= 500) { clearInterval(t); count = 0; return reject(); } }, 20); } else { return reslove({}); } }); } export default getJSBridge;