Pārlūkot izejas kodu

添加token鉴权

aaa 3 gadi atpakaļ
vecāks
revīzija
812f402d1f
2 mainītis faili ar 43 papildinājumiem un 39 dzēšanām
  1. 6 0
      src/plugins/modules/axios.js
  2. 37 39
      src/views/login/login.vue

+ 6 - 0
src/plugins/modules/axios.js

@@ -13,6 +13,12 @@ axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8";
 axios.defaults.headers.put["Content-Type"] = "application/json;charset=utf-8";
 axios.defaults.headers.delete["Content-Type"] = "application/json;charset=utf-8";
 
+// 添加token
+axios.defaults.headers.get["Authorization"] = getCookie(AUTH_TOKEN_FRONT);
+axios.defaults.headers.post["Authorization"] = getCookie(AUTH_TOKEN_FRONT);
+axios.defaults.headers.put["Authorization"] = getCookie(AUTH_TOKEN_FRONT);
+axios.defaults.headers.delete["Authorization"] = getCookie(AUTH_TOKEN_FRONT);
+
 let config = {
   baseURL: process.env.VUE_APP_BASE_API || process.env.apiUrl || "",
   timeout: 60 * 1000,

+ 37 - 39
src/views/login/login.vue

@@ -10,11 +10,7 @@
           <div class="login-description-none"></div>
           <div class="login-description-txt">后端管理平台</div>
         </div>
-        <a-form
-          :form="loginForm"
-          @submit="handleLoginFun"
-          style="width: 350px;"
-        >
+        <a-form :form="loginForm" @submit="handleLoginFun" style="width: 350px">
           <a-form-item>
             <a-input
               placeholder="账户"
@@ -55,7 +51,7 @@
             <a-button
               type="primary"
               html-type="submit"
-              style="width: 100%;margin-top: 40px;"
+              style="width: 100%; margin-top: 40px"
               >登录</a-button
             >
           </a-form-item>
@@ -69,21 +65,21 @@
 </template>
 
 <script>
-import { PROGRAM_Title, AUTH_TOKEN_FRONT } from '@/common/Constant';
+import { PROGRAM_Title, AUTH_TOKEN_FRONT } from "@/common/Constant";
+import { getCookie } from "@/utils/cookie";
 export default {
-  name: 'login',
+  name: "login",
   props: {},
   components: {},
   data() {
     return {
       loading: false, // 是否显示加载动画
-      logoImg: require('@/assets/image/logo.png'), // logo图
+      logoImg: require("@/assets/image/logo.png"), // logo图
       loginTitle: PROGRAM_Title, // 系统标题
-      loginForm: this.$form.createForm(this, { name: 'login' }),
+      loginForm: this.$form.createForm(this, { name: "login" })
     };
   },
   created() {
-    // this.setUserInfo({ name: '111', token: 'WFFFF' });
     this.initDataFun(); // 初始化数据
   },
   mounted() {},
@@ -102,80 +98,82 @@ export default {
           let params = {
             userName: values.loginUserName,
             password: values.loginPassword,
-            token: AUTH_TOKEN_FRONT,
+            token: AUTH_TOKEN_FRONT
           };
           this.$_http
             .get(this.$_API.INTERFACE_GET_USER_LOGIN, {
               auth: {
                 username: params.userName,
-                password: params.password,
-              },
+                password: params.password
+              }
             })
-            .then((res) => {
+            .then(res => {
               this.loading = false;
               if (res.data) {
+                this.setUserInfo(res.data);
+                console.log("--token---" + getCookie(AUTH_TOKEN_FRONT));
                 this.getEngineeringWorkList(res.data); // 获取工种,并存到vuex
               } else {
                 this.loginForm.setFields({
                   loginUserName: {
                     value: values.loginUserName,
-                    errors: [new Error('账号密码不正确')],
+                    errors: [new Error("账号密码不正确")]
                   },
                   loginPassword: {
                     value: values.loginPassword,
-                    errors: [new Error('账号密码不正确')],
-                  },
+                    errors: [new Error("账号密码不正确")]
+                  }
                 });
               }
+              
             })
             .catch(() => {
               this.loading = false;
               this.loginForm.setFields({
                 loginUserName: {
                   value: values.loginUserName,
-                  errors: [new Error('账号密码不正确')],
+                  errors: [new Error("账号密码不正确")]
                 },
                 loginPassword: {
                   value: values.loginPassword,
-                  errors: [new Error('账号密码不正确')],
-                },
+                  errors: [new Error("账号密码不正确")]
+                }
               });
             });
         }
       });
     },
     // 获取工种,并存到vuex
-    getEngineeringWorkList(userInfo) {
+    getEngineeringWorkList() {
       this.loading = true;
       this.$_http
         .get(this.$_API.INTERFACE_GET_ENGINEERINGWORK_LIST)
-        .then((res) => {
-          this.$store.commit('common/SET_ENGINEERING_WORK_LIST', res.data);
+        .then(res => {
+          this.$store.commit("common/SET_ENGINEERING_WORK_LIST", res.data);
           sessionStorage.setItem(
-            'ENGINEERING_WORK_LIST',
+            "ENGINEERING_WORK_LIST",
             JSON.stringify(res.data)
           );
-          this.getExamQuestionTypeConditionParentList(userInfo); // 获取试题类型-父类信息,并存到vuex
+          this.getExamQuestionTypeConditionParentList(); // 获取试题类型-父类信息,并存到vuex
         })
         .catch(() => {
           this.loading = false;
-          this.setUserInfo(userInfo); // 设置用户信息,跳转到首页
+           // 设置用户信息,跳转到首页
         });
     },
     // 获取试题类型-父类信息,并存到vuex
-    getExamQuestionTypeConditionParentList(userInfo) {
+    getExamQuestionTypeConditionParentList() {
       this.$_http
         .get(this.$_API.INTERFACE_GET_EXAMS_QUESTION_TYPE_CONDITION_PARENT)
-        .then((res) => {
+        .then(res => {
           this.$store.commit(
-            'common/SET_EXAM_QUESTION_TYPE_CONDITION_PARENT',
+            "common/SET_EXAM_QUESTION_TYPE_CONDITION_PARENT",
             res.data
           );
           sessionStorage.setItem(
-            'EXAM_QUESTION_TYPE_CONDITION_PARENT',
+            "EXAM_QUESTION_TYPE_CONDITION_PARENT",
             JSON.stringify(res.data)
           );
-          this.setUserInfo(userInfo); // 设置用户信息,跳转到首页
         })
         .catch(() => {
           this.loading = false;
@@ -183,12 +181,12 @@ export default {
     },
     // 设置用户信息,跳转到首页
     setUserInfo(userInfo) {
-      this.$store.commit('user/SET_LOGIN', userInfo); // // 存用户信息
-      this.$store.commit('user/SET_MENU', []); // 赋值菜单
+      this.$store.commit("user/SET_LOGIN", userInfo); // // 存用户信息
+      this.$store.commit("user/SET_MENU", []); // 赋值菜单
       this.loading = false;
-      this.$router.push({ name: 'home' });
-    },
-  },
+      this.$router.push({ name: "home" });
+    }
+  }
 };
 </script>
 
@@ -202,12 +200,12 @@ export default {
 </style>
 
 <style lang="less" scoped>
-@import '~@/styles/common/variable.less';
+@import "~@/styles/common/variable.less";
 .login-box {
   .login-body {
     width: 100vw;
     height: 100vh;
-    background-image: url('../../assets/image/login/background.png');
+    background-image: url("../../assets/image/login/background.png");
     background-repeat: no-repeat;
     background-size: cover;
     display: flex;