App.vue 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <script>
  2. import { mapState, mapMutations } from 'vuex';
  3. import BaseTool from '@/utils/tool';
  4. // 需要登录才能访问的 tabBar 页面路径(首页允许未登录浏览)
  5. const TAB_NEED_LOGIN = ['/pages/operate/operate', '/pages/middle/middle', '/pages/tidings/tidings', '/pages/my/my'];
  6. // 免登录可查看的普通页面白名单(扫码查看设备详情等场景)
  7. const PUBLIC_PAGES = ['/pages/equipment-detail/equipment-detail'];
  8. export default {
  9. mixins: [{
  10. onShow() {
  11. // 全局页面鉴权:未登录时记录来源页面并跳转登录
  12. const pages = getCurrentPages();
  13. if (!pages || pages.length === 0) return;
  14. const page = pages[pages.length - 1];
  15. const route = '/' + page.route;
  16. // 登录页、微信授权回调页不做鉴权,不记录回跳
  17. if (route === '/pages/login/login') return;
  18. if (route === '/pages/setting/wechat-oauth') return;
  19. // 首页、白名单页面允许未登录浏览,其他页面需要登录
  20. if (route === '/pages/home/home') return;
  21. if (PUBLIC_PAGES.indexOf(route) !== -1) return;
  22. if (!BaseTool.UserUtil.isLogin()) {
  23. // 记录当前页面路径(含参数),登录成功后回跳
  24. try {
  25. const opts = page.options || (page.$page && page.$page.options) || {};
  26. const query = Object.keys(opts)
  27. .map(k => k + '=' + encodeURIComponent(opts[k]))
  28. .join('&');
  29. const isTab = TAB_NEED_LOGIN.indexOf(route) !== -1;
  30. uni.setStorageSync('login_back_url', route + (query ? '?' + query : ''));
  31. uni.setStorageSync('login_back_is_tab', isTab);
  32. } catch (e) {}
  33. uni.navigateTo({ url: '/pages/login/login' });
  34. }
  35. }
  36. }],
  37. methods: {
  38. ...mapMutations(['_login', '_userInfo'])
  39. },
  40. onLaunch: function () {
  41. const token = uni.getStorageSync('token');
  42. if (token) {
  43. this._login(token);
  44. }
  45. const userInfo = uni.getStorageSync('userInfo');
  46. if (userInfo) {
  47. this._userInfo(userInfo);
  48. }
  49. // #ifdef MP-WEIXIN
  50. const updateManager = uni.getUpdateManager();
  51. updateManager.onCheckForUpdate(function (res) {
  52. // 请求完新版本信息的回调
  53. console.log(res.hasUpdate);
  54. });
  55. updateManager.onUpdateReady(function (res) {
  56. uni.showModal({
  57. title: '更新提示',
  58. content: '新版本已经准备好,是否重启应用?',
  59. success(res) {
  60. if (res.confirm) {
  61. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  62. updateManager.applyUpdate();
  63. }
  64. }
  65. });
  66. });
  67. updateManager.onUpdateFailed(function (res) {
  68. // 新的版本下载失败
  69. });
  70. // #endif
  71. // uni.navigateTo({
  72. // url:'pages/login/login'
  73. // })
  74. },
  75. onShow: function () {},
  76. onHide: function () {}
  77. };
  78. </script>
  79. <style lang="scss">
  80. @import 'uview-ui/index.scss';
  81. /*每个页面公共css */
  82. @import '/static/common/css/main.css';
  83. @import '/static/common/css/icon.css';
  84. @import '/static/common/css/state.css';
  85. </style>