app.js 5.9 KB

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