|
|
@@ -1,5 +1,244 @@
|
|
|
-import { ProginnBridge } from 'proginn-lib'
|
|
|
+const factory = function() {
|
|
|
+ var COOKIE_APP_KEY = 'x_app';
|
|
|
|
|
|
-const bridge = process.client ? new ProginnBridge() : {}
|
|
|
+ function setupWebViewJavascriptBridge(callback) {
|
|
|
+ if (window.WebViewJavascriptBridge) {
|
|
|
+ return callback(window.WebViewJavascriptBridge)
|
|
|
+ }
|
|
|
+ if (window.WVJBCallbacks) {
|
|
|
+ return window.WVJBCallbacks.push(callback)
|
|
|
+ }
|
|
|
+ window.WVJBCallbacks = [callback]
|
|
|
+ var WVJBIframe = document.createElement('iframe')
|
|
|
+ WVJBIframe.style.display = 'none'
|
|
|
+ WVJBIframe.src = 'https://__bridge_loaded__'
|
|
|
+ document.documentElement.appendChild(WVJBIframe)
|
|
|
+ setTimeout(function() {
|
|
|
+ document.documentElement.removeChild(WVJBIframe)
|
|
|
+ }, 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ var compareVersions = (function () {
|
|
|
+ var semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+))?(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
|
|
|
+
|
|
|
+ function indexOrEnd(str, q) {
|
|
|
+ return str.indexOf(q) === -1 ? str.length : str.indexOf(q);
|
|
|
+ }
|
|
|
+
|
|
|
+ function split(v) {
|
|
|
+ var c = v.replace(/^v/, '').replace(/\+.*$/, '');
|
|
|
+ var patchIndex = indexOrEnd(c, '-');
|
|
|
+ var arr = c.substring(0, patchIndex).split('.');
|
|
|
+ arr.push(c.substring(patchIndex + 1));
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
+
|
|
|
+ function tryParse(v) {
|
|
|
+ return isNaN(Number(v)) ? v : Number(v);
|
|
|
+ }
|
|
|
+
|
|
|
+ function validate(version) {
|
|
|
+ if (typeof version !== 'string') {
|
|
|
+ throw new TypeError('Invalid argument expected string');
|
|
|
+ }
|
|
|
+ if (!semver.test(version)) {
|
|
|
+ throw new Error('Invalid argument not valid semver (\'' + version + '\' received)');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function compareVersions(v1, v2) {
|
|
|
+ [v1, v2].forEach(validate);
|
|
|
+
|
|
|
+ var s1 = split(v1);
|
|
|
+ var s2 = split(v2);
|
|
|
+
|
|
|
+ for (var i = 0; i < Math.max(s1.length - 1, s2.length - 1); i++) {
|
|
|
+ var n1 = parseInt(s1[i] || 0, 10);
|
|
|
+ var n2 = parseInt(s2[i] || 0, 10);
|
|
|
+
|
|
|
+ if (n1 > n2) return 1;
|
|
|
+ if (n2 > n1) return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ var sp1 = s1[s1.length - 1];
|
|
|
+ var sp2 = s2[s2.length - 1];
|
|
|
+
|
|
|
+ if (sp1 && sp2) {
|
|
|
+ var p1 = sp1.split('.').map(tryParse);
|
|
|
+ var p2 = sp2.split('.').map(tryParse);
|
|
|
+
|
|
|
+ for (i = 0; i < Math.max(p1.length, p2.length); i++) {
|
|
|
+ if (p1[i] === undefined || typeof p2[i] === 'string' && typeof p1[i] === 'number') return -1;
|
|
|
+ if (p2[i] === undefined || typeof p1[i] === 'string' && typeof p2[i] === 'number') return 1;
|
|
|
+
|
|
|
+ if (p1[i] > p2[i]) return 1;
|
|
|
+ if (p2[i] > p1[i]) return -1;
|
|
|
+ }
|
|
|
+ } else if (sp1 || sp2) {
|
|
|
+ return sp1 ? -1 : 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ };
|
|
|
+
|
|
|
+ var operatorResMap = {
|
|
|
+ 'gt': [1],
|
|
|
+ 'gte': [0, 1],
|
|
|
+ 'eq': [0],
|
|
|
+ 'lte': [-1, 0],
|
|
|
+ 'lt': [-1]
|
|
|
+ }
|
|
|
+
|
|
|
+ compareVersions.compare = function(operator, v1, v2) {
|
|
|
+ var res = compareVersions(v1, v2);
|
|
|
+ return operatorResMap[operator].indexOf(res) > -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return compareVersions;
|
|
|
+ })()
|
|
|
+
|
|
|
+ function getCookie(key) {
|
|
|
+ return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
|
|
|
+ }
|
|
|
+
|
|
|
+ function getAppInfo() {
|
|
|
+ var val = getCookie(COOKIE_APP_KEY)
|
|
|
+ var matched = val && /^(ios|android)\ ((?:\d\.?)+)$/.exec(val)
|
|
|
+
|
|
|
+ if (!matched) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ os: matched[1],
|
|
|
+ version: matched[2]
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function compareAppVersion(operator, version) {
|
|
|
+ return !!this.appInfo && compareVersions.compare(operator, this.appInfo.version, version)
|
|
|
+ }
|
|
|
+
|
|
|
+ function invoke(fn, data, cb) {
|
|
|
+ if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
|
|
|
+ setupWebViewJavascriptBridge(function (bridge) {
|
|
|
+ bridge.callHandler(fn, data, cb)
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!this.root) {
|
|
|
+ console.warn('Bridge invoke ' + fn + ' skipped.')
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.isAndroid) {
|
|
|
+ if (typeof this.root[fn] === 'function') {
|
|
|
+ return payload ? this.root[fn](payload) : this.root[fn]()
|
|
|
+ } else {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return this.root(fn, payload)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function load(url) {
|
|
|
+ window.location.href = url
|
|
|
+ }
|
|
|
+
|
|
|
+ function login() {
|
|
|
+ if (!this.appInfo) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ this.load('proginn://login?backToPage=true')
|
|
|
+ }
|
|
|
+
|
|
|
+ function back() {
|
|
|
+ if (!this.appInfo) {
|
|
|
+ window.history.back()
|
|
|
+ } else {
|
|
|
+ if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
|
|
|
+ this.invoke('back')
|
|
|
+ } else {
|
|
|
+ this.invoke('back_page')
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function loadUserData(data) {
|
|
|
+ if (this.isAndroid) {
|
|
|
+ this.invoke('user_load', data)
|
|
|
+ } else if (this.compareAppVersion('lt', '4.22.0')) {
|
|
|
+ this.invoke('user_load', {
|
|
|
+ userInfo: data,
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ this.invoke('loadUserData', data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function loadShareData(data) {
|
|
|
+ if (this.isAndroid) {
|
|
|
+ this.invoke('load_share_data', JSON.stringify(data))
|
|
|
+ } else if (this.compareAppVersion('lt', '4.22.0')) {
|
|
|
+ this.invoke('load_share_data', data)
|
|
|
+ } else {
|
|
|
+ this.invoke('loadShareData', data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function setNavigationBarColor(hex) {
|
|
|
+ if (this.isAndroid) {
|
|
|
+ window.appBridge.setTitleBarColor(hex);
|
|
|
+ }
|
|
|
+ else if (this.compareAppVersion('lt', '4.22.0')) {
|
|
|
+ this.invoke('setTitleBarColor', hex);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ this.invoke('setNavigationBarColor', hex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function setNavigationBarTitle(text) {
|
|
|
+ this.invoke('setNavigationBarTitle', text);
|
|
|
+ }
|
|
|
+
|
|
|
+ function close() {
|
|
|
+ if (this.isAndroid || this.compareAppVersion('lt', '4.22.0')) {
|
|
|
+ this.invoke('finishActivity')
|
|
|
+ } else {
|
|
|
+ this.invoke('close')
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var ua = window.navigator.userAgent
|
|
|
+ var appInfo = getAppInfo()
|
|
|
+ var isIos = /iP(hone|ad|od)/.test(ua) || (appInfo && appInfo.os === 'ios') || false
|
|
|
+ var isAndroid = /Android/.test(ua) || (appInfo && appInfo.os === 'android') || false
|
|
|
+
|
|
|
+ return {
|
|
|
+ get isInApp() {
|
|
|
+ return !!(appInfo || window.app_event)
|
|
|
+ },
|
|
|
+ root: window.app_event,
|
|
|
+ appInfo: appInfo,
|
|
|
+ isIos: isIos,
|
|
|
+ isAndroid: isAndroid,
|
|
|
+ compareAppVersion: compareAppVersion,
|
|
|
+ invoke: invoke,
|
|
|
+ load: load,
|
|
|
+ login: login,
|
|
|
+ back: back,
|
|
|
+ close: close,
|
|
|
+ loadUserData: loadUserData,
|
|
|
+ loadShareData: loadShareData,
|
|
|
+ setNavigationBarColor: setNavigationBarColor,
|
|
|
+ setNavigationBarTitle: setNavigationBarTitle
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const bridge = process.client ? factory() : {}
|
|
|
|
|
|
export default bridge
|