app.js 3.4 KB

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