App.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <script setup>
  2. import loginMethods from '@/utils/login'
  3. import permissionListener from '@/utils/authorize/permissionListener.js'
  4. import { initLog } from '@/utils/flow'
  5. import { useThemeStore } from '@/stores/theme'
  6. const themeStore = useThemeStore()
  7. onLaunch(async () => {
  8. // 初始化埋点请求数据
  9. initLog()
  10. // eslint-disable-next-line no-console, no-restricted-syntax
  11. console.log('mall4j.v240506')
  12. // #ifdef H5
  13. uni.getSystemInfo({
  14. success: function ({ platform }) {
  15. // 客户端平台,值域为:ios、android
  16. if (platform === 'ios') {
  17. uni.setStorageSync('bbcIosUrl', window.location.href.split('#')[0])
  18. }
  19. }
  20. })
  21. // 判断浏览器环境
  22. const ua = navigator.userAgent.toLowerCase()
  23. if (ua.search(/MicroMessenger/i) > -1 && !uni.getStorageSync('bbcAppType')) {
  24. // 微信环境
  25. uni.setStorageSync('bbcAppType', AppType.MP)
  26. } else {
  27. if (!uni.getStorageSync('bbcAppType')) {
  28. uni.setStorageSync('bbcAppType', AppType.H5)
  29. }
  30. }
  31. // #endif
  32. // 获取系统支付设置
  33. util.getSysPaySwitch(true)
  34. // 获取网站配置
  35. useWebConfigStore().getUniWebConfig()
  36. // 获取通联支付设置
  37. util.getAllinpayConfig(1)
  38. // #ifdef MP-WEIXIN
  39. uni.setStorageSync('bbcAppType', AppType.MINI)
  40. // 微信小程序检查升级
  41. checkMiniUpdate()
  42. // #endif
  43. // APP模式下保持竖屏
  44. // #ifdef APP-PLUS
  45. plus.screen.lockOrientation('portrait-primary')
  46. uni.getSystemInfo({
  47. success: (sysInfo) => {
  48. if (sysInfo.platform === 'android') {
  49. uni.setStorageSync('bbcAppType', AppType.ANDROID)
  50. } else {
  51. uni.setStorageSync('bbcAppType', AppType.IOS)
  52. }
  53. }
  54. })
  55. // #endif
  56. // #ifdef MP-WEIXIN || H5
  57. setTimeout(() => {
  58. loginMethods.weChatLogin()
  59. })
  60. // #endif
  61. uni.getSystemInfo({
  62. success: function (res) {
  63. // px转换到rpx的比例
  64. const pxToRpxScale = 750 / res.windowWidth
  65. const systems = {
  66. ktxStatusHeight: res.statusBarHeight * pxToRpxScale, // 状态栏的高度
  67. navigationHeight: 44 * pxToRpxScale, // 导航栏的高度
  68. ktxWindowWidth: res.windowWidth * pxToRpxScale, // window的宽度
  69. ktxWindowHeight: res.windowHeight * pxToRpxScale, // window的高度
  70. ktxScreentHeight: res.screenHeight * pxToRpxScale // 屏幕的高度
  71. }
  72. const app = createApp()
  73. app.config.globalProperties.$system = systems
  74. app.config.globalProperties.$lineHeight = systems.ktxStatusHeight + systems.navigationHeight
  75. provide('$system', systems)
  76. provide('$lineHeight', systems.ktxStatusHeight + systems.navigationHeight)
  77. }
  78. })
  79. // 全局变量
  80. nextTick(() => initGlobalData())
  81. // 请求购物车数据
  82. useCartCountStore().updateCartCount()
  83. // 初始化主题
  84. if (!themeStore.themeColor || themeStore.themeColor === '#F81A1A') {
  85. await themeStore.init()
  86. }
  87. })
  88. onShow((options) => {
  89. permissionListener?.listenerFunc()
  90. // #ifdef MP-WEIXIN
  91. // 通联支付从通联微信小程序返回后接收的参数
  92. if (uni.getStorageSync('bbcPaySettlementType') !== 1) {
  93. // 非通联支付模式下不处理
  94. return
  95. }
  96. if (options.referrerInfo && options.referrerInfo.extraData) {
  97. let backMerchantUrl = options.referrerInfo.extraData.backMerchantUrl
  98. if (backMerchantUrl) {
  99. backMerchantUrl = decodeURIComponent(backMerchantUrl)
  100. const result = backMerchantUrl.split('?')[1].split('&')
  101. const query = {}
  102. for (let i = 0; i < result.length; i++) {
  103. const item = result[i].split('=')
  104. query[item[0]] = item[1]
  105. }
  106. if (Number(query.isSignSuccess) === 1 && query.bizContent) {
  107. // 获取内存中的签约编号
  108. const acctProtocolNo = uni.getStorageSync('bbcAllinpayAcctProtocolNo')
  109. const bizContent = JSON.parse(decodeURIComponent(query.bizContent))
  110. if (bizContent.acctProtocolNo !== acctProtocolNo) {
  111. uni.showToast({
  112. title: $t('signSuccess'),
  113. icon: 'none'
  114. })
  115. uni.setStorageSync('bbcAllinpayAcctProtocolNo', bizContent.acctProtocolNo)
  116. }
  117. } else if (Number(query.isSignSuccess) === 0) {
  118. uni.showToast({
  119. title: $t('signFail'),
  120. icon: 'none'
  121. })
  122. }
  123. }
  124. }
  125. // #endif
  126. })
  127. onHide(() => {
  128. permissionListener?.stopFunc()
  129. })
  130. // 页面不存在时触发
  131. onPageNotFound(() => {
  132. uni.reLaunch({
  133. url: '/pages/index/index'
  134. })
  135. })
  136. const initGlobalData = () => {
  137. const appType = uni.getStorageSync('bbcAppType')
  138. // 全局请求队列
  139. getApp().globalData.requestQueue = []
  140. // 是否正在进行登录
  141. getApp().globalData.isLanding = false
  142. // 购物车商品数量
  143. getApp().globalData.totalCartCount = 0
  144. // 当前请求数量
  145. getApp().globalData.currentReqCounts = 0
  146. // 是否重新加载页面数据
  147. getApp().globalData.isRefreshPage = false
  148. // 直播间socket任务
  149. getApp().globalData.imSocketTask = null
  150. getApp().globalData.reloadHomePopupAd = true // 是否重新加载首页弹窗
  151. getApp().globalData.reloadPageData = false // 是否重新加载页面数据
  152. // 悟空im设备标识 0.app 1.其他;相同用户相同设备标记的主设备登录会互相踢
  153. getApp().globalData.wukongDeviceFlag = appType === AppType.ANDROID || appType === AppType.IOS ? 0 : 1
  154. }
  155. /**
  156. * 微信小程序检查升级
  157. */
  158. const checkMiniUpdate = () => {
  159. const updateManager = wx.getUpdateManager()
  160. updateManager.onUpdateReady(function () {
  161. wx.showModal({
  162. title: '更新提示',
  163. content: '新版本已经准备好,是否重启应用?',
  164. success: function (res) {
  165. if (res.confirm) {
  166. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  167. updateManager.applyUpdate()
  168. }
  169. }
  170. })
  171. })
  172. updateManager.onUpdateFailed(function () {
  173. // 新版本下载失败
  174. uni.showToast({
  175. title: '新版本下载失败,请重试',
  176. duration: 2000,
  177. icon: 'none'
  178. })
  179. })
  180. }
  181. </script>
  182. <!-- eslint-disable-next-line vue-scoped-css/enforce-style-type -->
  183. <style>
  184. /*每个页面公共样式 */
  185. /* #ifndef APP-PLUS-NVUE */
  186. @import url('./app.css');
  187. @import url('./theme/index.css');
  188. /* #endif */
  189. @import "@/static/iconfont/iconfont.css";
  190. </style>