Bladeren bron

单点登录

duanxiaoduan 1 jaar geleden
bovenliggende
commit
e3741a9d1a
4 gewijzigde bestanden met toevoegingen van 73 en 37 verwijderingen
  1. 4 4
      src/permission.js
  2. 36 15
      src/store/modules/user.js
  3. 1 0
      src/utils/auth.js
  4. 32 18
      src/views/login/index.vue

+ 4 - 4
src/permission.js

@@ -19,7 +19,8 @@ router.beforeEach(async(to, from, next) => {
 
   // determine whether the user has logged in
   const hasToken = getToken()
-
+  
+    console.log('hasToken==>',hasToken)
   if (hasToken) {
     if (to.path === '/login') {
       // if is logged in, redirect to the home page
@@ -72,12 +73,11 @@ router.beforeEach(async(to, from, next) => {
       next({ path: '/login' })
       NProgress.done()
        // next({ path: '/login' })
-      console.log(123)
       // 构建要跳转的URL
-       const externalURL = "https://id.xiandaimuye.com/api/v1/oauth2/authorize?response_type=code&client_id=fTBm64I4k3kqHYtoFTUpvirCDxxCfx7I&redirect_uri=http://tmrwatch.cn:8082";
+       // const externalURL = "https://id.xiandaimuye.com/api/v1/oauth2/authorize?response_type=code&client_id=fTBm64I4k3kqHYtoFTUpvirCDxxCfx7I&redirect_uri=http://tmrwatch.cn:8082/api/v1/oauth2/token";
       // 使用 $router.push 进行页面跳转
       // 注意: 这里的跳转是在当前窗口进行的,如果需要在新标签页打开,可以使用 window.open(externalURL)
-      window.open(externalURL, "_self");
+      // window.open(externalURL, "_self");
     }
   }
 })

+ 36 - 15
src/store/modules/user.js

@@ -99,21 +99,42 @@ const actions = {
     const { username, password } = userInfo
     return new Promise((resolve, reject) => {
       login({ username: username.trim(), password: password }).then(response => {
-        const { data, msg } = response
-        if (msg !== 'ok') {
-          Message({
-            message: data,
-            type: 'error',
-            duration: 5 * 1000
-          })
-          reject(data)
-        }
-        commit('SET_TOKEN', data.token)
-        if (Cookies == null) {
-          console.log(1)
-        }
-        setToken(data.token)
-        resolve()
+        // =============================
+        const fullURL = window.location.href;
+        // 使用正则表达式提取code参数的值
+        const codeMatch = fullURL.match(/[\?&]access=([^&]+)/);
+        if (codeMatch) {
+          // 如果匹配成功,将code的值存储在组件的data中
+          this.code = codeMatch[1];
+            //TODO 保存token
+            // 去除后面的#/login
+          if ( this.code &&  this.code.includes("#/login")) {
+            this.code =  this.code.replace("#/login", "");
+          }
+          console.log('token',this.code)
+           //base64解密this.cose
+            console.log('解密token',atob(this.code))
+            let token = atob(this.code)
+            commit('SET_TOKEN', token)
+            setToken(token)
+            resolve()
+          }
+        // =============================
+        // const { data, msg } = response
+        // if (msg !== 'ok') {
+        //   Message({
+        //     message: data,
+        //     type: 'error',
+        //     duration: 5 * 1000
+        //   })
+        //   reject(data)
+        // }
+        // commit('SET_TOKEN', data.token)
+        // if (Cookies == null) {
+        //   console.log(1)
+        // }
+        // setToken(data.token)
+        // resolve()
       }).catch(error => {
         reject(error)
       })

+ 1 - 0
src/utils/auth.js

@@ -7,6 +7,7 @@ export function getToken() {
 }
 
 export function setToken(token) {
+  // console.log(token,'token')
   return Cookies.set(TokenKey, token)
 }
 

+ 32 - 18
src/views/login/index.vue

@@ -41,6 +41,9 @@
 <script>
 import { validUsername } from '@/utils/validate'
 import { getJson } from '@/api/common'
+import Cookies from 'js-cookie'
+import axios from 'axios';
+import { setToken } from '@/utils/auth' // get token from cookie
 export default {
   name: 'Login',
   data() {
@@ -130,24 +133,35 @@ export default {
     //   })
     // }
     getcodeList(){
-      // 获取完整的URL
-      const fullURL = window.location.href;
-      // 使用正则表达式提取code参数的值
-      const codeMatch = fullURL.match(/[\?&]code=([^&]+)/);
-      if (codeMatch) {
-        // 如果匹配成功,将code的值存储在组件的data中
-        this.code = codeMatch[1];
-        let url = 'http://tmrwatch.cn:8082/api/v1/oauth2/token'
-        let data = '?code='+this.code
-        console.log('logincode====>',this.code)
-        getJson(url,data).then(response => {
-          console.log(response)
-          //TODO 保存token
-        })
-      } else {
-        console.log('error submit!!')
-        return false
-      }
+      this.$store.dispatch('user/login', this.loginForm) .then(() => {
+        
+      }).catch(() => {
+        this.loading = false
+      })
+      // // 获取完整的URL
+      // const fullURL = window.location.href;
+      // // 使用正则表达式提取code参数的值
+      // const codeMatch = fullURL.match(/[\?&]access=([^&]+)/);
+      // if (codeMatch) {
+      //   // 如果匹配成功,将code的值存储在组件的data中
+      //   this.code = codeMatch[1];
+      //   console.log(this.code)
+      //     //TODO 保存token
+      //     // 去除后面的#/login
+      //   if ( this.code &&  this.code.includes("#/login")) {
+      //      this.code =  this.code.replace("#/login", "");
+      //   }
+      //    console.log(this.code)
+      //    //base64解密this.cose
+      //     // atob(this.code)
+      //     console.log( atob(this.code))
+      //     let token = atob(this.code)
+      //     setToken(token)
+          
+      // } else {
+      //   console.log('error submit!!')
+      //   return false
+      // }
     }
   }
 }