app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. require("./utils/string.js")
  2. const ald = require('./libs/ald-stat.js')
  3. import {
  4. updataStorageData
  5. } from './utils/storage.js'
  6. import {
  7. getWxCode,
  8. hasAuth,
  9. getUserInfo
  10. } from './utils/wx.js'
  11. import {
  12. wxLogin,
  13. saveLogin,
  14. checkPhone
  15. } from './services/wx.js'
  16. var startTime = Date.now();//启动时间
  17. App({
  18. onLaunch: function (options) {
  19. this.aldstat.sendEvent('小程序的启动时长', {
  20. time: Date.now() - startTime
  21. })
  22. // 小程序版本更新
  23. if (wx.getUpdateManager) {
  24. const updateManager = wx.getUpdateManager()
  25. updateManager.onCheckForUpdate(function (res) {
  26. // 请求完新版本信息的回调
  27. console.log(res.hasUpdate)
  28. })
  29. updateManager.onUpdateReady(function () {
  30. wx.showModal({
  31. title: '更新提示',
  32. content: '新版本已经准备好,是否重启应用?',
  33. success(res) {
  34. if (res.confirm) {
  35. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  36. updateManager.applyUpdate()
  37. }
  38. }
  39. })
  40. })
  41. updateManager.onUpdateFailed(function () {
  42. // 新版本下载失败
  43. })
  44. } else {
  45. wx.showModal({
  46. title: '提示',
  47. content: '当前微信版本过低,部分功能可能无法使用,请升级到最新微信版本后重试。'
  48. })
  49. }
  50. // let sceneArr = [1007, 1008, 1044, 1011, 1012, 1013, 1047, 1048, 1049]
  51. let shareToken = options.query.shareToken || null
  52. getWxCode().then(code => {
  53. return wxLogin({
  54. code: code,
  55. shareToken: shareToken
  56. })
  57. })
  58. .then(res => {
  59. console.log("dd", res)
  60. this.globalData.openId = res.data.openId ? res.data.openId : '';
  61. this.globalData.sessionKey = res.data.sessionKey ? res.data.sessionKey : '';
  62. return hasAuth('scope.userInfo');
  63. })
  64. .catch(err => {
  65. console.log("没用获取用户信息权限")
  66. wx.navigateTo({
  67. url: '/pages/login/login',
  68. })
  69. return
  70. })
  71. .then(data => {
  72. return getUserInfo()
  73. })
  74. .then(res => {
  75. console.log("ee", res)
  76. if (!updataStorageData('city')) {
  77. updataStorageData('city', res.userInfo.city)
  78. }
  79. saveLogin({
  80. encryptedData: encodeURIComponent(res.encryptedData),
  81. iv: encodeURIComponent(res.iv),
  82. openid: this.globalData.openId,
  83. session_key: this.globalData.sessionKey
  84. }).then(data => {
  85. console.log("ff", data)
  86. this.globalData.userId = data.data.hpUser.id ? data.data.hpUser.id : '';
  87. this.globalData.userToken = data.data.hpUser.userToken ? data.data.hpUser.userToken : '';
  88. updataStorageData('shareToken', data.data.hpUser.userToken || '') //用户识别码
  89. wx.setStorageSync("hpuser", data.data.hpUser);
  90. //在获取用户信息之后,再返回userInfo,防止首页 onLoad先执行
  91. this.globalData.userInfo = res.userInfo;
  92. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  93. // 所以此处加入 callback 以防止这种情况
  94. if (this.userInfoReadyCallback) {
  95. this.userInfoReadyCallback(res)
  96. }
  97. }).catch(data => {
  98. console.log("gg", data)
  99. })
  100. })
  101. .catch(err => {
  102. console.log(err)
  103. })
  104. },
  105. onShow(options) {
  106. console.log("rr", options)
  107. //群聊信息
  108. if (options.shareTicket) {
  109. this.getShareInfo(options.shareTicket)
  110. }
  111. },
  112. globalData: {
  113. openId: '',
  114. userInfo: null,
  115. userId: '',
  116. userToken: '',
  117. sessionKey: '',
  118. city: updataStorageData('city') || '无锡'
  119. },
  120. //页面分享
  121. onShareAppMessage: function () {
  122. wx.showShareMenu({
  123. withShareTicket: true,
  124. success: (res) => { // 成功后要做的事情
  125. console.log(res)
  126. },
  127. fail: function (res) {
  128. console.log(res)
  129. }
  130. })
  131. },
  132. //获取群聊分享信息
  133. getShareInfo(shareTicket) {
  134. wx.getShareInfo({
  135. shareTicket: shareTicket,
  136. success: (res) => {
  137. //需后台解析数据 encryptedData iv
  138. console.log(res)
  139. },
  140. fail: function (res) {
  141. console.log(res)
  142. },
  143. complete: function (res) { }
  144. })
  145. },
  146. // 跳转登录页面
  147. goLogin() {
  148. wx.navigateTo({
  149. url: '/pages/login/login',
  150. })
  151. },
  152. })