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