main.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import Vue from 'vue'
  2. import App from './App'
  3. import store from './store' // store
  4. import plugins from './plugins' // plugins
  5. import './permission' // permission
  6. import {refreshToken} from "@/api/login";
  7. // import uView from 'uview-ui'
  8. import uView from '@/uni_modules/uview-ui'
  9. import $ from '@/pages/common/fun.js'
  10. import {
  11. getRefreshToken,
  12. getExpiresTime,
  13. setToken
  14. } from '@/utils/auth'
  15. // 在main.js中注册全局组件
  16. import isLodingModel from './components/loading/loading.vue'
  17. Vue.component('isLodingModel', isLodingModel)
  18. // import navInfo from "@/pagesA/components/my-nav/nav.vue"
  19. // Vue.component('navInfo', navInfo)
  20. Vue.use(uView)
  21. Vue.use(plugins)
  22. Vue.config.productionTip = false
  23. Vue.prototype.$store = store
  24. Vue.prototype.$ = $
  25. Vue.prototype.$screenHeight = uni.getSystemInfoSync().windowHeight
  26. App.mpType = 'app'
  27. const app = new Vue({
  28. ...App
  29. })
  30. app.$mount()
  31. var rsing=0
  32. rs()
  33. let isrefresh=false
  34. function rs(){
  35. const nowTime = new Date().getTime()
  36. if (rsing==0 && nowTime > (getExpiresTime()-10*1000)&& !isrefresh) {
  37. refreshToken({
  38. refreshToken: getRefreshToken()
  39. }).then((res) => {
  40. setToken(res.data)
  41. isrefresh = true; // 设置标志变量为 true
  42. rsing=0
  43. setTimeout(function() {
  44. isrefresh = false;
  45. }, 11 * 1000)
  46. }).catch(() => {
  47. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  48. if (res.confirm) {
  49. uni.navigateTo({
  50. url: '/pages/login'
  51. })
  52. }
  53. })
  54. })
  55. }
  56. setTimeout(function(){
  57. rs()
  58. },1*1000)
  59. }
  60. // 判断是否断网的函数
  61. function isOffline() {
  62. return !navigator.onLine;
  63. }
  64. // 监听断网事件
  65. window.addEventListener('offline', function() {
  66. if (isOffline()) {
  67. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then((res) => {
  68. if (res.confirm) {
  69. uni.navigateTo({
  70. url: '/pages/login',
  71. });
  72. }
  73. });
  74. }
  75. });
  76. // #ifdef H5
  77. Vue.mixin({
  78. mounted() {
  79. if (this.isWeiXinBrowser() && !this.isCustomNav()) {
  80. this.navTitle()
  81. }
  82. },
  83. methods: {
  84. isWeiXinBrowser() {
  85. let ua = navigator.userAgent.toLowerCase()
  86. return ua.indexOf('micromessenger') != -1
  87. },
  88. isCustomNav() {
  89. return this.$mp && this.$mp.appOptions && this.$mp.appOptions.window && this.$mp.appOptions.window.navigationStyle === 'custom';
  90. },
  91. navTitle() {
  92. this.$nextTick(() => {
  93. let navTitleDom = document.getElementsByTagName('uni-page-head')
  94. if (navTitleDom.length) {
  95. navTitleDom[0].style.display = 'none'
  96. if (typeof wx !== 'undefined' && wx.hideNavigationBarLoading) {
  97. wx.hideNavigationBarLoading()
  98. }
  99. }
  100. })
  101. }
  102. }
  103. })
  104. // #endif