app.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //app.js
  2. const {
  3. request,
  4. serverName
  5. } = require('./utils/services');
  6. import {
  7. getWxUserInfo,
  8. wxUserLogin
  9. } from './utils/request'
  10. App({
  11. onLaunch: function () {
  12. console.log('wx', wx)
  13. // 展示本地存储能力
  14. var logs = wx.getStorageSync('logs') || []
  15. logs.unshift(Date.now())
  16. wx.setStorageSync('logs', logs)
  17. this.autoLogin();
  18. let loginSessionKey = wx.getStorageSync("token") || "";
  19. if (loginSessionKey) {
  20. wx.request({
  21. url: serverName + '/wx/api/user/getBrowsedExhibitions',
  22. data: {
  23. loginSessionKey
  24. },
  25. header: {
  26. 'content-type': 'application/x-www-form-urlencoded'
  27. },
  28. method: "post",
  29. success: (res) => {
  30. if (res.data.code == 0) {
  31. let cookieIds = res.data.data.ids || undefined
  32. if (cookieIds) {
  33. this.globalData.cookieIDs = cookieIds.split(',');
  34. }
  35. } else {
  36. return
  37. }
  38. }
  39. })
  40. } else {}
  41. // 获取用户信息
  42. wx.getSetting({
  43. success: res => {
  44. if (res.authSetting['scope.userInfo']) {
  45. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  46. wx.getUserInfo({
  47. success: res => {
  48. // 可以将 res 发送给后台解码出 unionId
  49. this.globalData.userInfo = res.userInfo
  50. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  51. // 所以此处加入 callback 以防止这种情况
  52. if (this.userInfoReadyCallback) {
  53. this.userInfoReadyCallback(res)
  54. }
  55. }
  56. })
  57. }
  58. }
  59. })
  60. const updateManager = wx.getUpdateManager()
  61. updateManager.onCheckForUpdate((res) => {
  62. // 请求完新版本信息的回调
  63. console.log(res.hasUpdate)
  64. })
  65. updateManager.onUpdateReady(() => {
  66. wx.showModal({
  67. title: '更新提示',
  68. content: '新版本已经准备好,是否重启应用?',
  69. success: function (res) {
  70. if (res.confirm) {
  71. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  72. updateManager.applyUpdate()
  73. }
  74. }
  75. })
  76. })
  77. updateManager.onUpdateFailed(() => {
  78. // 新的版本下载失败
  79. })
  80. },
  81. onShow() {
  82. },
  83. autoLogin(callback) {
  84. wx.login({
  85. success: async res => {
  86. let {
  87. code
  88. } = res;
  89. if (code) {
  90. const result = await wxUserLogin(code)
  91. console.log('result', result)
  92. if (result.code === 0) {
  93. const {
  94. loginSessionKey,
  95. sessionKey
  96. } = result.data
  97. wx.setStorageSync('token', loginSessionKey)
  98. wx.setStorageSync('sessionKey', sessionKey)
  99. if (sessionKey) {
  100. const res = await getWxUserInfo(sessionKey)
  101. const userInfo = wx.getStorageInfoSync('userInfo')
  102. if (res.data) {
  103. const mergeObj = {
  104. ...userInfo,
  105. ...res.data
  106. }
  107. wx.setStorageSync('userInfo', mergeObj)
  108. }
  109. }
  110. }
  111. }
  112. }
  113. })
  114. },
  115. //应用关闭的函数
  116. onHide: function () {
  117. let {
  118. cookieIDs
  119. } = this.globalData;
  120. let ids = undefined;
  121. console.log(cookieIDs)
  122. if (cookieIDs) {
  123. if (cookieIDs.length > 9) {
  124. cookieIDs.length = 10
  125. }
  126. ids = cookieIDs.join(",") || undefined;
  127. }
  128. let loginSessionKey = wx.getStorageSync("token") || "";
  129. //关闭应用的时候发送你浏览过的场景
  130. if (loginSessionKey) {
  131. wx.request({
  132. url: serverName + '/wx/api/user/saveBrowsedExhibitions',
  133. data: {
  134. ids: ids,
  135. loginSessionKey
  136. },
  137. header: {
  138. 'content-type': 'application/x-www-form-urlencoded'
  139. },
  140. method: "post",
  141. success: (res) => {}
  142. })
  143. } else {
  144. return
  145. }
  146. },
  147. globalData: {
  148. userInfo: null,
  149. city: "",
  150. collectedArr: [],
  151. collectedChange: false,
  152. clickToSelect: false,
  153. isLogin: false,
  154. cookieIDs: [],
  155. currentUrl: '',
  156. currentShareImg: ''
  157. }
  158. })
  159. let app = getApp()
  160. export default app