yuantianjiao 6 years ago
parent
commit
41d93ace1d
8 changed files with 93 additions and 5 deletions
  1. 1
    0
      package.json
  2. 1
    1
      src/App.vue
  3. 4
    1
      src/main.js
  4. 15
    1
      src/page/test/test.vue
  5. 1
    1
      src/router/index.js
  6. 0
    1
      src/style/main.scss
  7. 60
    0
      src/util/ajax.js
  8. 11
    0
      src/util/api.js

+ 1
- 0
package.json View File

@@ -14,6 +14,7 @@
14 14
     "build": "node build/build.js"
15 15
   },
16 16
   "dependencies": {
17
+    "axios": "^0.18.0",
17 18
     "vant": "^1.1.15",
18 19
     "vue": "^2.5.2",
19 20
     "vue-router": "^3.0.1"

+ 1
- 1
src/App.vue View File

@@ -11,7 +11,7 @@ export default {
11 11
 </script>
12 12
 
13 13
 <style>
14
-@import 'style/main.sass';
14
+@import 'style/main.scss';
15 15
 #app {
16 16
   font-family: 'Avenir', Helvetica, Arial, sans-serif;
17 17
   -webkit-font-smoothing: antialiased;

+ 4
- 1
src/main.js View File

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

src/page/test.vue → src/page/test/test.vue View File

@@ -8,12 +8,26 @@
8 8
 </template>
9 9
 
10 10
 <script>
11
-// import { Button } from 'vant'
12 11
 export default {
13 12
   data () {
14 13
     return {
15 14
       a: '1'
16 15
     }
16
+  },
17
+  mounted () {
18
+    console.log(this)
19
+    this.$ajax(this.$api.test.area.url, {
20
+      method: this.$api.test.area.method,
21
+      data: {
22
+        caseid: 1,
23
+        id: 2
24
+      }
25
+    })
26
+      .then(res => {
27
+        console.log(res)
28
+      }).catch(msg => {
29
+        return false
30
+      })
17 31
   }
18 32
 }
19 33
 </script>

+ 1
- 1
src/router/index.js View File

@@ -2,7 +2,7 @@ import Vue from 'vue'
2 2
 import Router from 'vue-router'
3 3
 const test = () =>
4 4
   import(/* webpackChunkName: "test" */
5
-    '../page/test')
5
+    '../page/test/test')
6 6
 Vue.use(Router)
7 7
 
8 8
 export default new Router({

src/style/main.sass → src/style/main.scss View File

@@ -31,7 +31,6 @@ html,body {
31 31
 * {
32 32
     margin: 0;
33 33
     padding: 0;
34
-    color: #000;
35 34
     font-family: "微软雅黑";
36 35
     -webkit-tap-highlight-color: rgba(255,0,0,0);
37 36
     -webkit-font-smoothing: antialiased;

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

@@ -0,0 +1,60 @@
1
+import axios from 'axios'
2
+import { Toast } from 'vant'
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
+  Toast.loading({
14
+    duration: 0, // 持续展示 toast
15
+    forbidClick: true, // 禁用背景点击
16
+    loadingType: 'spinner',
17
+    color: 'white',
18
+    message: '请求中'
19
+  })
20
+  // 处理请求data,若为get请求,拼到url后面,若为post请求,直接添加到body中
21
+  let data = qs.stringify(config.data)
22
+  if (config.method === 'get' || config.method === 'GET') {
23
+    // 判断是通过斜杠传参还是通过query传参
24
+    if (config.url.indexOf(':') > -1) {
25
+      if (typeof config.data === 'object') {
26
+        config.url = Object.keys(config.data).reduce((url, k) => { // 此方法对每个元素进行处理
27
+          const re = new RegExp(`:${k}(?!w)`, 'i')
28
+          return url.replace(re, config.data[k])
29
+        }, config.url)
30
+      } else {
31
+        config.url = config.url.slice(0, config.url.indexOf(':')) + data
32
+      }
33
+    } else {
34
+      config.url += '?' + data
35
+    }
36
+  }
37
+  return config
38
+}, (error) => {
39
+  console.log(error)
40
+})
41
+
42
+Axios.interceptors.response.use((res) => {
43
+  setTimeout(() => {
44
+    Toast.clear()
45
+  }, 500)
46
+  if (res.status === 200) {
47
+    return res.data
48
+  } else {
49
+    Toast.fail('请求失败,状态码为' + res.status)
50
+    return res.data
51
+  }
52
+}, (error) => {
53
+  Toast.fail('请求失败,服务器异常')
54
+  setTimeout(() => {
55
+    Toast.clear()
56
+  }, 500)
57
+  console.log(error)
58
+})
59
+
60
+export default Axios

+ 11
- 0
src/util/api.js View File

@@ -0,0 +1,11 @@
1
+const baseUrl = process.env.NODE_ENV === 'development' ? '/api' : 'http://192.168.0.62:8080/api'
2
+// const baseUrl = '/check-api'
3
+const $api = {
4
+  test: { // 测试接口
5
+    area: {
6
+      method: 'post',
7
+      url: `${baseUrl}/machine/area`
8
+    }
9
+  }
10
+}
11
+export default $api