index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Shopro 第三方平台功能聚合
  3. * @version 1.0.3
  4. * @author lidongtony
  5. * @param {String} name - 厂商+平台名称
  6. * @param {String} provider - 厂商
  7. * @param {String} platform - 平台名称
  8. * @param {String} os - 系统型号
  9. * @param {Object} device - 设备信息
  10. */
  11. import { isEmpty } from 'lodash';
  12. // #ifdef H5
  13. import { isWxBrowser } from '@/sheep/helper/utils';
  14. // #endif
  15. import wechat from './provider/wechat/index.js';
  16. import apple from './provider/apple';
  17. import share from './share';
  18. import Pay from './pay';
  19. const device = uni.getSystemInfoSync();
  20. const os = device.platform;
  21. let name = '';
  22. let provider = '';
  23. let platform = '';
  24. let isWechatInstalled = true;
  25. // #ifdef H5
  26. if (isWxBrowser()) {
  27. name = 'WechatOfficialAccount';
  28. provider = 'wechat';
  29. platform = 'officialAccount';
  30. } else {
  31. name = 'H5';
  32. platform = 'h5';
  33. }
  34. // #endif
  35. // #ifdef APP-PLUS
  36. name = 'App';
  37. platform = 'openPlatform';
  38. // 检查微信客户端是否安装,否则AppleStore会因此拒绝上架
  39. if (os === 'ios') {
  40. isWechatInstalled = plus.ios.import('WXApi').isWXAppInstalled();
  41. }
  42. // #endif
  43. // #ifdef MP-WEIXIN
  44. name = 'WechatMiniProgram';
  45. platform = 'miniProgram';
  46. provider = 'wechat';
  47. // #endif
  48. if (isEmpty(name)) {
  49. uni.showToast({
  50. title: '暂不支持该平台',
  51. icon: 'none',
  52. });
  53. }
  54. // 加载当前平台前置行为
  55. const load = () => {
  56. if (provider === 'wechat') {
  57. wechat.load();
  58. }
  59. };
  60. // 使用厂商独占sdk name = 'wechat' | 'alipay' | 'apple'
  61. const useProvider = (_provider = '') => {
  62. if (_provider === '') _provider = provider;
  63. if (_provider === 'wechat') return wechat;
  64. if (_provider === 'apple') return apple;
  65. };
  66. // 支付服务转发
  67. const pay = (payment, orderType, orderSN, orderType2) => {
  68. return new Pay(payment, orderType, orderSN, orderType2);
  69. };
  70. /**
  71. * 检查更新 (只检查小程序和App)
  72. * @param {Boolean} silence - 静默检查
  73. */
  74. const checkUpdate = (silence = false) => {
  75. let canUpdate;
  76. // #ifdef MP-WEIXIN
  77. useProvider().checkUpdate(silence);
  78. // #endif
  79. // #ifdef APP-PLUS
  80. // TODO: 热更新
  81. // #endif
  82. };
  83. /**
  84. * 检查网络
  85. * @param {Boolean} silence - 静默检查
  86. */
  87. async function checkNetwork() {
  88. const networkStatus = await uni.getNetworkType();
  89. if (networkStatus.networkType == 'none') {
  90. return Promise.resolve(false);
  91. }
  92. return Promise.resolve(true);
  93. }
  94. // 获取小程序胶囊信息
  95. const getCapsule = () => {
  96. // #ifdef MP
  97. let capsule = uni.getMenuButtonBoundingClientRect();
  98. if (!capsule) {
  99. capsule = {
  100. bottom: 56,
  101. height: 32,
  102. left: 278,
  103. right: 365,
  104. top: 24,
  105. width: 87,
  106. };
  107. }
  108. return capsule;
  109. // #endif
  110. // #ifndef MP
  111. return {
  112. bottom: 56,
  113. height: 32,
  114. left: 278,
  115. right: 365,
  116. top: 24,
  117. width: 87,
  118. };
  119. // #endif
  120. };
  121. const capsule = getCapsule();
  122. // 标题栏高度
  123. const getNavBar = () => {
  124. return device.statusBarHeight + 44;
  125. };
  126. const navbar = getNavBar();
  127. function getLandingPage() {
  128. let page = '';
  129. // #ifdef H5
  130. page = location.href.split('?')[0];
  131. // #endif
  132. return page;
  133. }
  134. // 设置ios+公众号网页落地页 解决微信sdk签名问题
  135. const landingPage = getLandingPage();
  136. const _platform = {
  137. name,
  138. device,
  139. os,
  140. provider,
  141. platform,
  142. useProvider,
  143. checkUpdate,
  144. checkNetwork,
  145. pay,
  146. share,
  147. load,
  148. capsule,
  149. navbar,
  150. landingPage,
  151. isWechatInstalled,
  152. };
  153. export default _platform;