app.js 4.2 KB

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