张延森 пре 2 година
родитељ
комит
949df363e5
8 измењених фајлова са 67 додато и 6 уклоњено
  1. 10
    1
      src/App.vue
  2. 24
    0
      src/components/loading/index.vue
  3. 5
    0
      src/main.js
  4. 5
    4
      src/pages/login.vue
  5. 1
    1
      src/router/index.js
  6. 2
    0
      src/store/index.js
  7. 13
    0
      src/store/models/app.js
  8. 7
    0
      src/utils/request.js

+ 10
- 1
src/App.vue Прегледај датотеку

@@ -1,7 +1,16 @@
1 1
 <template>
2
-  <div id="app"><router-view /></div>
2
+  <div id="app">
3
+    <x-loading :show="loading" size="36px" style="position: fixed; top: 0; width: 100vw; z-index: 999" />
4
+    <router-view />
5
+  </div>
3 6
 </template>
4 7
 
8
+<script setup>
9
+import { useModel } from '@zjxpcyc/vue-tiny-store'
10
+
11
+const { loading } = useModel('app')
12
+</script>
13
+
5 14
 <style>
6 15
 html, body, #app {
7 16
   height: 100%;

+ 24
- 0
src/components/loading/index.vue Прегледај датотеку

@@ -0,0 +1,24 @@
1
+<template>
2
+  <div class="cmp-loading" v-if="show">
3
+    <van-loading color="#1989fa" :size="size" />
4
+  </div>
5
+</template>
6
+
7
+<script setup>
8
+const props = defineProps({
9
+  show: false,
10
+  size: {
11
+    type: String,
12
+    default: '48px',
13
+  }
14
+})
15
+
16
+</script>
17
+
18
+<style lang="less" scoped>
19
+.cmp-loading {
20
+  padding-top: 30%;
21
+  display: flex;
22
+  justify-content: center;
23
+}
24
+</style>

+ 5
- 0
src/main.js Прегледај датотеку

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

+ 5
- 4
src/pages/login.vue Прегледај датотеку

@@ -1,6 +1,6 @@
1 1
 <template>
2 2
   <div class="login-page">
3
-    <van-loading color="#1989fa" size="48px" />
3
+    <x-loading :show="true" />
4 4
   </div>
5 5
 </template>
6 6
 
@@ -9,6 +9,7 @@ import { onMounted } from 'vue';
9 9
 import { useRouter, useRoute } from 'vue-router'
10 10
 import { useModel } from '@zjxpcyc/vue-tiny-store'
11 11
 import { getRedirectURL } from '@/utils/wx';
12
+import { Notify } from 'vant';
12 13
 
13 14
 const { signIn } = useModel('user')
14 15
 const router = useRouter();
@@ -31,6 +32,8 @@ onMounted(() => {
31 32
       } else {
32 33
         router.replace({ name: 'index' })
33 34
       }
35
+    }).catch((err) => {
36
+      Notify({ type: 'warning', message: err.message });
34 37
     })
35 38
   }
36 39
 })
@@ -39,8 +42,6 @@ onMounted(() => {
39 42
 
40 43
 <style lang="less" scoped>
41 44
 .login-page {
42
-  padding-top: 30vh;
43
-  display: flex;
44
-  justify-content: center;
45
+  height: 100%;
45 46
 }
46 47
 </style>

+ 1
- 1
src/router/index.js Прегледај датотеку

@@ -22,7 +22,7 @@ router.beforeEach((to, from, next) => {
22 22
   }
23 23
 
24 24
   const { user, getCurrent } = store.getState('user');
25
-  if (!user && !user.personId) {
25
+  if (!user || !user.personId) {
26 26
     getCurrent().then(() => {
27 27
       NProgress.done();
28 28
       next();

+ 2
- 0
src/store/index.js Прегледај датотеку

@@ -1,8 +1,10 @@
1 1
 import createStore from '@zjxpcyc/vue-tiny-store'
2 2
 
3
+import useApp from './models/app'
3 4
 import useUser from './models/user'
4 5
 
5 6
 const models = {
7
+  'app': useApp,
6 8
   'user': useUser,
7 9
 }
8 10
 

+ 13
- 0
src/store/models/app.js Прегледај датотеку

@@ -0,0 +1,13 @@
1
+import { ref } from "vue";
2
+
3
+export default function useApp() {
4
+  const loading = ref(false)
5
+
6
+  const setLoading = b => loading.value = b;
7
+  window.__setloading = setLoading;
8
+
9
+  return {
10
+    loading,
11
+    setLoading,
12
+  }
13
+}

+ 7
- 0
src/utils/request.js Прегледај датотеку

@@ -11,6 +11,9 @@ request.interceptors.request.use(function (config = {}) {
11 11
   const token = getToken()
12 12
   const Authorization = token ? { Authorization: token } : {};
13 13
 
14
+  // 加载动画
15
+  window.__setloading(true);
16
+
14 17
   // 在发送请求之前做些什么
15 18
   return {
16 19
     ...config,
@@ -26,6 +29,8 @@ request.interceptors.request.use(function (config = {}) {
26 29
 
27 30
 // 添加响应拦截器
28 31
 request.interceptors.response.use(function (response) {
32
+  window.__setloading(false);
33
+
29 34
   // 2xx 范围内的状态码都会触发该函数。
30 35
   // 对响应数据做点什么
31 36
   const { data : respData } = response;
@@ -37,6 +42,8 @@ request.interceptors.response.use(function (response) {
37 42
     return Promise.reject(new Error(message));
38 43
   }
39 44
 }, function (error) {
45
+  window.__setloading(false);
46
+
40 47
   // 超出 2xx 范围的状态码都会触发该函数。
41 48
   // 对响应错误做点什么
42 49
   return Promise.reject(error);