App.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <script>
  2. const _ = require("lodash");
  3. import jwt from "jsonwebtoken";
  4. import {
  5. mapState,
  6. mapMutations,
  7. createNamespacedHelpers
  8. } from "vuex";
  9. export default {
  10. globalData: {
  11. version: plus.runtime.version
  12. },
  13. onLaunch: function() {
  14. // this.checkVersion();
  15. if (process.env.VUE_APP_PLATFORM === "app-plus") {
  16. // plus.screen.lockOrientation('portrait-secondary');
  17. plus.screen.lockOrientation("portrait-primary");
  18. }
  19. },
  20. onShow: function() {
  21. this.checkUser();
  22. // console.log('App Show')
  23. },
  24. onHide: function() {
  25. // console.log('App Hide')
  26. },
  27. methods: {
  28. // 检查用户
  29. async checkUser() {
  30. if (this.user.id) return;
  31. const token = uni.getStorageSync("token");
  32. if (!token) {
  33. uni.redirectTo({
  34. url: "/pages/login/index",
  35. });
  36. } else {
  37. const tokenObject = jwt.decode(token);
  38. if (tokenObject) {
  39. this.setUser(tokenObject);
  40. uni.redirectTo({
  41. url: "/pages/home/index",
  42. });
  43. }
  44. }
  45. },
  46. //检查版本
  47. checkVersion() {
  48. uni.request({
  49. method: "get",
  50. url: "http://broadcast.waityou24.cn/files/sps/appVersion.json",
  51. success: (res) => {
  52. const {
  53. version,
  54. url
  55. } = res.data;
  56. if (getApp().globalData.version === version) return;
  57. uni.showModal({
  58. title: "更新",
  59. content: "有新版本,是否下载?",
  60. success: (res) => {
  61. if (res.confirm) {
  62. // 下载app
  63. this.toUpdateAPK(url);
  64. }
  65. },
  66. });
  67. },
  68. });
  69. },
  70. // 更新
  71. toUpdateAPK(url) {
  72. if (!url) return;
  73. uni.showLoading({
  74. title: "更新中……",
  75. });
  76. uni.downloadFile({
  77. url,
  78. success: (res) => {
  79. uni.hideLoading();
  80. if (res.statusCode == 200) {
  81. uni.showModal({
  82. title: "",
  83. content: "更新成功,确定现在重启吗?",
  84. confirmText: "重启",
  85. confirmColor: "#EE8F57",
  86. success: (modalRes) => {
  87. if (modalRes.confirm) {
  88. plus.runtime.install(
  89. res.tempFilePath, {
  90. force: true
  91. },
  92. function(res) {
  93. plus.runtime.restart();
  94. }
  95. );
  96. }
  97. },
  98. });
  99. }
  100. },
  101. });
  102. // const task = plus.downloader.createDownload(url, {}, function (d, status) {
  103. // console.log(d, status);
  104. // if (status === 200) {
  105. // // 安装
  106. // plus.runtime.install(
  107. // plus.io.convertLocalFileSystemURL(d.filename, {}, {}, function (error) {
  108. // uni.showToast({
  109. // title: '更新失败',
  110. // mask: false,
  111. // })
  112. // })
  113. // )
  114. // }
  115. // });
  116. // task.start();
  117. },
  118. },
  119. computed: {
  120. ...mapState(["user"]),
  121. },
  122. };
  123. </script>
  124. <style lang="scss">
  125. @import "uview-ui/index.scss";
  126. @import "@/static/icon/iconfont.css";
  127. /*每个页面公共css */
  128. </style>