app.js 5.2 KB

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