UNPKG

autobots-lib

Version:

汽车人基础库

115 lines (103 loc) 2.54 kB
import ReactNative from 'react-native'; import config from './config'; import { QCRRNModule } from 'autobots-spec-lib' const { NativeModules, RCTDeviceEventEmitter, Platform } = ReactNative; const { AppLoader } = NativeModules; let GetQueryString = function (url, name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = url.match(reg); if (r != null) return unescape(r[2]); return null; } var native = { navigator: null, initNative(callback) { try { var that = this; that.callNative("autobots://sys/getUrlParams", function (result, data) { that.callNative("autobots://sys/getEmpCode", function (result, data) { if (callback) { callback(true, null); } }); }); } catch (e) { if (callback) { callback(false, e); } } }, getVerCode(cb) { var that = this; that.callNative("autobots://sys/getVerCode", function (result, data) { if (cb) { cb(data); } }); }, getNewVerCode(cb) { var that = this; that.callNative("autobots://sys/getNewVerCode", function (result, data) { if (cb) { cb(data); } }); }, gotoHomePage() { try { this.callNative("autobots://sys/gotoHomePage"); } catch (e) { } }, setNoShare(noShare) { let url = 'autobots://sys/setNoShare?noShare=' + noShare; try { this.callNative(url); } catch (e) { } }, gotoPreviousIOS() { try { if (Platform.OS == 'android' || Platform.OS == 'ios') { AppLoader.gotoPrevious(); } } catch (e) { } }, getEmpCode() { return config.get().employee.empCode; }, getPostUrl(url) { let c = config.get(); return c && c.urlParams ? url + '?' + c.urlParams : url; }, callNative(url, callback) { function cb(p) { var obj = JSON.parse(p); if (callback) { if (obj.state == true) { callback(true, obj.returnObject); } else { callback(false, obj.info); } } } try { if (Platform.OS == 'android') { ReactNative.NativeModules.RNModule.callNative(url, cb); } else if (Platform.OS == 'ios') { AppLoader.callNative(url, cb); } else if (Platform.OS == 'harmony') { QCRRNModule.callNative(url, cb); } else { if (callback) { callback(false); } } } catch (e) { if (callback) { callback(false); } } } } module.exports = native;