app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. onHide: function () {
  85. let {
  86. cookieIDs
  87. } = this.globalData;
  88. let ids = undefined;
  89. console.log(cookieIDs)
  90. if(cookieIDs){
  91. if (cookieIDs.length > 9) {
  92. cookieIDs.length = 10
  93. }
  94. ids = cookieIDs.join(",") || undefined;
  95. }
  96. let loginSessionKey = wx.getStorageSync("token") || "";
  97. if (loginSessionKey) {
  98. wx.request({
  99. url: serverName + '/wx/api/user/saveBrowsedExhibitions',
  100. data: {
  101. ids:ids,
  102. loginSessionKey
  103. },
  104. header: {
  105. 'content-type': 'application/x-www-form-urlencoded'
  106. },
  107. method: "post",
  108. success: (res) => {
  109. }
  110. })
  111. } else {
  112. return
  113. }
  114. },
  115. globalData: {
  116. userInfo: null,
  117. city: "",
  118. collectedArr: [],
  119. collectedChange: false,
  120. clickToSelect: false,
  121. isLogin: false,
  122. cookieIDs:[],
  123. currentUrl:'',
  124. currentShareImg:''
  125. }
  126. })