app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // debugger
  90. if (code) {
  91. const result = await wxUserLogin(code)
  92. console.log('result', result)
  93. if (result.code === 0) {
  94. const {
  95. loginSessionKey,
  96. sessionKey,
  97. token
  98. } = result.data
  99. wx.setStorageSync('loginSessionKey', loginSessionKey)
  100. wx.setStorageSync('token', token)
  101. wx.setStorageSync('sessionKey', sessionKey)
  102. const res = await getWxUserInfo(sessionKey)
  103. const userInfo = wx.getStorageInfoSync('userInfo')
  104. if (res.data) {
  105. const mergeObj = {
  106. ...userInfo,
  107. ...res.data
  108. }
  109. wx.setStorageSync('userInfo', mergeObj)
  110. callback && callback(mergeObj);
  111. }
  112. }
  113. }
  114. }
  115. })
  116. },
  117. //应用关闭的函数
  118. onHide: function () {
  119. let {
  120. cookieIDs
  121. } = this.globalData;
  122. let ids = undefined;
  123. console.log(cookieIDs)
  124. if (cookieIDs) {
  125. if (cookieIDs.length > 9) {
  126. cookieIDs.length = 10
  127. }
  128. ids = cookieIDs.join(",") || undefined;
  129. }
  130. let loginSessionKey = wx.getStorageSync("token") || "";
  131. //关闭应用的时候发送你浏览过的场景
  132. if (loginSessionKey) {
  133. wx.request({
  134. url: serverName + '/wx/api/user/saveBrowsedExhibitions',
  135. data: {
  136. ids: ids,
  137. loginSessionKey
  138. },
  139. header: {
  140. 'content-type': 'application/x-www-form-urlencoded'
  141. },
  142. method: "post",
  143. success: (res) => {}
  144. })
  145. } else {
  146. return
  147. }
  148. },
  149. globalData: {
  150. userInfo: null,
  151. city: "",
  152. collectedArr: [],
  153. collectedChange: false,
  154. clickToSelect: false,
  155. isLogin: false,
  156. cookieIDs: [],
  157. currentUrl: '',
  158. currentShareImg: '',
  159. envVersion: "trial"
  160. }
  161. })
  162. let app = getApp()
  163. export default app