app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const WXAPI = require('apifm-wxapi')
  2. const CONFIG = require('config.js')
  3. const AUTH = require('utils/auth')
  4. App({
  5. onLaunch: function() {
  6. const subDomain = wx.getExtConfigSync().subDomain
  7. const componentAppid = wx.getExtConfigSync().componentAppid
  8. if (componentAppid) {
  9. wx.setStorageSync('appid', wx.getAccountInfoSync().miniProgram.appId)
  10. wx.setStorageSync('componentAppid', componentAppid)
  11. }
  12. if (subDomain) {
  13. WXAPI.init(subDomain)
  14. } else {
  15. WXAPI.init(CONFIG.subDomain)
  16. }
  17. const that = this;
  18. // 检测新版本
  19. const updateManager = wx.getUpdateManager()
  20. updateManager.onUpdateReady(function () {
  21. wx.showModal({
  22. title: '更新提示',
  23. content: '新版本已经准备好,是否重启应用?',
  24. success(res) {
  25. if (res.confirm) {
  26. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  27. updateManager.applyUpdate()
  28. }
  29. }
  30. })
  31. })
  32. /**
  33. * 初次加载判断网络情况
  34. * 无网络状态下根据实际情况进行调整
  35. */
  36. wx.getNetworkType({
  37. success(res) {
  38. const networkType = res.networkType
  39. if (networkType === 'none') {
  40. that.globalData.isConnected = false
  41. wx.showToast({
  42. title: '当前无网络',
  43. icon: 'loading',
  44. duration: 2000
  45. })
  46. }
  47. }
  48. });
  49. /**
  50. * 监听网络状态变化
  51. * 可根据业务需求进行调整
  52. */
  53. wx.onNetworkStatusChange(function(res) {
  54. if (!res.isConnected) {
  55. that.globalData.isConnected = false
  56. wx.showToast({
  57. title: '网络已断开',
  58. icon: 'loading',
  59. duration: 2000
  60. })
  61. } else {
  62. that.globalData.isConnected = true
  63. wx.hideToast()
  64. }
  65. })
  66. WXAPI.queryConfigBatch('mallName,WITHDRAW_MIN,ALLOW_SELF_COLLECTION,order_hx_uids,subscribe_ids,share_profile,adminUserIds,goodsDetailSkuShowType,shopMod,needIdCheck,balance_pay_pwd').then(res => {
  67. if (res.code == 0) {
  68. res.data.forEach(config => {
  69. wx.setStorageSync(config.key, config.value);
  70. })
  71. if (this.configLoadOK) {
  72. this.configLoadOK()
  73. }
  74. }
  75. })
  76. },
  77. onShow (e) {
  78. // 保存邀请人
  79. if (e && e.query && e.query.inviter_id) {
  80. wx.setStorageSync('referrer', e.query.inviter_id)
  81. if (e.shareTicket) {
  82. wx.getShareInfo({
  83. shareTicket: e.shareTicket,
  84. success: res => {
  85. wx.login({
  86. success(loginRes) {
  87. if (loginRes.code) {
  88. WXAPI.shareGroupGetScore(
  89. loginRes.code,
  90. e.query.inviter_id,
  91. res.encryptedData,
  92. res.iv
  93. ).then(_res => {
  94. console.log(_res)
  95. }).catch(err => {
  96. console.error(err)
  97. })
  98. } else {
  99. console.error('登录失败!' + loginRes.errMsg)
  100. }
  101. }
  102. })
  103. }
  104. })
  105. }
  106. }
  107. // 自动登录
  108. AUTH.checkHasLogined().then(isLogined => {
  109. if (!isLogined) {
  110. AUTH.login()
  111. }
  112. })
  113. },
  114. globalData: {
  115. isConnected: true,
  116. sdkAppID: CONFIG.sdkAppID
  117. }
  118. })