lilei 4 rokov pred
rodič
commit
b5bc7b5e3b
3 zmenil súbory, kde vykonal 44 pridanie a 55 odobranie
  1. 4 3
      src/api/login.js
  2. 14 20
      src/store/modules/user.js
  3. 26 32
      src/utils/request.js

+ 4 - 3
src/api/login.js

@@ -3,15 +3,16 @@ import { axios } from '@/utils/request'
 
 export function login ({ username, password }) {
   return axios({
-    url: 'auth/login',
-    data: { username, password },
+    url: '/login',
+    headers: { 'content-type': 'application/x-www-form-urlencoded' },
+    data: `username=${username}&password=${password}`,
     method: 'post'
   })
 }
 
 export function logout () {
   return axios({
-    url: 'auth/logout',
+    url: 'logout',
     method: 'get'
   })
 }

+ 14 - 20
src/store/modules/user.js

@@ -39,28 +39,22 @@ const user = {
     Login ({ commit }, userInfo) {
       console.log(userInfo, 'userInfo----')
       return new Promise((resolve, reject) => {
-        login(userInfo).then(ret => {
-          console.log(ret, 'login success')
-          if (ret.status == 200) {
-            const res = ret.data
-            if (res.access_token && res.auth_user) {
-              const users = res.auth_user
-              Vue.ls.set('hasError', 0)
-              commit('SET_TOKEN', res.access_token)
-              commit('SET_NAME', { name: users.userNameCN, welcome: welcome() })
-              Vue.ls.set('rolesAccess', { permissionList: users.permCodes }, 7 * 24 * 60 * 60 * 1000)
-              commit('SET_AVATAR', res.avatar ? res.avatar : '')
-              // 记住密码
-              if (userInfo.rememberMe) {
-                VueCookies.set('REMEMBER_ME', JSON.stringify(userInfo))
-              } else {
-                VueCookies.remove('REMEMBER_ME')
-              }
+        login(userInfo).then(res => {
+          console.log(res, 'login success')
+          if (res.userId) {
+            Vue.ls.set('hasError', 0)
+            commit('SET_TOKEN', res.userId)
+            commit('SET_NAME', { name: res.userRealname, welcome: welcome() })
+            commit('SET_AVATAR', res.avatar ? res.avatar : '')
+            Vue.ls.set('rolesAccess', { permissionList: res.permCodes }, 7 * 24 * 60 * 60 * 1000)
+            // 记住密码
+            if (userInfo.rememberMe) {
+              VueCookies.set('REMEMBER_ME', JSON.stringify(userInfo))
+            } else {
+              VueCookies.remove('REMEMBER_ME')
             }
-            resolve(res)
-          } else {
-            reject(ret)
           }
+          resolve()
         }).catch(error => {
           reject(error)
         })

+ 26 - 32
src/utils/request.js

@@ -1,11 +1,10 @@
-// import Vue from 'vue'
+import Vue from 'vue'
 import axios from 'axios'
 import store from '@/store'
 import notification from 'ant-design-vue/es/notification'
-import modal from 'ant-design-vue/es/modal'
 import { VueAxios } from './axios'
-// import { ACCESS_TOKEN } from '@/store/mutation-types'
-// console.log(process.env.VUE_APP_PRO_NAME)
+import { ACCESS_TOKEN } from '@/store/mutation-types'
+console.log(process.env.VUE_APP_PRO_NAME)
 // 创建 axios 实例
 const service = axios.create({
   baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
@@ -13,62 +12,57 @@ const service = axios.create({
 })
 
 const err = (error) => {
-  console.log(error.response.data.message, 'error')
   if (error.response) {
-    const status = error.response.status
-    if ((status == 503 || status == 500) && window.location.pathname != '/user/login') {
-      notification.destroy()
+    const data = error.response.data
+    if ((error.response.status == 403 || error.response.status == 401) && window.location.pathname != '/user/login') {
       notification.error({
         message: '提示',
-        description: error.response.data.message
+        description: data.message
       })
+      setTimeout(function () {
+        store.dispatch('Logout').then(() => {
+          window.location.reload()
+        })
+      }, 2000)
     }
   }
   // 超时无法访问服务
   if (error.message.indexOf('timeout') >= 0 && window.location.pathname != '/user/login') {
-    notification.destroy()
     notification.error({
       message: '提示',
-      description: '请求超时'
+      description: error.message
     })
+    setTimeout(function () {
+      store.dispatch('Logout').then(() => {
+        window.location.reload()
+      })
+    }, 2000)
   }
   return Promise.reject(error)
 }
 
 // request interceptor
 service.interceptors.request.use(config => {
-  const token = store.getters.token
-  config.headers['App-Type'] = 9 // 平台类型
+  const token = Vue.ls.get(ACCESS_TOKEN)
   if (token) {
-    config.headers['authorization'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
+    // config.headers['Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
   }
   return config
 }, err)
 
 // response interceptor
 service.interceptors.response.use((response) => {
-  // console.log(response, 'response.data.status')
-  const et = sessionStorage.getItem('errorTips')
-  if (response.data.status == '1001' && !et) {
-    sessionStorage.setItem('errorTips', 1)
-    modal.destroyAll()
-    modal.error({
-      title: '提示',
-      content: response.data.message,
-      onOk () {
-        sessionStorage.setItem('errorTips', 0)
-        store.dispatch('Logout').then(() => {
-          window.location.reload()
-        })
-      }
-    })
-  }
-  if ((response.data.status == '401' || response.data.status == '900' || response.data.status == '500') && window.location.pathname != '/user/login') {
-    notification.destroy()
+  console.log(response, 'responseresponseresponse')
+  if (response.data.status == '-1') {
     notification.error({
       message: '提示',
       description: response.data.message
     })
+    setTimeout(function () {
+      store.dispatch('Logout').then(() => {
+        window.location.reload()
+      })
+    }, 2000)
   }
   return response.data
 }, err)