index.js 3.8 KB

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