Browse Source

fix:路由前置守卫

sunChengjie 11 months ago
parent
commit
75e9b79a71
1 changed files with 31 additions and 29 deletions
  1. 31 29
      src/main.js

+ 31 - 29
src/main.js

@@ -36,21 +36,21 @@ Vue.prototype.clipboard = clipboard
 
 // 时间戳过滤器
 Vue.filter('dateFormat', (dataStr) => {
-  var time = new Date(dataStr)
+    var time = new Date(dataStr)
 
-  function timeAdd0(str) {
-    if (str < 10) {
-      str = '0' + str
+    function timeAdd0(str) {
+        if (str < 10) {
+            str = '0' + str
+        }
+        return str
     }
-    return str
-  }
-  var y = time.getFullYear()
-  var m = time.getMonth() + 1
-  var d = time.getDate()
-  var h = time.getHours()
-  var mm = time.getMinutes()
-  var s = time.getSeconds()
-  return y + '-' + timeAdd0(m) + '-' + timeAdd0(d) + ' ' + timeAdd0(h) + ':' + timeAdd0(mm) + ':' + timeAdd0(s)
+    var y = time.getFullYear()
+    var m = time.getMonth() + 1
+    var d = time.getDate()
+    var h = time.getHours()
+    var mm = time.getMinutes()
+    var s = time.getSeconds()
+    return y + '-' + timeAdd0(m) + '-' + timeAdd0(d) + ' ' + timeAdd0(h) + ':' + timeAdd0(mm) + ':' + timeAdd0(s)
 })
 
 
@@ -64,25 +64,27 @@ Vue.filter('dateFormat', (dataStr) => {
 
 // })
 /* eslint-disable no-new */
+// 判断userid和openid,userid没有的情况下让他直接去登录,openid没有的情况下让他只走授权
+// 路由前置守卫
 router.beforeEach((to, from, next) => {
-  if (to.matched.some(record => record.meta.needLogin)) { // 判断该路由是否需要登录权限
-    //登录判断
-
-    if (localStorage.getItem('userId')) { // 判断当前用户的登录信息loginInfo是否存在
-      next();
+    if (to.matched.some(record => record.meta.needLogin)) { // 判断该路由是否需要登录权限
+        //登录判断
+        if (!localStorage.getItem('userId')) { // 判断当前用户的登录信息loginInfo是否存在
+            next('/login')
+        } else if (!localStorage.getItem('openid')) {
+            this.getOpenid()
+        } else {
+            next()
+        }
     } else {
-      next({
-        path: '/Login'
-      })
+        next();
     }
-  } else {
-    next();
-  }
 })
+
 new Vue({
-  el: '#app',
-  router,
-  store: store,
-  components: { App },
-  template: '<App/>'
+    el: '#app',
+    router,
+    store: store,
+    components: { App },
+    template: '<App/>'
 })