yuantianjiao 6 years ago
parent
commit
dd02e27b2b
3 changed files with 45 additions and 2 deletions
  1. 1
    1
      config/index.js
  2. 3
    1
      src/main.js
  3. 41
    0
      src/util/ajax.js

+ 1
- 1
config/index.js View File

@@ -13,7 +13,7 @@ module.exports = {
13 13
     proxyTable: {},
14 14
 
15 15
     // Various Dev Server settings
16
-    host: 'localhost', // can be overwritten by process.env.HOST
16
+    host: '0.0.0.0', // can be overwritten by process.env.HOST
17 17
     port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
18 18
     autoOpenBrowser: false,
19 19
     errorOverlay: true,

+ 3
- 1
src/main.js View File

@@ -5,11 +5,13 @@ import App from './App'
5 5
 import ElementUI from 'element-ui'
6 6
 import 'element-ui/lib/theme-chalk/index.css'
7 7
 import router from './router'
8
+import Ajax from './util/ajax'
8 9
 
9 10
 Vue.use(ElementUI)
10 11
 
11 12
 Vue.config.productionTip = false
12
-
13
+const http = new Ajax()
14
+Vue.prototype.$ajax = http
13 15
 /* eslint-disable no-new */
14 16
 new Vue({
15 17
   el: '#app',

+ 41
- 0
src/util/ajax.js View File

@@ -0,0 +1,41 @@
1
+import axios from 'axios'
2
+import { Message } from 'element-ui'
3
+import qs from 'qs'
4
+
5
+const Axios = axios.create({
6
+  timeout: 60000,
7
+  responseType: 'json',
8
+  withCredentials: true,
9
+  headers: {}
10
+})
11
+
12
+Axios.interceptors.request.use((config) => {
13
+  config.data = qs.stringify(config.data)
14
+  return config
15
+}, (error) => {
16
+  console.log(error)
17
+})
18
+
19
+Axios.interceptors.response.use((res) => {
20
+  if (res.data.code === 1) {
21
+    return res
22
+  } else if (res.data.code === 2) {
23
+    Message({
24
+      message: res.data.message,
25
+      position: 'bottom',
26
+      duration: 2000
27
+    })
28
+    return res.data.code
29
+  } else if (res.data.code === 0) {
30
+    Message({
31
+      message: res.data.message,
32
+      position: 'bottom',
33
+      duration: 2000
34
+    })
35
+    return res.data.code
36
+  }
37
+}, (error) => {
38
+  console.log(error)
39
+})
40
+
41
+export default Axios