Browse Source

使用了 VUE_APP_BASE_API 环境变量作为请求基本地址

dusenyao 4 years ago
parent
commit
4001ff5512
8 changed files with 25 additions and 9 deletions
  1. 1 1
      .env.development
  2. 1 1
      .env.production
  3. 3 0
      .env.staging
  4. 1 1
      src/api/table.js
  5. 1 1
      src/api/user.js
  6. 16 2
      src/utils/request.js
  7. 0 1
      src/views/teacher/main/TaskKanban.vue
  8. 2 2
      vue.config.js

+ 1 - 1
.env.development

@@ -2,4 +2,4 @@
 ENV = 'development'
 
 # base api
-VUE_APP_BASE_API = '/dev-api'
+VUE_APP_BASE_API = '/api'

+ 1 - 1
.env.production

@@ -2,4 +2,4 @@
 ENV = 'production'
 
 # base api
-VUE_APP_BASE_API = '/prod-api'
+VUE_APP_BASE_API = ''

+ 3 - 0
.env.staging

@@ -2,3 +2,6 @@ NODE_ENV = production
 
 # just a flag
 ENV = 'staging'
+
+# base api
+VUE_APP_BASE_API = ''

+ 1 - 1
src/api/table.js

@@ -2,7 +2,7 @@ import request from '@/utils/request';
 
 export function getMyCsItemsDateTeacher(Parameter) {
   return request({
-    url: '/api/ServiceInterface',
+    url: '/ServiceInterface',
     method: 'post',
     params: {
       MethodName: 'teaching-cs_item_manager-GetMyCSItems_Date_Teacher',

+ 1 - 1
src/api/user.js

@@ -2,7 +2,7 @@ import request from '@/utils/request';
 
 export function login(params) {
   return request({
-    url: '/api/ServiceInterface',
+    url: '/ServiceInterface',
     method: 'post',
     params
   });

+ 16 - 2
src/utils/request.js

@@ -1,3 +1,5 @@
+import store from '@/store';
+import { getToken } from '@/utils/auth';
 import axios from 'axios';
 import { Message } from 'element-ui';
 
@@ -8,11 +10,23 @@ axios.defaults.headers['Content-Type'] = 'application/json';
 axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest';
 
 const service = axios.create({
-  timeout: 10000
+  baseURL: process.env.VUE_APP_BASE_API,
+  // withCredentials: true, // 跨域请求时发送 cookies
+  timeout: 5000
 });
 
 // 请求拦截器
-service.interceptors.request.use({});
+service.interceptors.request.use(
+  config => {
+    if (store.getters.token) {
+      config.headers['X-Token'] = getToken();
+    }
+    return config;
+  },
+  error => {
+    return Promise.reject(error);
+  }
+);
 
 //响应拦截器
 service.interceptors.response.use(

+ 0 - 1
src/views/teacher/main/TaskKanban.vue

@@ -72,7 +72,6 @@ export default {
   },
   mounted() {
     getMyCsItemsDateTeacher({ Date_Stamp: formatDate(this.date) }).then(response => {
-      console.log(response.CSItem_List);
       this.CSItemList = response.CSItem_List;
     });
   },

+ 2 - 2
vue.config.js

@@ -15,11 +15,11 @@ let proxy = {};
 
 if (NODE_ENV === 'development') {
   proxy = {
-    '/api': {
+    [process.env.VUE_APP_BASE_API]: {
       target: 'http://localhost:20088/GCLSLearnWebSI/',
       changeOrigin: true,
       pathRewrite: {
-        '^/api': ''
+        ['^' + process.env.VUE_APP_BASE_API]: ''
       }
     }
   };