C端集团版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.5 KiB

  1. //app.js
  2. const config = require("./config/config.js");
  3. const Http = require("./utils/HttpBasics");
  4. App({
  5. onLaunch: function (options) {
  6. var that = this;
  7. // 展示本地存储能力
  8. var logs = wx.getStorageSync('logs') || []
  9. logs.unshift(Date.now())
  10. wx.setStorageSync('logs', logs)
  11. /**
  12. * 小程序版本更新
  13. */
  14. that.autoUpdate();
  15. /**
  16. * 用户登录
  17. */
  18. that.userLogin(options.scene);
  19. },
  20. //登陆
  21. userLogin(sceneAddress) {
  22. console.log(sceneAddress)
  23. let that = this;
  24. // 登录
  25. wx.login({
  26. success: ({
  27. code
  28. }) => {
  29. let usrdata = {
  30. appId: config.weapp.AppId,
  31. code: code,
  32. sceneAddress: sceneAddress
  33. }
  34. Http.post({
  35. url: config.api.login,
  36. data: usrdata
  37. })
  38. .then(res => {
  39. wx.setStorageSync('openId',res.data.openId)
  40. wx.setStorageSync('subMalls', JSON.parse(res.data.subMalls) )
  41. if (res.data && res.data.score) {
  42. if (res.data.score != 0) {
  43. that.globalData.score = res.data.score;
  44. }
  45. }
  46. Http.setToken(res.data.token);
  47. console.log(Http.headers.token,"headers.token")
  48. that.globalData.token = res.data.token;
  49. if (that.tokenCallback) {
  50. that.tokenCallback(res.data.token);
  51. }
  52. })
  53. .catch(err => {
  54. console.log(err)
  55. wx.showModal({
  56. title: '提示',
  57. showCancel: false,
  58. content: '登录失败,请重新尝试',
  59. success: function(res) {
  60. if (res.cancel) {
  61. //点击取消,默认隐藏弹框
  62. } else {
  63. //点击确定
  64. wx.reLaunch({
  65. url: '/pages/index/index',
  66. })
  67. }
  68. }
  69. })
  70. })
  71. }
  72. })
  73. },
  74. autoUpdate: function() {
  75. let that = this;
  76. if (wx.canIUse('getUpdateManager')) {
  77. const updateManager = wx.getUpdateManager()
  78. // 1. 检查小程序是否有新版本发布
  79. updateManager.onCheckForUpdate(function(res) {
  80. // 请求完新版本信息的回调
  81. if (res.hasUpdate) {
  82. // 检测到新版本,需要更新,给出提示
  83. wx.showModal({
  84. title: '更新提示',
  85. content: '检测到新版本,是否下载新版本并重启小程序?',
  86. success: function (res) {
  87. if (res.confirm) {
  88. //2. 用户确定下载更新小程序,小程序下载及更新静默进行
  89. that.downLoadAndUpdate(updateManager)
  90. } else if (res.cancel) {
  91. //用户点击取消按钮的处理,如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
  92. wx.showModal({
  93. title: '温馨提示~',
  94. content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
  95. showCancel: false,//隐藏取消按钮
  96. confirmText: "确定更新",//只保留确定更新按钮
  97. success: function (res) {
  98. if (res.confirm) {
  99. //下载新版本,并重新应用
  100. that.downLoadAndUpdate(updateManager)
  101. }
  102. }
  103. })
  104. }
  105. }
  106. })
  107. }
  108. })
  109. } else {
  110. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  111. wx.showModal({
  112. title: '提示',
  113. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  114. })
  115. }
  116. },
  117. /**
  118. * 下载小程序新版本并重启应用
  119. */
  120. downLoadAndUpdate: function(updateManager) {
  121. let that = this
  122. wx.showLoading();
  123. //静默下载更新小程序新版本
  124. updateManager.onUpdateReady(function() {
  125. wx.hideLoading()
  126. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  127. updateManager.applyUpdate()
  128. })
  129. updateManager.onUpdateFailed(function() {
  130. // 新的版本下载失败
  131. wx.showModal({
  132. title: '已经有新版本了哟~',
  133. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  134. })
  135. })
  136. },
  137. globalData: {
  138. userInfo: null,
  139. score: 0,
  140. token: 0,
  141. }
  142. })