root 3 лет назад
Родитель
Сommit
0d8fa5ff0d
6 измененных файлов с 37 добавлено и 23 удалено
  1. 2 0
      .env.development
  2. 1 0
      .env.production
  3. 8 0
      src/api/user.js
  4. 12 16
      src/store/modules/user.js
  5. 9 6
      src/utils/request.js
  6. 5 1
      src/views/login/index.vue

+ 2 - 0
.env.development

@@ -3,3 +3,5 @@ ENV = 'development'
 
 # base api
 VUE_APP_BASE_API = ''
+VUE_APP_ACCESS_TOKEN_PREFIX = 'Bearer '
+

+ 1 - 0
.env.production

@@ -3,4 +3,5 @@ ENV = 'production'
 
 # base api
 VUE_APP_BASE_API = '/prod-api'
+ACCESS_TOKEN_PREFIX = 'Bearer '
 

+ 8 - 0
src/api/user.js

@@ -18,6 +18,14 @@ export function isLoginVerify(data) {
   })
 }
 
+export function getUserInfo(data) {
+  return request({
+    url: '/v1/oauth/info',
+    method: 'get',
+    data
+  })
+}
+
 // export function login(data) {
 //   return request({
 //     url: '/vue-element-admin/user/login',

+ 12 - 16
src/store/modules/user.js

@@ -1,7 +1,7 @@
-import { login, logout, getInfo, isLoginVerify } from '@/api/user'
+import { login, logout, getInfo, isLoginVerify, getUserInfo } from '@/api/user'
 import { getToken, setToken, removeToken } from '@/utils/auth'
 import router, { resetRouter } from '@/router'
-import { updateLoginAllInfo, clear } from '@/utils/localUserInfo'
+import { updateLoginAllInfo, clear, accessToken } from '@/utils/localUserInfo'
 
 const state = {
   token: getToken(),
@@ -65,20 +65,16 @@ const actions = {
       })
     })
   },
-  // user login
-  // login({ commit }, userInfo) {
-  //   const { username, password } = userInfo
-  //   return new Promise((resolve, reject) => {
-  //     login({ username: username.trim(), password: password }).then(response => {
-  //       const { data } = response
-  //       commit('SET_TOKEN', data.token)
-  //       setToken(data.token)
-  //       resolve()
-  //     }).catch(error => {
-  //       reject(error)
-  //     })
-  //   })
-  // },
+  getUserInfo({ commit }, data) {
+    return new Promise((resolve, reject) => {
+      getUserInfo(data).then(response => {
+        console.log(response)
+        resolve(response)
+      }).catch(error => {
+        reject(error)
+      })
+    })
+  },
 
   // get user info
   getInfo({ commit, state }) {

+ 9 - 6
src/utils/request.js

@@ -2,6 +2,7 @@ import axios from 'axios'
 import { MessageBox, Message } from 'element-ui'
 import store from '@/store'
 import { getToken } from '@/utils/auth'
+import { accessToken } from '@/utils/localUserInfo'
 
 // create an axios instance
 const service = axios.create({
@@ -9,17 +10,19 @@ const service = axios.create({
   // withCredentials: true, // send cookies when cross-domain requests
   timeout: 5000 // request timeout
 })
-
 // request interceptor
 service.interceptors.request.use(
   config => {
     // do something before request is sent
 
-    if (store.getters.token) {
-      // let each request carry token
-      // ['X-Token'] is a custom headers key
-      // please modify it according to the actual situation
-      config.headers['X-Token'] = getToken()
+    // if (store.getters.token) {
+    //   // let each request carry token
+    //   // ['X-Token'] is a custom headers key
+    //   // please modify it according to the actual situation
+    //   config.headers['X-Token'] = getToken()
+    // }
+    if (accessToken()) {
+      config.headers['Authorization'] = process.env.VUE_APP_ACCESS_TOKEN_PREFIX + accessToken()
     }
     return config
   },

+ 5 - 1
src/views/login/index.vue

@@ -196,12 +196,16 @@ export default {
           }
           this.$store.dispatch('user/login', loginData)
             .then((response) => {
-              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
+              // this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
               this.loading = false
               // getUserInfo
               // getBaseInfo
               // getData
               // 
+            }).then((response) => {
+                // getUserInfo
+               
+                return this.$store.dispatch('user/getUserInfo', {})
             })
             .catch(error => {
               this.refreshLoginVerifyStatus();