Yansen 1 vuosi sitten
vanhempi
commit
cf39562849
4 muutettua tiedostoa jossa 12912 lisäystä ja 16487 poistoa
  1. 4
    4
      src/app.js
  2. 13
    11
      src/layouts/index.jsx
  3. 17
    8
      src/store/user.js
  4. 12878
    16464
      yarn.lock

+ 4
- 4
src/app.js Näytä tiedosto

@@ -5,10 +5,10 @@ import { useEffect } from "react";
5 5
 import "@antmjs/vantui/lib/index.less";
6 6
 
7 7
 const App = (props) => {
8
-  useEffect(() => {
9
-    const userState = store.getState("user");
10
-    userState.current();
11
-  }, []);
8
+  // useEffect(() => {
9
+  //   const userState = store.getState("user");
10
+  //   userState.current();
11
+  // }, []);
12 12
 
13 13
   return <Provider>{props.children}</Provider>;
14 14
 };

+ 13
- 11
src/layouts/index.jsx Näytä tiedosto

@@ -11,7 +11,7 @@ import laySty from "./layout.module.less";
11 11
 export default (props) => {
12 12
   const { className, style, roles, tabBar = false, loading } = props;
13 13
 
14
-  const { user, duty } = useModel("user");
14
+  const { user, duty, current: currentUser } = useModel("user");
15 15
   const containerClass = `${laySty["page-conatiner"]} ${
16 16
     tabBar ? laySty["with-tabbar"] : ""
17 17
   } ${className}`;
@@ -50,17 +50,19 @@ export default (props) => {
50 50
     console.log("userRef******************");
51 51
     console.log(user);
52 52
     console.log(!user);
53
-    if (!userRef.current&&user) {
54
-      userRef.current = user;
55
-    }
53
+    // if (!userRef.current&&user) {
54
+    //   userRef.current = user;
55
+    // }
56 56
 
57
-    if (!userRef.current) {
58
-      const currentPage = Taro.getCurrentPages().slice().pop();
59
-      if (!currentPage.route.includes("pages/login/index")) {
60
-        Taro.navigateTo({
61
-          url: "/pages/login/index",
62
-        });
63
-      }
57
+    if (!user) {
58
+      currentUser().catch(() => {
59
+        const currentPage = Taro.getCurrentPages().slice().pop();
60
+        if (!currentPage.route.includes("pages/login/index")) {
61
+          Taro.navigateTo({
62
+            url: "/pages/login/index",
63
+          });
64
+        }
65
+      })
64 66
     }
65 67
   }, [user]);
66 68
 

+ 17
- 8
src/store/user.js Näytä tiedosto

@@ -10,6 +10,7 @@ import {
10 10
 } from "@/services/wxma";
11 11
 import { changePassword } from "@/services/sysuser";
12 12
 import { ROLE_CITIZEN } from "@/utils/user";
13
+import { setToken } from "@/utils/token";
13 14
 
14 15
 export default function useUser() {
15 16
   const [person, setPerson] = React.useState();
@@ -62,14 +63,18 @@ export default function useUser() {
62 63
   };
63 64
 
64 65
   const current = () => {
65
-    currentUser()
66
-      .then((res) => {
67
-        setUser(res?.user);
68
-        initDuty(res?.user);
69
-      })
70
-      .catch((e) => {
71
-        console.log(e);
72
-      });
66
+    return new Promise((resolve, reject) => {
67
+      currentUser()
68
+        .then((res) => {
69
+          setUser(res?.user);
70
+          initDuty(res?.user);
71
+          resolve(res?.user);
72
+        })
73
+        .catch((e) => {
74
+          console.log(e);
75
+          reject(e);
76
+        });
77
+    });
73 78
   };
74 79
 
75 80
   const signinByPhone = (code) => {
@@ -91,6 +96,10 @@ export default function useUser() {
91 96
     console.log(111111)
92 97
     setUser(null);
93 98
     initDuty(null);
99
+    setToken();
100
+    Taro.navigateTo({
101
+      url: "/pages/login/index",
102
+    });
94 103
   };
95 104
 
96 105
   const changePwd = (params) => {

+ 12878
- 16464
yarn.lock
File diff suppressed because it is too large
Näytä tiedosto