app.js 3.8 KB

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