张延森 4 年前
父节点
当前提交
aa16fae166
共有 6 个文件被更改,包括 38 次插入43 次删除
  1. 3
    3
      src/app.js
  2. 2
    4
      src/layout/IsLogin.jsx
  3. 2
    6
      src/reducers/index.js
  4. 6
    20
      src/reducers/user.js
  5. 24
    9
      src/store/user.js
  6. 1
    1
      src/util/getCodeUrl.js

+ 3
- 3
src/app.js 查看文件

@@ -10,7 +10,7 @@ import './app.scss'
10 10
 const store = configStore()
11 11
 
12 12
 const App = (props)=> {
13
-  const {user, getUser} = useUser()
13
+  const {user, getUser} = useUser(store)
14 14
   const code = getCode()
15 15
 
16 16
   useEffect(() => {
@@ -21,11 +21,11 @@ const App = (props)=> {
21 21
       } else {
22 22
         // 请求 user
23 23
         getUser(code)
24
+        setCode(code)
24 25
       }
25 26
     }
26 27
 
27
-    return () => setCode(code)
28
-
28
+    // return () => setCode(code)
29 29
   }, [])
30 30
 
31 31
   // 在 App 类中的 render() 函数没有实际作用

+ 2
- 4
src/layout/IsLogin.jsx 查看文件

@@ -7,11 +7,9 @@ const IsLogin = ({ children }) => {
7 7
   const { user } = useUser();
8 8
   console.log(user, "user");
9 9
 
10
-  return user 
11
-  ? user.personId
10
+  return user && user.userId
12 11
     ? children
13
-    :<View style={{textAlign:'center'}}>您没有登录权限</View>
14
-  : <View style={{textAlign:'center'}}>登录中...</View>
12
+    : <View style={{textAlign:'center', marginTop: '200px', fontSize: '0.7em', color: '#888'}}>您暂无权限访问当前页面</View>
15 13
   ;
16 14
 };
17 15
 

+ 2
- 6
src/reducers/index.js 查看文件

@@ -1,10 +1,6 @@
1 1
 import { combineReducers } from 'redux'
2
-// import user from './user'
3
-// import page from './page'
4
-// import guide from './guide'
2
+import user from './user'
5 3
 
6 4
 export default combineReducers({
7
-  // user,
8
-  // page,
9
-  // guide,
5
+  user,
10 6
 })

+ 6
- 20
src/reducers/user.js 查看文件

@@ -1,28 +1,14 @@
1
-import {
2
-  ADD,
3
-  MINUS
4
-} from '../constants/user'
5 1
 
6
-// landlord: 'landlord',
7
-//     customer:'customer'
8
-// adminuser
9
-const INITIAL_STATE = {
10
-  user:'22'
11
-}
2
+const INITIAL_STATE = {}
12 3
 
13
-export default function counter(state = INITIAL_STATE, action) {
4
+export default function user(state = INITIAL_STATE, action) {
14 5
   switch (action.type) {
15
-    case ADD:
6
+    case 'setUser':
16 7
       return {
17 8
         ...state,
18
-        num: state.num + 1
9
+        user: action.payload
19 10
       }
20
-      case MINUS:
21
-        return {
22
-          ...state,
23
-          num: state.num - 1
24
-        }
25
-        default:
26
-          return state
11
+      default:
12
+        return state
27 13
   }
28 14
 }

+ 24
- 9
src/store/user.js 查看文件

@@ -1,20 +1,35 @@
1
-import { useState } from "react";
2 1
 import Taro from '@tarojs/taro'
2
+import { useSelector, useDispatch } from "react-redux"
3 3
 
4 4
 import request from '../util/request'
5 5
 
6
-export default function useUser() {
7
-    const [user, setUser] = useState()
6
+export default function useUser(store) {
7
+  let user = undefined
8
+  try {
9
+    user = useSelector(s => s.user)
10
+  } catch (e) {
11
+    user = store ? store.getState().user : undefined
12
+  }
13
+
14
+  let dispatch = undefined
15
+  try {
16
+    dispatch = useDispatch()
17
+  } catch (e) {
18
+    dispatch = store ? store.dispatch : undefined
19
+  }
8 20
 
9 21
     const getUser = code => {
10 22
         request({url:`/login?code=${code}`,method:"POST"}).then((res)=>{
11
-          console.log(res.data.data,'res')
12
-          setUser(res.data.data)
23
+          
24
+          const usr = res.data.data
25
+          dispatch({type: 'setUser', payload: usr})
13 26
 
14
-          Taro.setStorage({
15
-            key:"token",
16
-            data:res.data.data.token
17
-          })
27
+          if (usr.token) {
28
+            Taro.setStorage({
29
+              key: "token",
30
+              data: usr.token
31
+            })
32
+          }
18 33
         })
19 34
     }
20 35
 

+ 1
- 1
src/util/getCodeUrl.js 查看文件

@@ -4,7 +4,7 @@ const appid = APPID
4 4
 
5 5
 export default function(path) {
6 6
 
7
-    const  redirect_uri = `${window.location.origin}${window.location.pathname}#/${window.location.hash}`
7
+    const  redirect_uri = `${window.location.origin}${window.location.pathname}${window.location.hash}`
8 8
 
9 9
     const url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(redirect_uri)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
10 10
     return url