浏览代码

Merge branch 'master' of http://git.ycjcjy.com/ershoufang/mp-agent into master

zlisen 4 年前
父节点
当前提交
f275e142db
共有 8 个文件被更改,包括 191 次插入2 次删除
  1. 2
    0
      package.json
  2. 35
    0
      src/components/Authentication/index.vue
  3. 61
    0
      src/components/XLoading/index.vue
  4. 11
    1
      src/main.js
  5. 8
    0
      src/store/index.js
  6. 9
    0
      src/store/models/loading.js
  7. 52
    0
      src/utils/request/index.js
  8. 13
    1
      yarn.lock

+ 2
- 0
package.json 查看文件

@@ -8,6 +8,8 @@
8 8
     "lint": "vue-cli-service lint"
9 9
   },
10 10
   "dependencies": {
11
+    "@zjxpcyc/vue-tiny-store": "^1.0.1",
12
+    "axios": "^0.21.1",
11 13
     "core-js": "^3.6.5",
12 14
     "vant": "^3.0.0",
13 15
     "vue": "^3.0.0",

+ 35
- 0
src/components/Authentication/index.vue 查看文件

@@ -0,0 +1,35 @@
1
+<template>
2
+    <template v-if="!show">
3
+      <span style="display: inline-block" v-if="error">{{error}}</span>
4
+      <slot name="callback"></slot>
5
+    </template>
6
+    <slot v-else></slot>
7
+</template>
8
+
9
+<script>
10
+import { computed } from 'vue'
11
+
12
+export default {
13
+  props: {
14
+    check: undefined,
15
+    error: String,
16
+  },
17
+  
18
+  setup(props) {
19
+    const show = computed(() => {
20
+      if (typeof props.check === 'boolean') {
21
+        return props.check
22
+      } else if (typeof props.check === 'function') {
23
+        // 函数必须返回 boolean 值
24
+        return props.check()
25
+      } else {
26
+        return false
27
+      }
28
+    })
29
+
30
+    return {
31
+      show,
32
+    }
33
+  }
34
+}
35
+</script>

+ 61
- 0
src/components/XLoading/index.vue 查看文件

@@ -0,0 +1,61 @@
1
+<template>
2
+  <div class="x-loading">
3
+    <div v-show="show" class="loadding-wrapper">
4
+      <div class="loading-flex">
5
+        <div style="flex:none">
6
+          <van-loading :color="color || '#1989fa'">{{tip}}</van-loading>
7
+        </div>
8
+      </div>
9
+    </div>
10
+    <slot></slot>
11
+  </div>
12
+</template>
13
+
14
+<script>
15
+import { computed } from 'vue'
16
+import { Loading } from 'vant'
17
+import { useModel } from '@zjxpcyc/vue-tiny-store'
18
+// import store from '../../store'
19
+
20
+export default {
21
+  components: {
22
+    [Loading.name]: Loading,
23
+  },
24
+  props: {
25
+    color: String,
26
+    tip: String,
27
+    xId: String,
28
+  },
29
+  setup(props) {
30
+    const { loading } = useModel('loading')
31
+
32
+    const show = computed(() => loading[props.xId])
33
+
34
+    return {
35
+      show
36
+    }
37
+  }
38
+}
39
+</script>
40
+
41
+<style lang="less" scoped>
42
+.x-loading {
43
+  position: relative;
44
+
45
+  .loadding-wrapper {
46
+    width: 100%;
47
+    height: 100%;
48
+    background: rgba(255, 255, 255, .9);
49
+    z-index: 9999;
50
+    position: absolute;
51
+    top: 0;
52
+    left: 0;
53
+
54
+    .loading-flex {
55
+      display: flex;
56
+      justify-content: center;
57
+      align-items: center;
58
+    }
59
+  }
60
+}
61
+</style>

+ 11
- 1
src/main.js 查看文件

@@ -2,7 +2,17 @@
2 2
 import { createApp } from 'vue';
3 3
 import App from './App.vue';
4 4
 import { router } from './router';
5
+import store from './store'
5 6
 import 'vant/lib/index.css';
7
+
8
+import Authentication from './components/Authentication'
9
+import XLoading from './components/XLoading'
10
+
6 11
 const app = createApp(App);
12
+app.use(store);
7 13
 app.use(router);
8
-app.mount('#app');
14
+
15
+app.component('x-loading', XLoading)
16
+app.component('x-auth', Authentication)
17
+
18
+app.mount('#app');

+ 8
- 0
src/store/index.js 查看文件

@@ -0,0 +1,8 @@
1
+import createStore from '@zjxpcyc/vue-tiny-store'
2
+import loading from './models/loading'
3
+
4
+const store = createStore({
5
+  loading,
6
+})
7
+
8
+export default store

+ 9
- 0
src/store/models/loading.js 查看文件

@@ -0,0 +1,9 @@
1
+import { reactive } from "@vue/reactivity"
2
+
3
+export default () => {
4
+  const loading = reactive({})
5
+
6
+  const setLoading = (id, state) => loading[id] = state
7
+
8
+  return { loading, setLoading }
9
+}

+ 52
- 0
src/utils/request/index.js 查看文件

@@ -0,0 +1,52 @@
1
+import axios from 'axios'
2
+
3
+// request 拦截器
4
+const requestInterceptor = config => {
5
+  return config
6
+}
7
+
8
+// response 拦截器
9
+const responseInterceptor = response => {
10
+  const { data, headers } = response
11
+  const contentType = headers['Content-Type'] || headers['content-type'] || ''
12
+
13
+  if (/json/.test(contentType)) {
14
+    if ('0' === data.code) {
15
+      return data.data
16
+    } else if ('1' === data.code) {
17
+      return Promise.reject('账号未登录或没有权限')
18
+    } else {
19
+      return Promise.reject(data.message)
20
+    }
21
+
22
+  } else {
23
+    return Promise.reject("账号未登录或没有权限")
24
+  }
25
+}
26
+
27
+// 错误处理
28
+const handleError = type => error => {
29
+  if (error.response) {
30
+    console.error(`[${type}]:`)
31
+    console.error(error.response)    
32
+  } else {
33
+    const errorMessage = error.message || error.msg || error
34
+    console.error(`[${type}] ${errorMessage}`)
35
+    console.error(error)
36
+    return Promise.reject(errorMessage)
37
+  }
38
+}
39
+
40
+const request = axios.create({
41
+  // 通用前缀
42
+  baseURL: '/mp',
43
+  // 允许 cookie
44
+  withCredentials: true,
45
+  // 全部都是 post 请求
46
+  method: 'post',
47
+})
48
+
49
+request.interceptors.request.use(requestInterceptor, handleError('REQUEST'))
50
+request.interceptors.response.use(responseInterceptor, handleError('RESPONSE'))
51
+
52
+export default request

+ 13
- 1
yarn.lock 查看文件

@@ -1625,6 +1625,11 @@
1625 1625
   resolved "https://registry.npm.taobao.org/@xtuc/long/download/@xtuc/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
1626 1626
   integrity sha1-0pHGpOl5ibXGHZrPOWrk/hM6cY0=
1627 1627
 
1628
+"@zjxpcyc/vue-tiny-store@^1.0.1":
1629
+  version "1.0.1"
1630
+  resolved "https://registry.npm.taobao.org/@zjxpcyc/vue-tiny-store/download/@zjxpcyc/vue-tiny-store-1.0.1.tgz#9e67c0f08e9bddd802bdcb543a28c18181e27395"
1631
+  integrity sha1-nmfA8I6b3dgCvctUOijBgYHic5U=
1632
+
1628 1633
 accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.7:
1629 1634
   version "1.3.7"
1630 1635
   resolved "https://registry.npm.taobao.org/accepts/download/accepts-1.3.7.tgz#531bc726517a3b2b41f850021c6cc15eaab507cd"
@@ -1912,6 +1917,13 @@ aws4@^1.8.0:
1912 1917
   resolved "https://registry.npm.taobao.org/aws4/download/aws4-1.11.0.tgz?cache=0&sync_timestamp=1604101244098&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Faws4%2Fdownload%2Faws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
1913 1918
   integrity sha1-1h9G2DslGSUOJ4Ta9bCUeai0HFk=
1914 1919
 
1920
+axios@^0.21.1:
1921
+  version "0.21.1"
1922
+  resolved "https://registry.npm.taobao.org/axios/download/axios-0.21.1.tgz?cache=0&sync_timestamp=1608611162952&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Faxios%2Fdownload%2Faxios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
1923
+  integrity sha1-IlY0gZYvTWvemnbVFu8OXTwJsrg=
1924
+  dependencies:
1925
+    follow-redirects "^1.10.0"
1926
+
1915 1927
 babel-eslint@^10.1.0:
1916 1928
   version "10.1.0"
1917 1929
   resolved "https://registry.npm.taobao.org/babel-eslint/download/babel-eslint-10.1.0.tgz?cache=0&sync_timestamp=1611946213770&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fbabel-eslint%2Fdownload%2Fbabel-eslint-10.1.0.tgz#6968e568a910b78fb3779cdd8b6ac2f479943232"
@@ -4004,7 +4016,7 @@ flush-write-stream@^1.0.0:
4004 4016
     inherits "^2.0.3"
4005 4017
     readable-stream "^2.3.6"
4006 4018
 
4007
-follow-redirects@^1.0.0:
4019
+follow-redirects@^1.0.0, follow-redirects@^1.10.0:
4008 4020
   version "1.13.3"
4009 4021
   resolved "https://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.13.3.tgz#e5598ad50174c1bc4e872301e82ac2cd97f90267"
4010 4022
   integrity sha1-5VmK1QF0wbxOhyMB6CrCzZf5Amc=