UNPKG

@auraxy/cef-bridge

Version:

cefCore, boundAsync, cefEnviroment, cefPopupEnvironment, cefJsWebBridge

610 lines (513 loc) 17.4 kB
/** * Bundle of @auraxy/cef-bridge * Generated: 2021-06-25 * Version: 3.0.2 * License: MIT * Author: 2631541504@qq.com */ import { PublishSubscribe } from '@livelybone/simple-observer'; import { singletonObj } from '@livelybone/singleton'; import { StorageUtils } from '@livelybone/storage'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * 是否是有 cefCore, cefEnvironment, cefPopupEnvironment, cefJsWebBridge 的客户端 * */ var CefIsFullBridgeClient = typeof cefCore !== 'undefined'; /** 是否是弹窗 */ var CefIsPopupClient = CefIsFullBridgeClient && typeof cefPopupEnvironment !== 'undefined'; /** * cefJsWebBridge 注册判断,cefJsWebBridge 的相关事件需要在其注册之后调用才能成功 * */ var CefJsWebBridgeReady = new Promise(function (res) { if (typeof cefJsWebBridge !== 'undefined') res();else if (CefIsFullBridgeClient) window.addEventListener('cefJsWebBridgeReady', res);else window.addEventListener('load', res); }); function warn() { console.warn('该功能只在客户端主窗口可用!'); } function throwError() { return Promise.reject(new Error('该功能只在客户端可用')); } /** 前置主窗体 */ function CefShowMainWindow() { if (CefIsFullBridgeClient) cefCore.ShowMainWindow(); } /** 关闭窗口 */ function CefWindowClose() { if (CefIsFullBridgeClient) cefCore.CloseWindow();else window.close(); } function CefExitApp() { if (CefIsFullBridgeClient) { if (cefCore.ExitApp) { cefCore.ExitApp(); } else { cefCore.CloseWindow(); } } else window.close(); } /** 窗口最小化 */ function CefWindowMinimize() { if (CefIsFullBridgeClient) cefCore.SetWindowMinimized(); } /** 打开一个外部浏览器接口 */ function CefOpenUrlInBrowser(url) { if (CefIsFullBridgeClient) cefCore.OpenUrlOnSystem(url);else window.open(url); } /** 监听客户端事件,用于接收从客户端发来的消息 */ function CefAddEventListener(type, handler) { if (CefIsFullBridgeClient && !CefIsPopupClient) { cefJsWebBridge.addEventListener(type, handler); } else if (CefIsPopupClient) warn(); } function CefRemoveEventListener(type, handler) { if (CefIsFullBridgeClient && !CefIsPopupClient) { cefJsWebBridge.addEventListener(type, handler); } else if (CefIsPopupClient) warn(); } /** 获取系统当前显示器的 DPI */ function CefGetDPI() { if (!CefIsFullBridgeClient) { return Promise.resolve(1); } return cefCore.GetDpi().then(function (dpi) { return +dpi || 1; }); } /** 打开弹窗,并接收弹窗传递过来的数据,只能在主窗口调用 */ function CefWindowPopup(renderProps, options) { var content = (renderProps.content || '').replace(/\s\s+/g, ' '); var $renderProps = _objectSpread2(_objectSpread2({}, renderProps), {}, { content: content, title: renderProps.title || '' }); var $options = _objectSpread2(_objectSpread2({ width: 320, height: 180, position: 'centerScreen' }, options), $renderProps); return CefIsFullBridgeClient ? CefJsWebBridgeReady.then(function () { return CefGetDPI().then(function (dpi) { return new Promise(function (res) { return cefJsWebBridge.invoke('showPopup', _objectSpread2(_objectSpread2({}, $options), {}, { width: Math.ceil($options.width * dpi), height: Math.ceil($options.height * dpi) }), res); }); }); }) : new Promise(function (res) { window.open("https://lite.yipiaoyun.cn/custom-dialog#data=".concat(encodeURIComponent(JSON.stringify($renderProps))), 'custom-dialog'); alert(content); res(); }); } /** popup 向主体窗口传递数据,只能在弹窗中调用 */ function CefPopupCallback(data) { if (CefIsPopupClient) { cefPopupEnvironment.callback(StorageUtils.stringifyJSON(data)); } } /** 版本,只能在主窗口调用 */ function CefVersion() { return CefJsWebBridgeReady.then(function () { return new Promise(function (res) { return cefJsWebBridge.getVersion(res); }); }); } /** 获取税盘状态 */ function CefGetTaxDeviceInfo() { return (CefIsFullBridgeClient ? cefCore.GetNowTaxInfo().then(function (str) { return StorageUtils.parseJSON(str); }) : Promise.resolve({ returnCode: '-1', returnMsg: '只能在客户端获取税盘信息!' })).then(function (res) { return _objectSpread2(_objectSpread2({}, res), {}, { status: res.returnCode === '0000' ? 1 : 0, companyName: res.companyName || '', taxNo: res.companyTax || '', deviceNo: res.machineCode || '', message: res.returnMsg || '', type: +res.diskType || 0 }); }); } /** 订阅税盘状态 */ function CefSubscribeTaxDeviceInfo(callback) { var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000; var pubSub = singletonObj('taxDevicePubSub', function () { return new PublishSubscribe(); }); var subscriberId = pubSub.subscribe(callback); var timer; setTimeout(function () { CefGetTaxDeviceInfo().then(pubSub.publish); timer = setInterval(function () { CefGetTaxDeviceInfo().then(pubSub.publish); }, duration); }); return function () { pubSub.unsubscribe(subscriberId); if (timer) clearInterval(timer); }; } /** 打开航信进程/百旺,不传入根据税盘自动判断 */ function CefOpenTaxSoftware(name) { if (!CefIsFullBridgeClient) return throwError(); return Promise.resolve(cefCore.OpenTaxExe(name)).then(function (res) { if (typeof res === 'boolean') return res; return StorageUtils.parseJSON(res); }); } /** 轻量版发送抬头信息到客户端 */ function CefSendTitleToTaxSoftware(data) { if (!CefIsFullBridgeClient) return throwError(); return cefCore.DoSendTitle(StorageUtils.stringifyJSON(data)).then(function (res) { if (!res) return res; throw new Error(res); }); } /** * 轻量版电票开具,json 数据字符串化 * 如果成功,返回 null | '' * 如果证书密码错误,返回 '99',则弹出提示窗口 * 有异常,则返回提示字符串 * * data 数据示例:https://www.showdoc.cc/363387933772874?page_id=2117156569221380 (页面中的 `入参示例`) * */ function CefSendDataForCE(data) { if (!CefIsFullBridgeClient) return throwError(); return cefCore.MakeoutFromPreview(StorageUtils.stringifyJSON(data)).then(function (res) { if (!res) return res; throw new Error(res); }); } /** * 轻量版设置证书密码 * 如果成功,返回 null | '' * 有异常,则返回提示字符串 * */ function CefSetCertPassword(pwd) { if (!CefIsFullBridgeClient) return throwError(); return cefCore.SetCertPassword(pwd).then(function (res) { if (!res) return res; throw new Error(res); }); } /** * 轻量版 pdf 生成 * 如果成功,返回 null | '' * 如果证书密码错误,返回 '99',则弹出提示窗口 * 其他异常提示,返回提示字符串 * * data 数据来源于服务端 * */ function CefMakePdf(data) { if (!CefIsFullBridgeClient) return throwError(); return cefCore.MakePdf(StorageUtils.stringifyJSON(data)).then(function (res) { if (!res) return res; throw new Error(res); }); } /** 查找当前环境,是否是航信还是百旺,不传入根据税盘自动判断 */ function CefIsTaxSoftWareOpen(name) { if (!CefIsFullBridgeClient) return throwError(); return cefCore.IsTaxProcessOpen(name); } /** Play sound */ function CefPlayAudio(url) { if (!CefIsFullBridgeClient) console.warn('请在客户端调用音频接口!');else cefCore.PlaySound(url); } /** Has OFD function */ function CefHasOFD() { try { return Promise.resolve(cefCore.HasOfd()); } catch (e) { return Promise.resolve(false); } } /** * 调用没记录的可能存在的接口 * * 一般用于服务端通过 H5 与 .net 客户端交互,这时前端可以做被动调用就行,接口名以及参数都由后端提供 * */ function CefCall(apiName) { var api = cefCore[apiName] || cefJsWebBridge[apiName]; if (!api) throw new Error("\u63A5\u53E3 ".concat(apiName, " \u5728\u5F53\u524D\u7248\u672C\u7684\u5BA2\u6237\u7AEF\u4E0D\u5B58\u5728\uFF01")); for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } return api.apply(void 0, args); } /** 退出客户端 */ function SignOutClient(version) { if (cefCore.mutualSwitch) return cefCore.mutualSwitch(version); } /** * 判断是否是 KaiPiao 完整版客户端 * */ var IsKaiPiaoClient = typeof boundAsync !== 'undefined'; /** 获取完整版客户端信息 */ function GetKaiPiaoClientInfo() { return singletonObj('clientInfo', function () { if (IsKaiPiaoClient && boundAsync.getIce) { return boundAsync.getIce().then(function (res) { return Promise.resolve(JSON.parse(res)); }); } return Promise.resolve(null); }); } // 获取客户端当前具体版本 function GetKaiPiaoClientVersion() { return _GetKaiPiaoClientVersion.apply(this, arguments); } // 获取完整版客户端当前大版本 function _GetKaiPiaoClientVersion() { _GetKaiPiaoClientVersion = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var clientInfo; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return GetKaiPiaoClientInfo(); case 2: clientInfo = _context.sent; if (!clientInfo) { _context.next = 5; break; } return _context.abrupt("return", clientInfo.AppVer); case 5: return _context.abrupt("return", null); case 6: case "end": return _context.stop(); } } }, _callee); })); return _GetKaiPiaoClientVersion.apply(this, arguments); } function GetKaiPiaoClientAppName() { return _GetKaiPiaoClientAppName.apply(this, arguments); } function _GetKaiPiaoClientAppName() { _GetKaiPiaoClientAppName = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { var clientInfo; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return GetKaiPiaoClientInfo(); case 2: clientInfo = _context2.sent; if (!clientInfo) { _context2.next = 5; break; } return _context2.abrupt("return", clientInfo.AppName); case 5: return _context2.abrupt("return", null); case 6: case "end": return _context2.stop(); } } }, _callee2); })); return _GetKaiPiaoClientAppName.apply(this, arguments); } function IsKaiPiaoClientV2ByIce() { return _IsKaiPiaoClientV2ByIce.apply(this, arguments); } function _IsKaiPiaoClientV2ByIce() { _IsKaiPiaoClientV2ByIce = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { var appName; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return GetKaiPiaoClientAppName(); case 2: appName = _context3.sent; if (!appName) { _context3.next = 5; break; } return _context3.abrupt("return", appName.endsWith('V2')); case 5: return _context3.abrupt("return", false); case 6: case "end": return _context3.stop(); } } }, _callee3); })); return _IsKaiPiaoClientV2ByIce.apply(this, arguments); } function IsKaiPiaoClientV3ByIce() { return _IsKaiPiaoClientV3ByIce.apply(this, arguments); } /** * 完整版 - * 从选中的税盘判断是V2还是V3版本 * */ function _IsKaiPiaoClientV3ByIce() { _IsKaiPiaoClientV3ByIce = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { var appName; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return GetKaiPiaoClientAppName(); case 2: appName = _context4.sent; if (!appName) { _context4.next = 5; break; } return _context4.abrupt("return", appName.endsWith('V3')); case 5: return _context4.abrupt("return", false); case 6: case "end": return _context4.stop(); } } }, _callee4); })); return _IsKaiPiaoClientV3ByIce.apply(this, arguments); } function IsKaiPiaoClientV2ByTaxDisk(clientInfo) { if (clientInfo) { var AppName = clientInfo.AppName; return !!AppName && AppName.endsWith('V2'); } return false; } function IsKaiPiaoClientV3ByTaxDisk(clientInfo) { if (clientInfo) { var AppName = clientInfo.AppName; return !!AppName && AppName.endsWith('V3'); } return false; } /** 客户端唤起客户端 */ function JumpAction(jumpProps) { if (boundAsync.jumpAction) return boundAsync.jumpAction(jumpProps.type, jumpProps.url); } /** 完整版发票修复 */ function InvoiceFix() { if (IsKaiPiaoClient) { boundAsync.callInvoiceFix(); return Promise.resolve(true); } return Promise.reject(new Error('浏览器不支持修复功能,请在客户端修复!')); } /** 强制刷新页面 */ function PageReload() { if (IsKaiPiaoClient) boundAsync.refushHome();else window.location.reload(); } /** 完整版客户端登录 */ function LoginKaiPiao(username, pwd) { if (IsKaiPiaoClient) return boundAsync.login(username, pwd); return Promise.reject(new Error('不在客户端!')); } /** 完整版客户端登出 */ function LogoutKaiPiao() { if (IsKaiPiaoClient) boundAsync.logout(); } /** * T 回调数据类型 * */ var TaxSoftWares; (function (TaxSoftWares) { TaxSoftWares["HX"] = "hx"; TaxSoftWares["BW"] = "bw"; })(TaxSoftWares || (TaxSoftWares = {})); var InvoiceType; (function (InvoiceType) { InvoiceType["Common"] = "c"; InvoiceType["Special"] = "s"; InvoiceType["Electronic"] = "ce"; InvoiceType["BlockChain"] = "bc"; })(InvoiceType || (InvoiceType = {})); /** * 是否是客户端 * */ var IsClient = IsKaiPiaoClient || CefIsFullBridgeClient; export { CefAddEventListener, CefCall, CefExitApp, CefGetDPI, CefGetTaxDeviceInfo, CefHasOFD, CefIsFullBridgeClient, CefIsPopupClient, CefIsTaxSoftWareOpen, CefJsWebBridgeReady, CefMakePdf, CefOpenTaxSoftware, CefOpenUrlInBrowser, CefPlayAudio, CefPopupCallback, CefRemoveEventListener, CefSendDataForCE, CefSendTitleToTaxSoftware, CefSetCertPassword, CefShowMainWindow, CefSubscribeTaxDeviceInfo, CefVersion, CefWindowClose, CefWindowMinimize, CefWindowPopup, GetKaiPiaoClientAppName, GetKaiPiaoClientInfo, GetKaiPiaoClientVersion, InvoiceFix, InvoiceType, IsClient, IsKaiPiaoClient, IsKaiPiaoClientV2ByIce, IsKaiPiaoClientV2ByTaxDisk, IsKaiPiaoClientV3ByIce, IsKaiPiaoClientV3ByTaxDisk, JumpAction, LoginKaiPiao, LogoutKaiPiao, PageReload, SignOutClient, TaxSoftWares };