app.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. require("./utils/string.js")
  2. require('./utils/push_sdk.js')
  3. require('./libs/ald-stat.js')
  4. import {
  5. updataStorageData
  6. } from './utils/storage.js'
  7. import {
  8. getWxCode,
  9. } from './utils/wx.js'
  10. import {
  11. wxLogin,
  12. } from './services/wx.js'
  13. var startTime = Date.now(); //启动时间
  14. App({
  15. onLaunch: function (options) {
  16. // 设置城市列表
  17. this.setAreaList();
  18. this.aldstat.sendEvent('小程序的启动时长', {
  19. time: Date.now() - startTime
  20. })
  21. getWxCode().then(code => {
  22. return wxLogin({
  23. code: code
  24. })
  25. })
  26. .then(res => {
  27. wx.aldPushSendOpenid(res.data.openId)
  28. })
  29. },
  30. onShow(options) {
  31. //群聊信息
  32. if (options.shareTicket) {
  33. this.getShareInfo(options.shareTicket)
  34. }
  35. // 小程序版本更新
  36. if (wx.getUpdateManager) {
  37. const updateManager = wx.getUpdateManager()
  38. updateManager.onCheckForUpdate((res) => {
  39. // 请求完新版本信息的回调
  40. if (res.hasUpdate) {
  41. updateManager.onUpdateReady(() => {
  42. wx.showModal({
  43. title: '更新提示',
  44. content: '新版本已经准备好,是否重启应用?',
  45. success: (res) => {
  46. if (res.confirm) {
  47. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  48. wx.removeStorageSync("user_id");
  49. wx.removeStorageSync("member_id");
  50. wx.removeStorageSync("user_token");
  51. wx.removeStorageSync("userInfo");
  52. this.globalData.userId = '';
  53. this.globalData.userToken = '';
  54. this.globalData.memberId = '';
  55. this.globalData.userInfo = null;
  56. updateManager.applyUpdate()
  57. }
  58. }
  59. })
  60. })
  61. }
  62. })
  63. updateManager.onUpdateFailed(function () {
  64. // 新版本下载失败
  65. })
  66. } else {
  67. wx.showModal({
  68. title: '提示',
  69. content: '当前微信版本过低,部分功能可能无法使用,请升级到最新微信版本后重试。'
  70. })
  71. }
  72. // wx.onAppRoute((data) => {
  73. // const route = getCurrentPages();
  74. // const page = route[route.length - 1];
  75. // if (!page.data.hasShare) {
  76. // let _str = '';
  77. // for (let key in data.query) {
  78. // _str += `${key}=${data.query[key]}&&`
  79. // }
  80. // const shareTitle = this.shareTitle[page.route]
  81. // page.onShareAppMessage = function (e) {
  82. // return {
  83. // path: `/${page.route}?${_str}`,
  84. // imageUrl: page.data.shareImageUrl || '/images/happyjob/detail_share.jpg',
  85. // title: shareTitle || '开心工作'
  86. // }
  87. // }.bind(page)
  88. // }
  89. // });
  90. const date = wx.getStorageSync('detail_time');
  91. const _date = new Date().getTime();
  92. if (_date - date < 86400000) {
  93. wx.setStorageSync('detail_times', 0)
  94. }
  95. },
  96. shareTitle: {
  97. },
  98. globalData: {
  99. openId: '',
  100. userInfo: null,
  101. userId: '',
  102. userToken: '',
  103. memberId: "",
  104. sessionKey: '',
  105. city: updataStorageData('city') || '无锡'
  106. },
  107. //页面分享
  108. onShareAppMessage: function () {
  109. wx.showShareMenu({
  110. withShareTicket: true,
  111. success: (res) => { // 成功后要做的事情
  112. console.log(res)
  113. },
  114. fail: function (res) {
  115. console.log(res)
  116. }
  117. })
  118. },
  119. //获取群聊分享信息
  120. getShareInfo(shareTicket) {
  121. wx.getShareInfo({
  122. shareTicket: shareTicket,
  123. success: (res) => {
  124. //需后台解析数据 encryptedData iv
  125. console.log(res)
  126. },
  127. fail: function (res) {
  128. console.log(res)
  129. },
  130. complete: function (res) { }
  131. })
  132. },
  133. // 跳转登录页面
  134. goLogin() {
  135. wx.navigateTo({
  136. url: '/pages/login/login',
  137. })
  138. },
  139. setAreaList() {
  140. if (!wx.getStorageSync('areaList')) {
  141. wx.setStorageSync('areaList', [{
  142. "countryCode": "",
  143. "countryName": "不限",
  144. "firstNameLetter": "X"
  145. }, {
  146. "countryCode": "320205",
  147. "countryName": "锡山区",
  148. "firstNameLetter": "X"
  149. },
  150. {
  151. "countryCode": "320206",
  152. "countryName": "惠山区",
  153. "firstNameLetter": "H"
  154. },
  155. {
  156. "countryCode": "320211",
  157. "countryName": "滨湖区",
  158. "firstNameLetter": "B"
  159. },
  160. {
  161. "countryCode": "320213",
  162. "countryName": "梁溪区",
  163. "firstNameLetter": "L"
  164. },
  165. {
  166. "countryCode": "320214",
  167. "countryName": "新吴区",
  168. "firstNameLetter": "X"
  169. },
  170. {
  171. "countryCode": "320281",
  172. "countryName": "江阴市",
  173. "firstNameLetter": "J"
  174. },
  175. {
  176. "countryCode": "320282",
  177. "countryName": "宜兴市",
  178. "firstNameLetter": "Y"
  179. }
  180. ])
  181. }
  182. }
  183. })