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.

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