UNPKG

wnjk

Version:

两只蜗牛通用微服务脚本

216 lines (215 loc) 7.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ibaseNative_1 = __importDefault(require("../../interface/ibaseNative")); const jsBridge_1 = __importDefault(require("../../utils/jsBridge")); const response_1 = __importDefault(require("../../model/response")); const webCommand_1 = __importDefault(require("../../model/webCommand")); class WebNative extends ibaseNative_1.default { constructor(appKey) { super(appKey); this.listen(); } listen() { window.onmessage = (e) => { if (e && e.data) { try { let command = e.data; if (typeof e.data == 'string') { command = JSON.parse(e.data); } if (command.methodName && command.isContainerMsg != false) { if (window.native[command.methodName]) { window.native[command.methodName](command.content); } else { console.log('接收到没有约定过的命令'); } } else { console.log('接收到无法识别的命令'); } } catch (err) { console.log('命令序列化出错:', err); } } }; } sendMsg(data) { if (self != top) { window.parent.postMessage(data, '*'); } else { throw new Error(`当前为非容器环境运行,无法实现交互!`); console.log('没有找到父级页面'); } } createWebCommand(methodName, data) { let command = new webCommand_1.default(); command.methodName = methodName; command.source = this.appKey; command.content = data; return command; } getToken(callback) { let command = this.createWebCommand('getToken', "{}"); this.sendMsg(command); jsBridge_1.default.injectListen('getToken', (sysInfo) => { if (callback) { callback(sysInfo); } }); } getDoctorInfo(callback) { let command = this.createWebCommand('getDoctorInfo', "{}"); this.sendMsg(command); jsBridge_1.default.injectListen('getDoctorInfo', (userInfo) => { if (callback) { callback(userInfo); } }); } getSystemInfo(callback) { let command = this.createWebCommand('getSystemInfo', "{}"); this.sendMsg(command); jsBridge_1.default.injectListen('getSystemInfo', (sysInfo) => { if (callback) { callback(sysInfo); } }); } measure(measureType, presetParam, callback) { throw new Error('没有对应实现'); } onAppStart(appInfo) { let command = this.createWebCommand('onAppStart', appInfo); this.sendMsg(command); } onAPPClose(callback) { jsBridge_1.default.injectListen('onAPPClose', callback); } getResidentInfo(healthRecordId, callback) { let res = new response_1.default(); if (!healthRecordId) { res.code = 1; res.errorMsg = `居民档案id不能为空`; if (callback) { callback(res); } return; } let command = this.createWebCommand('getResidentInfo', { healthRecordId: healthRecordId }); this.sendMsg(command); jsBridge_1.default.injectListen('getResidentInfo', (res) => { if (callback) { callback(res); } }); } exitApp() { let command = this.createWebCommand('exitApp', ""); this.sendMsg(command); } scanQRCode(callback) { throw new Error('web端容器没有二维码扫码实现!'); } pickPhoto(callback) { throw new Error('web端容器没有照片选择实现!'); } pickResident(condition, callback) { let command = this.createWebCommand('pickResident', Object.assign({}, condition)); this.sendMsg(command); jsBridge_1.default.injectListen('pickResident', (res) => { if (callback) { callback(res); } }); } pickDoctor(condition, callback) { let command = this.createWebCommand('pickDoctor', Object.assign({}, condition)); this.sendMsg(command); jsBridge_1.default.injectListen('pickDoctor', (res) => { if (callback) { callback(res); } }); } verificationApp(callback) { let command = this.createWebCommand('verificationApp', ""); this.sendMsg(command); jsBridge_1.default.injectListen('verificationApp', (res) => { if (callback) { callback(res); } }); } checkResidentSignStatus(spkgId, healthRecordId, callback) { let res = new response_1.default(); if (!spkgId) { res.code = 1; res.errorMsg = `签约服务包id不能为空`; if (callback) { callback(res); } return; } if (!healthRecordId) { res.code = 1; res.errorMsg = `签约居民档案id不能为空`; if (callback) { callback(res); } return; } let command = this.createWebCommand('checkResidentSignStatus', { spkgId: spkgId, healthRecordId: healthRecordId }); this.sendMsg(command); jsBridge_1.default.injectListen('checkResidentSignStatus', (res) => { if (callback) { callback(res); } }); } bindInspectionNum(measureType, businessId, callback) { let res = new response_1.default(); if (!businessId) { res.code = 1; res.errorMsg = `微应用提供的业务id不能为空`; if (callback) { callback(res); } return; } if (!this.measureArr.includes(measureType)) { res.code = 1; res.errorMsg = `不支持的设备类型‘${measureType}’`; if (callback) { callback(res); } return; } let command = this.createWebCommand('bindInspectionNum', { id: businessId, measureType: measureType }); this.sendMsg(command); jsBridge_1.default.injectListen('bindInspectionNum', (res) => { if (callback) { callback(res); } }); } nativeExtendsCall(methodName, param, callback) { let fullParam = { appKey: this.appKey }; if (param) { fullParam = Object.assign(Object.assign({}, fullParam), param); } let command = this.createWebCommand(methodName, fullParam); this.sendMsg(command); jsBridge_1.default.injectListen(methodName, (res) => { if (callback) { callback(res); } }); } } exports.default = WebNative;