刘洋 1 년 전
부모
커밋
f683106d43
2개의 변경된 파일9개의 추가작업 그리고 4개의 파일을 삭제
  1. 1 4
      src/main.js
  2. 8 0
      src/router/permission.js

+ 1 - 4
src/main.js

@@ -7,7 +7,6 @@ import uView from 'uview-ui'
 
 
 // 此处为演示vuex使用,非uView的功能部分
 // 此处为演示vuex使用,非uView的功能部分
 import store from '@/store'
 import store from '@/store'
-import { pcLogin } from './api/user'
 // 引入uView对小程序分享的mixin封装
 // 引入uView对小程序分享的mixin封装
 const mpShare = require('uview-ui/libs/mixin/mpShare.js')
 const mpShare = require('uview-ui/libs/mixin/mpShare.js')
 // 引入uView提供的对vuex的简写法文件
 // 引入uView提供的对vuex的简写法文件
@@ -25,9 +24,7 @@ Vue.config.productionTip = false
 
 
 // 小程序页面组件和这个 App.vue 组件的写法和引入方式是一致的,为了区分两者,需要设置mpType值
 // 小程序页面组件和这个 App.vue 组件的写法和引入方式是一致的,为了区分两者,需要设置mpType值
 App.mpType = 'app'
 App.mpType = 'app'
-pcLogin().then((res) => {
-  uni.setStorageSync('user', res)
-})
+
 const app = new Vue({
 const app = new Vue({
   store,
   store,
   ...App
   ...App

+ 8 - 0
src/router/permission.js

@@ -1,4 +1,5 @@
 import store from '@/store'
 import store from '@/store'
+import { pcLogin } from '@/api/user'
 
 
 export default (router) => {
 export default (router) => {
   // 路由白名单,可以考虑不同环境配置不同白名单
   // 路由白名单,可以考虑不同环境配置不同白名单
@@ -6,6 +7,13 @@ export default (router) => {
 
 
   // 全局路由前置守卫
   // 全局路由前置守卫
   router.beforeEach(async (to, from, next) => {
   router.beforeEach(async (to, from, next) => {
+    if (!uni.getStorageSync('user')) {
+      let res = await pcLogin().catch(() => {})
+      if (res) {
+        uni.setStorageSync('user', res)
+      }
+    }
+
     next() //小程序登录接口还没有出来,先放开路由拦截器
     next() //小程序登录接口还没有出来,先放开路由拦截器
     return
     return
     const token = store.state.vuex_access_token
     const token = store.state.vuex_access_token