index.js 3.4 KB

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