Browse Source

请求工具新增通用请求方法

yangxiao 3 years ago
parent
commit
ac809ce4b0
1 changed files with 11 additions and 0 deletions
  1. 11 0
      src/api/axios.js

+ 11 - 0
src/api/axios.js

@@ -10,6 +10,7 @@ import { Message } from 'element-ui';
 
   this.API.requestData({
     isMust: true, // 请求是否携带 token ,默认为 true ,可缺省
+    showLoading: false, // 请求是否显示加载中遮罩层,默认 false 可缺省
     method: "GET", // 请求方式,默认为 GET ,可缺省
     subUrl: "api/repassword", // 请求接口地址,必传项
     timeout: 3000, // 请求超时时间,默认 3s ,可缺省
@@ -26,6 +27,10 @@ import { Message } from 'element-ui';
 export function requestData (options) {
   return new Promise((resolve, reject) => {
 
+    if (options.showLoading) {
+      store.state.loading = true;
+    }
+
     // 包装请求头
     let headers = {
       'Content-Type': 'application/x-www-form-urlencoded',
@@ -62,12 +67,18 @@ export function requestData (options) {
       method: options.method || 'GET',
       params,
     }).then(response => {
+      if (options.showLoading) {
+        store.state.loading = false;
+      }
       if (response.code === 200) {
         options.success(resolve(response));
       } else {
         Message.error(response.message || ("请求出错[" + response.code + "]"));
       }
     }).catch(error => {
+      if (options.showLoading) {
+        store.state.loading = false;
+      }
       options.fail(reject(error));
     });
   });