app.js 5.2 KB

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