Authored by 陈峰

接口异常捕获

@@ -41,8 +41,6 @@ @@ -41,8 +41,6 @@
41 return; 41 return;
42 } 42 }
43 return this.$Message.error(err_msg); 43 return this.$Message.error(err_msg);
44 - }, (error) => {  
45 - this.$Message.error(error.message);  
46 }); 44 });
47 }, 45 },
48 methods: { 46 methods: {
@@ -4,7 +4,6 @@ @@ -4,7 +4,6 @@
4 import _ from 'lodash'; 4 import _ from 'lodash';
5 import axios from 'axios'; 5 import axios from 'axios';
6 import userService from 'user-service'; 6 import userService from 'user-service';
7 -import iView from 'iview';  
8 import Rsa from 'rsa'; 7 import Rsa from 'rsa';
9 8
10 const plugin = { 9 const plugin = {
@@ -100,15 +99,11 @@ const plugin = { @@ -100,15 +99,11 @@ const plugin = {
100 Vue.$router.push('/login.html'); 99 Vue.$router.push('/login.html');
101 }; 100 };
102 axios.defaults.validateStatus = (status) => { 101 axios.defaults.validateStatus = (status) => {
103 - if (status >= 200 && status < 300) {  
104 - return true;  
105 - }  
106 if (status === 401) { 102 if (status === 401) {
107 Vue.logout(); 103 Vue.logout();
108 return false; 104 return false;
109 } 105 }
110 - iView.Message.error('接口异常');  
111 - return false; 106 + return true;
112 }; 107 };
113 } 108 }
114 }; 109 };
@@ -93,6 +93,16 @@ const plugin = { @@ -93,6 +93,16 @@ const plugin = {
93 // 设置axios默认参数 93 // 设置axios默认参数
94 axios.defaults.baseURL = Vue.$config.axiosBaseUrl; 94 axios.defaults.baseURL = Vue.$config.axiosBaseUrl;
95 axios.defaults.responseType = Vue.$config.axiosResponseType; 95 axios.defaults.responseType = Vue.$config.axiosResponseType;
  96 +
  97 + axios.interceptors.response.use(response => {
  98 + if (response.status >= 200 && response.status < 300) {
  99 + return response;
  100 + }
  101 + iView.Message.error('接口异常');
  102 + return Promise.reject({response});
  103 + }, error => {
  104 + return Promise.reject(error);
  105 + });
96 } 106 }
97 }; 107 };
98 108
@@ -58,7 +58,13 @@ class Api extends Context { @@ -58,7 +58,13 @@ class Api extends Context {
58 58
59 return request(params, (error, response) => { 59 return request(params, (error, response) => {
60 if (!error) { 60 if (!error) {
61 - logger.info(`api call ${response.statusCode} [${response.request.method}] ${response.request.href} ${response.request.body}`); // eslint-disable-line 61 + if (response && /^20\d/.test(response.statusCode)) {
  62 + logger.info(`api call ${response.statusCode} [${response.request.method}] ${response.request.href} ${response.request.body}`); // eslint-disable-line
  63 + } else {
  64 + let res = response.toJSON();
  65 +
  66 + logger.error(`api call ${res.statusCode} [${res.request.method}] ${res.request.uri.href} ${res.body || ''}`); // eslint-disable-line
  67 + }
62 } 68 }
63 }); 69 });
64 } 70 }
@@ -12,7 +12,7 @@ const apiDomain = require('./api-domain'); @@ -12,7 +12,7 @@ const apiDomain = require('./api-domain');
12 const config = { 12 const config = {
13 app: 'shop-manage', 13 app: 'shop-manage',
14 appVersion: '0.0.1', // 调用api的版本 14 appVersion: '0.0.1', // 调用api的版本
15 - platform: 2, 15 + platform: 4,
16 port: 6007, 16 port: 6007,
17 siteUrl: '//shop.yohobuy.com', 17 siteUrl: '//shop.yohobuy.com',
18 assetUrl: '//127.0.0.1:5001', 18 assetUrl: '//127.0.0.1:5001',