Browse Source

解决登录页面请求错误时页面会崩掉的问题

zhuth 7 years ago
parent
commit
0011afa037
4 changed files with 10 additions and 38 deletions
  1. 2 2
      src/components/common/login/login.jsx
  2. 2 2
      src/models/recent.js
  3. 2 31
      src/services/index.js
  4. 4 3
      src/utils/request.js

+ 2 - 2
src/components/common/login/login.jsx

@@ -90,11 +90,11 @@ class LoginComponent extends React.Component {
                     });
                 });
             }).catch(ex => {
+                console.error('登录失败', ex);
                 this.setState({
-                    notice: ex,
+                    notice: ex.message || '登录失败',
                     fetching: false
                 });
-                console.error('登录失败', ex);
             });
         });
     };

+ 2 - 2
src/models/recent.js

@@ -24,7 +24,7 @@ export default {
     effects: {
         *fetchRecentChart(action, { select, call, put }) {
             try {
-                const recent = yield select(state => state.present.recent);
+                // const recent = yield select(state => state.present.recent);
                 // if(!action.mandatory && recent.recentChart.length > 0) {
                 //     return;
                 // }
@@ -57,7 +57,7 @@ export default {
         },
         *fetchRecentDashboard(action, { select, call, put }) {
             try {
-                const recent = yield select(state => state.present.recent);
+                // const recent = yield select(state => state.present.recent);
                 // if(!action.mandatory && recent.recentDashboard.length > 0) {
                 //     return;
                 // }

+ 2 - 31
src/services/index.js

@@ -1,15 +1,8 @@
 import request from '../utils/request'
 
 export function fetch(option) {
-  // let client =  new XMLHttpRequest();
-  // client.open('POST', 'http://192.168.253.129:8080/showHistogram');
-  // client.setRequestHeader('Content-Type', 'application/json');
-  // client.send(JSON.stringify(values));
   const { url, body, timeout } = option;
   const token = window.localStorage.getItem("token");
-  // if(token === 'false') {
-  //   return {err: 'token已过期'}
-  // }
   return request(url, {
     method: 'POST',
     headers: {
@@ -17,27 +10,5 @@ export function fetch(option) {
       'Content-Type': 'application/json'
     },
     body: JSON.stringify(body),
-    timeout
-  });
-  // return request(`http://localhost:8001/api/users?_page=1&_limit=3`);
-}
-
-export function remove(id) {
-  return request(`/api/users/${id}`, {
-    method: 'DELETE',
-  });
-}
-
-export function patch(id, values) {
-  return request(`/api/users/${id}`, {
-    method: 'PATCH',
-    body: JSON.stringify(values),
-  });
-}
-
-export function create(values) {
-  return request('/api/users', {
-    method: 'POST',
-    body: JSON.stringify(values),
-  });
-}
+  }, timeout);
+}

+ 4 - 3
src/utils/request.js

@@ -36,13 +36,12 @@ class TimeoutError extends Error {
  * @param {*} [options={ method: 'GET' }]
  * @returns {Promise} the request result
  */
-export default function request(url, options) {
+export default function request(url, options, timeout) {
   let retryCount = 0;
 
   class Request {
     constructor(url, {
       retry,
-      timeout,
       ...options
     }) {
       this.url = url;
@@ -71,7 +70,9 @@ export default function request(url, options) {
         .then(data => {
           // 未进入重试或者超时错误,返回结果
           if (!done) {
-            fn({data});
+            fn({
+              data
+            });
             done = true;
           }
         })