@hd-front-end/jsbridge-sdk 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/README.md +4 -2
  2. package/dist/index.d.ts +204 -2
  3. package/dist/index.esm.js +446 -1
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.js +448 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.umd.js +450 -4
  8. package/dist/index.umd.js.map +1 -1
  9. package/docs/00-/351/241/271/347/233/256/346/246/202/350/247/210.md +282 -0
  10. package/docs/01-/346/236/266/346/236/204/350/256/276/350/256/241.md +623 -0
  11. package/docs/02-/346/212/200/346/234/257/345/256/236/347/216/260.md +867 -0
  12. package/docs/03-API/344/275/277/347/224/250/346/226/207/346/241/243.md +1104 -0
  13. package/docs/04-/346/265/213/350/257/225/346/226/271/346/241/210.md +360 -0
  14. package/docs/05-/350/277/201/347/247/273/346/214/207/345/215/227.md +181 -0
  15. package/docs/06-/346/236/266/346/236/204/345/233/276/351/233/206.md +738 -0
  16. package/docs/07-/346/226/260/346/241/245/346/216/245/346/226/271/346/263/225/346/211/251/345/261/225/350/257/264/346/230/216.md +139 -0
  17. package/docs/CODE_REVIEW.md +65 -0
  18. package/docs/EVALUATION.md +72 -0
  19. package/docs/README.md +258 -0
  20. package/docs//345/205/263/351/224/256/351/227/256/351/242/230/350/247/243/347/255/224.md +495 -0
  21. package/docs//346/226/207/346/241/243/346/225/264/345/220/210/350/257/264/346/230/216.md +265 -0
  22. package/docs//346/233/264/346/226/260/346/227/245/345/277/227.md +669 -0
  23. package/docs//347/224/237/344/272/247/347/272/247-/345/277/253/351/200/237/345/274/200/345/247/213-v2.md +673 -0
  24. package/docs//347/224/237/344/272/247/347/272/247-/346/236/266/346/236/204/350/256/276/350/256/241-v2.md +730 -0
  25. package/docs//350/256/276/350/256/241/347/220/206/345/277/265/350/257/264/346/230/216.md +438 -0
  26. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var imageTools = require('image-tools');
4
+
3
5
  /**
4
6
  * JSBridge 核心通信层
5
7
  *
@@ -1341,6 +1343,449 @@ async function logout() {
1341
1343
  }
1342
1344
  }
1343
1345
 
1346
+ class PageLifeCycle {
1347
+ constructor(state, route) {
1348
+ this.state = state;
1349
+ this.route = route;
1350
+ }
1351
+ }
1352
+
1353
+ class UniScanRequest {
1354
+ constructor() {
1355
+ this.scanType = ['qrCode', 'barCode']; // 扫码识别类型,参数可多选,qrCode、barCode,不设置,默认识别所有
1356
+ this.hideAlbum = false; // 是否隐藏相册,默认false不隐藏
1357
+ this.language = 'zh-Hans'; // ios需要设置这个参数,只支持中英文 zh-Hans、en,默认中文
1358
+ this.failedMsg = '未识别到二维码,请重试'; // 相册选择照片识别错误提示(ios)
1359
+ this.screenType = 'full'; // Android支持全屏需要设置此参数
1360
+ this.timeoutInterval = '10'; // 设置超时时间,单位秒,默认10秒
1361
+ this.timeoutText = '未识别到二维码?'; // 超时提醒文本,默认未识别到二维码?
1362
+ this.viewText = '扫二维码/条码'; // 扫码提示文字,设置null时显示sdk默认文案
1363
+ }
1364
+ }
1365
+
1366
+ class UniScanResult {
1367
+ }
1368
+
1369
+ class JsBridgeHandlers {
1370
+ /**
1371
+ * 路由变化通知原生
1372
+ * @param to
1373
+ * @param from
1374
+ */
1375
+ static handleRouterAfterEach(to, from) {
1376
+ return bridge.call('routerAfterEach', { to, from });
1377
+ }
1378
+ /**
1379
+ * 获取app端信息
1380
+ */
1381
+ static handleGetAppInfo() {
1382
+ return bridge.call('getAppInfo');
1383
+ }
1384
+ /**
1385
+ * 页面生命周期通知原生
1386
+ * @param lifecycle
1387
+ */
1388
+ static handlePageLifeCycle(lifecycle) {
1389
+ return bridge.call('uniPageLifeCycle', lifecycle);
1390
+ }
1391
+ /**
1392
+ * 注册路由跳转调用
1393
+ * @param handler
1394
+ */
1395
+ static registerOnRoute(handler) {
1396
+ return bridge.register('route', handler);
1397
+ }
1398
+ /**
1399
+ * 注册获取路由信息调用
1400
+ */
1401
+ static registerOnGetRouteInfo(handler) {
1402
+ return bridge.register('getRouteInfo', handler);
1403
+ }
1404
+ /**
1405
+ * 注册刷新store数据
1406
+ * @param handler
1407
+ */
1408
+ static registerOnRefreshStore(handler) {
1409
+ return bridge.register('refreshStore', handler);
1410
+ }
1411
+ /**
1412
+ * 注册PDA扫码回调
1413
+ * @param handler
1414
+ */
1415
+ static registerOnPdaScan(handler) {
1416
+ return bridge.register('pdaScan', handler);
1417
+ }
1418
+ /**
1419
+ * 调用原生扫码
1420
+ * @param request
1421
+ */
1422
+ static handleScan(request = new UniScanRequest()) {
1423
+ return bridge.call('scan', request).then((res) => {
1424
+ const json = JSON.parse(res);
1425
+ const result = new UniScanResult();
1426
+ // eslint-disable-next-line @typescript-eslint/camelcase
1427
+ result.resp_code = json.resp_code;
1428
+ // eslint-disable-next-line @typescript-eslint/camelcase
1429
+ result.resp_result = json.resp_result;
1430
+ // eslint-disable-next-line @typescript-eslint/camelcase
1431
+ result.resp_message = json.resp_message;
1432
+ return result;
1433
+ });
1434
+ }
1435
+ /**
1436
+ * 调用原生选择图片/视频
1437
+ * @param request
1438
+ */
1439
+ static handleChooseMedia(request) {
1440
+ return bridge.call('chooseMedia', request).then((res) => {
1441
+ if (res) {
1442
+ const result = JSON.parse(res);
1443
+ return result;
1444
+ }
1445
+ else
1446
+ return null;
1447
+ });
1448
+ }
1449
+ /**
1450
+ * 获取图片信息
1451
+ * @param request
1452
+ */
1453
+ static handleGetImageInfo(request) {
1454
+ return bridge.call('getImageInfo', request).then((res) => {
1455
+ const result = JSON.parse(res);
1456
+ return result;
1457
+ });
1458
+ }
1459
+ /**
1460
+ * 上传文件
1461
+ * @param request
1462
+ */
1463
+ static handleUploadFile(request) {
1464
+ return bridge.call('uploadFile', request).then((res) => {
1465
+ const result = JSON.parse(res);
1466
+ return result;
1467
+ });
1468
+ }
1469
+ /**
1470
+ * 关闭当前WebView
1471
+ * @param request
1472
+ */
1473
+ static handleCloseWebView() {
1474
+ return bridge.call('closeWebView');
1475
+ }
1476
+ /**
1477
+ * 打电话
1478
+ * @param request
1479
+ */
1480
+ static handleMakePhoneCall(request) {
1481
+ return bridge.call('makePhoneCall', request);
1482
+ }
1483
+ /**
1484
+ * 动态设置标题
1485
+ * @param request
1486
+ */
1487
+ static handleSetNavigationBar(request) {
1488
+ return bridge.call('setNavigationBar', request);
1489
+ }
1490
+ /**
1491
+ * 打日志
1492
+ * @param request
1493
+ */
1494
+ static handleLog(request) {
1495
+ return bridge.call('log', request);
1496
+ }
1497
+ /**
1498
+ * 通知原生APP已加载完成
1499
+ * @param data
1500
+ */
1501
+ static handleAppOnload(data) {
1502
+ return JsBridgeHandlers.handlePageLifeCycle(new PageLifeCycle('appOnLaunch', data));
1503
+ }
1504
+ /**
1505
+ * 通知原生APP退出登录
1506
+ * @param data
1507
+ */
1508
+ static handleLogout() {
1509
+ return bridge.call('logout');
1510
+ }
1511
+ /**
1512
+ * 通知原生APP打开链接
1513
+ * @param data
1514
+ */
1515
+ static handleOpenSchemeUrl(request) {
1516
+ return bridge.call('openSchemeUrl', request);
1517
+ }
1518
+ /**
1519
+ * 图片路径转换成base64格式
1520
+ * @param path
1521
+ */
1522
+ static handlePathToBase64(request) {
1523
+ return bridge.call('pathToBase64', request).then((res) => {
1524
+ const result = JSON.parse(res);
1525
+ return result;
1526
+ });
1527
+ }
1528
+ static handleBluetoothPrintImage(request) {
1529
+ console.log(request);
1530
+ return bridge.call('printImage', request).then((res) => {
1531
+ if (!res)
1532
+ return res;
1533
+ if (typeof res === 'string') {
1534
+ try {
1535
+ return JSON.parse(res);
1536
+ }
1537
+ catch (e) {
1538
+ return res;
1539
+ }
1540
+ }
1541
+ return res;
1542
+ });
1543
+ }
1544
+ /**
1545
+ * 蓝牙打印
1546
+ * @param data
1547
+ */
1548
+ static handleBluetoothPrint(request) {
1549
+ return bridge.call('bluetoothPrint', request).then((res) => {
1550
+ if (!res)
1551
+ return res;
1552
+ if (typeof res === 'string') {
1553
+ try {
1554
+ return JSON.parse(res);
1555
+ }
1556
+ catch (e) {
1557
+ return res;
1558
+ }
1559
+ }
1560
+ return res;
1561
+ });
1562
+ }
1563
+ /**
1564
+ * 通知原生APP重新加载子应用
1565
+ * @param request
1566
+ */
1567
+ static handleReloadModule(request) {
1568
+ return bridge.call('reloadModule', request);
1569
+ }
1570
+ }
1571
+
1572
+ // 为什么会有这个类?
1573
+ //uni.chooseImage = chooseImage
1574
+ //Cannot assign to read only property 'chooseImage' of object '[object Object]'
1575
+ // chooseImage是只读的,无法替换,只能用代理模式,如果有好方法替换可以删除这个类
1576
+ const UniProxy = {
1577
+ chooseImage: uni.chooseImage,
1578
+ pathToBase64: imageTools.pathToBase64
1579
+ };
1580
+
1581
+ function hdH5Adapter() {
1582
+ uni.scanCode = (options) => {
1583
+ const mpaasScanOptions = {
1584
+ scanType: options.scanType || ['qrCode', 'barCode'],
1585
+ hideAlbum: options.onlyFromCamera || false,
1586
+ viewText: '扫二维码/条码',
1587
+ language: 'zh-Hans',
1588
+ failedMsg: '未识别到二维码,请重试',
1589
+ screenType: 'full',
1590
+ timeoutInterval: '10',
1591
+ timeoutText: '未识别到二维码?'
1592
+ };
1593
+ JsBridgeHandlers.handleScan(mpaasScanOptions).then((ret) => {
1594
+ var _a, _b, _c, _d;
1595
+ if (ret.resp_code === 1000) {
1596
+ (_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, {
1597
+ result: ret.resp_result || '',
1598
+ scanType: mpaasScanOptions.scanType[0],
1599
+ charSet: 'utf-8',
1600
+ path: ''
1601
+ });
1602
+ }
1603
+ else if (ret.resp_code === 10) {
1604
+ (_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: '用户取消扫码' });
1605
+ }
1606
+ else {
1607
+ (_c = options.fail) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: ret.resp_message || '扫码失败' });
1608
+ }
1609
+ (_d = options.complete) === null || _d === void 0 ? void 0 : _d.call(options, ret);
1610
+ });
1611
+ };
1612
+ // uni.chooseImage = chooseImage 会报错: Cannot assign to read only property 'chooseImage' of object '[object Object]'
1613
+ UniProxy.chooseImage = (options) => {
1614
+ const request = {
1615
+ extension: options.extension || [], //TODO 未实现
1616
+ sourceType: options.sourceType || ['album', 'camera'],
1617
+ camera: 'back',
1618
+ mediaType: ['image'],
1619
+ count: options.count || 9,
1620
+ maxDuration: 30, // 图片忽略
1621
+ sizeType: ['original', 'compressed'] //TODO 未实现
1622
+ };
1623
+ JsBridgeHandlers.handleChooseMedia(request).then((ret) => {
1624
+ var _a, _b, _c;
1625
+ if (ret == null) {
1626
+ (_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: '用户取消' });
1627
+ }
1628
+ else {
1629
+ const result = {
1630
+ tempFiles: ret.tempFiles.map((item) => {
1631
+ return {
1632
+ path: item.tempFilePath,
1633
+ size: item.size,
1634
+ type: item.fileType,
1635
+ name: item.name,
1636
+ width: item.width,
1637
+ height: item.height
1638
+ };
1639
+ }),
1640
+ tempFilePaths: ret.tempFilePaths
1641
+ };
1642
+ (_b = options.success) === null || _b === void 0 ? void 0 : _b.call(options, result);
1643
+ }
1644
+ (_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, ret);
1645
+ });
1646
+ };
1647
+ uni.chooseVideo = (options) => {
1648
+ const request = {
1649
+ extension: options.extension || [], //TODO 未实现
1650
+ sourceType: options.sourceType || ['album', 'camera'],
1651
+ camera: options.camera || 'back',
1652
+ mediaType: ['video'],
1653
+ count: 1,
1654
+ maxDuration: options.maxDuration || 30, // 图片忽略
1655
+ sizeType: ['original', 'compressed'] //TODO 未实现
1656
+ };
1657
+ JsBridgeHandlers.handleChooseMedia(request).then((ret) => {
1658
+ var _a, _b, _c;
1659
+ if (ret == null) {
1660
+ (_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: '用户取消' });
1661
+ }
1662
+ else {
1663
+ const first = ret.tempFiles[0];
1664
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
1665
+ // @ts-ignore
1666
+ const result = {
1667
+ tempFilePath: first.tempFilePath,
1668
+ size: first.size,
1669
+ duration: first.duration / 1000,
1670
+ height: first.height,
1671
+ width: first.width,
1672
+ name: first.name
1673
+ };
1674
+ (_b = options.success) === null || _b === void 0 ? void 0 : _b.call(options, result);
1675
+ }
1676
+ (_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, ret);
1677
+ });
1678
+ };
1679
+ uni.getImageInfo = (options) => {
1680
+ const request = {
1681
+ src: options.src
1682
+ };
1683
+ JsBridgeHandlers.handleGetImageInfo(request).then((ret) => {
1684
+ var _a;
1685
+ const result = {
1686
+ width: ret.width,
1687
+ height: ret.height,
1688
+ path: ret.tempFilePath,
1689
+ type: ret.fileType,
1690
+ orientation: ''
1691
+ };
1692
+ (_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, result);
1693
+ });
1694
+ };
1695
+ UniProxy.pathToBase64 = function (path) {
1696
+ return new Promise((resolve, reject) => {
1697
+ JsBridgeHandlers.handlePathToBase64({ src: path }).then((result) => {
1698
+ if (result && result.startsWith('data:'))
1699
+ resolve(result);
1700
+ else
1701
+ reject(result);
1702
+ });
1703
+ });
1704
+ };
1705
+ uni.uploadFile = (options) => {
1706
+ // 核心修复:防止 DataCloneError
1707
+ // iOS 的 postMessage 无法克隆包含 success/fail 等函数的对象
1708
+ // 我们必须提取出纯数据字段发送给原生
1709
+ const request = {
1710
+ url: options.url,
1711
+ filePath: options.filePath,
1712
+ name: options.name,
1713
+ formData: options.formData,
1714
+ header: options.header,
1715
+ timeout: options.timeout
1716
+ };
1717
+ JsBridgeHandlers.handleUploadFile(request).then((ret) => {
1718
+ var _a, _b, _c;
1719
+ if (ret.code === 200) {
1720
+ (_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, {
1721
+ data: ret.data || '',
1722
+ statusCode: ret.statusCode || 200
1723
+ });
1724
+ }
1725
+ else {
1726
+ (_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: ret.errMsg || '上传失败' });
1727
+ }
1728
+ (_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: ret.errMsg || '' });
1729
+ });
1730
+ //TODO 假的,不支持
1731
+ return {
1732
+ abort: () => { },
1733
+ onHeadersReceived: () => { },
1734
+ offHeadersReceived: () => { },
1735
+ onProgressUpdate: () => { },
1736
+ offProgressUpdate: () => { }
1737
+ };
1738
+ };
1739
+ // 这里不能直接赋值,后面uni-simple-router会重写uni.navigateBack,导致此处赋值失效,所以用setTimeout延时赋值就ok了
1740
+ setTimeout(() => {
1741
+ // 原始的navigateBack
1742
+ const navigateBack = uni.navigateBack;
1743
+ uni.navigateBack = (options) => {
1744
+ const pages = getCurrentPages();
1745
+ if (pages && pages.length > 1) {
1746
+ navigateBack(options);
1747
+ }
1748
+ else {
1749
+ JsBridgeHandlers.handleCloseWebView();
1750
+ }
1751
+ };
1752
+ }, 100);
1753
+ uni.makePhoneCall = (options) => {
1754
+ const request = {
1755
+ phoneNumber: options.phoneNumber
1756
+ };
1757
+ JsBridgeHandlers.handleMakePhoneCall(request).then((ret) => {
1758
+ var _a, _b, _c;
1759
+ if (ret)
1760
+ (_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, ret);
1761
+ else
1762
+ (_b = options.success) === null || _b === void 0 ? void 0 : _b.call(options, null);
1763
+ (_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, null);
1764
+ });
1765
+ };
1766
+ uni.setNavigationBarTitle = (options) => {
1767
+ const request = {
1768
+ title: options.title
1769
+ };
1770
+ JsBridgeHandlers.handleSetNavigationBar(request).then((ret) => {
1771
+ var _a, _b;
1772
+ (_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, null);
1773
+ (_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, null);
1774
+ });
1775
+ };
1776
+ uni.setNavigationBarColor = (options) => {
1777
+ const request = {
1778
+ frontColor: options.frontColor,
1779
+ backgroundColor: options.backgroundColor
1780
+ };
1781
+ JsBridgeHandlers.handleSetNavigationBar(request).then((ret) => {
1782
+ var _a, _b;
1783
+ (_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, null);
1784
+ (_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, null);
1785
+ });
1786
+ };
1787
+ }
1788
+
1344
1789
  /**
1345
1790
  * JSBridge SDK
1346
1791
  * 统一的原生能力封装
@@ -1649,8 +2094,11 @@ const Debug = {
1649
2094
 
1650
2095
  exports.Debug = Debug;
1651
2096
  exports.JSBridge = JSBridge;
2097
+ exports.JsBridgeHandlers = JsBridgeHandlers;
1652
2098
  exports.Monitor = Monitor;
2099
+ exports.UniProxy = UniProxy;
1653
2100
  exports.getPlatform = getPlatform;
2101
+ exports.hdH5Adapter = hdH5Adapter;
1654
2102
  exports.inApp = inApp;
1655
2103
  exports.init = init;
1656
2104
  exports.isReady = isReady;