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.

131 lines
4.2 KiB

  1. let config = require("./config/config.js");
  2. const Http = require("./utils/http");
  3. import GlobalConfig from './config/index'
  4. const globalConfig = new GlobalConfig()
  5. globalConfig.init()
  6. App({
  7. onLaunch: function () {
  8. var that = this
  9. // 1. 自动更新
  10. that.autoUpdate()
  11. // 2. 获取当前位置坐标
  12. // wx.getLocation({
  13. // type: 'wgs84',
  14. // success: function(res) {
  15. // that.globalData.latitude = res.latitude
  16. // that.globalData.longitude = res.longitude
  17. // }
  18. // })
  19. // 3. 展示本地存储能力
  20. var logs = wx.getStorageSync('logs') || []
  21. logs.unshift(Date.now())
  22. wx.setStorageSync('logs', logs)
  23. // 获取用户信息
  24. wx.getSetting({
  25. success: res => {
  26. if (res.authSetting['scope.userInfo']) {
  27. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  28. wx.getUserInfo({
  29. success: res => {
  30. // 可以将 res 发送给后台解码出 unionId
  31. this.globalData.userInfo = res.userInfo
  32. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  33. // 所以此处加入 callback 以防止这种情况
  34. if (this.userInfoReadyCallback) {
  35. this.userInfoReadyCallback(res)
  36. }
  37. }
  38. })
  39. }
  40. }
  41. })
  42. },
  43. autoUpdate: function () {
  44. let that = this;
  45. if (wx.canIUse('getUpdateManager')) {
  46. const updateManager = wx.getUpdateManager()
  47. // 1. 检查小程序是否有新版本发布
  48. updateManager.onCheckForUpdate(function (res) {
  49. // 请求完新版本信息的回调
  50. if (res.hasUpdate) {
  51. // 检测到新版本,需要更新,给出提示
  52. wx.showModal({
  53. title: '更新提示',
  54. content: '检测到新版本,是否下载新版本并重启小程序?',
  55. success: function (res) {
  56. if (res.confirm) {
  57. //2. 用户确定下载更新小程序,小程序下载及更新静默进行
  58. that.downLoadAndUpdate(updateManager)
  59. } else if (res.cancel) {
  60. //用户点击取消按钮的处理,如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
  61. wx.showModal({
  62. title: '温馨提示~',
  63. content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
  64. showCancel: false,//隐藏取消按钮
  65. confirmText: "确定更新",//只保留确定更新按钮
  66. success: function (res) {
  67. if (res.confirm) {
  68. //下载新版本,并重新应用
  69. that.downLoadAndUpdate(updateManager)
  70. }
  71. }
  72. })
  73. }
  74. }
  75. })
  76. }
  77. })
  78. } else {
  79. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  80. wx.showModal({
  81. title: '提示',
  82. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  83. })
  84. }
  85. },
  86. /**
  87. * 下载小程序新版本并重启应用
  88. */
  89. downLoadAndUpdate: function (updateManager) {
  90. let that = this
  91. wx.showLoading();
  92. //静默下载更新小程序新版本
  93. updateManager.onUpdateReady(function () {
  94. wx.hideLoading()
  95. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  96. updateManager.applyUpdate()
  97. })
  98. updateManager.onUpdateFailed(function () {
  99. // 新的版本下载失败
  100. wx.showModal({
  101. title: '已经有新版本了哟~',
  102. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  103. })
  104. })
  105. },
  106. globalData: {
  107. mouldType: 0,//主题风格模板
  108. userInfo: '',
  109. superopenId: null,
  110. session_key: null,
  111. latitude: null,
  112. longitude: null,
  113. token: null,
  114. enddate: null,
  115. startdate: null,
  116. curHtml: '',
  117. config: globalConfig
  118. }
  119. })
  120. // 测试 wxfa116c4a29453fc9
  121. // 生产 wx7299c86d22d0ab42
  122. // 测试账号信息
  123. // 18872592633
  124. // 123456GD