Pārlūkot izejas kodu

Merge branch 'master' of http://git.ycjcjy.com/shigongli/client-s into master

zlisen 4 gadus atpakaļ
vecāks
revīzija
711baef846

+ 1
- 1
config/index.js Parādīt failu

@@ -45,7 +45,7 @@ const config = {
45 45
     }
46 46
   },
47 47
   h5: {
48
-    publicPath: '/',
48
+    publicPath: './',
49 49
     staticDirectory: 'static',
50 50
     postcss: {
51 51
       autoprefixer: {

+ 3
- 3
src/app.js Parādīt failu

@@ -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() 函数没有实际作用

+ 3
- 8
src/layout/IsLogin.jsx Parādīt failu

@@ -1,18 +1,13 @@
1
-import React from "react";
1
+import React, { useState, useEffect } from "react";
2 2
 import { View, ScrollView } from "@tarojs/components";
3 3
 import "./index.scss";
4 4
 import useUser from "../store/user";
5 5
 
6 6
 const IsLogin = ({ children }) => {
7 7
   const { user } = useUser();
8
-  console.log(user, "user");
9 8
 
10
-  return user 
11
-  ? user.personId
12
-    ? children
13
-    :<View style={{textAlign:'center'}}>您没有登录权限</View>
14
-  : <View style={{textAlign:'center'}}>登录中...</View>
15
-  ;
9
+  return user && user.userId ? children :
10
+    (<View className="no-rights">您暂无权限访问当前页面</View>)
16 11
 };
17 12
 
18 13
 export default IsLogin;

+ 8
- 1
src/layout/index.scss Parādīt failu

@@ -2,4 +2,11 @@
2 2
     
3 3
     height: calc(100vh - 173.33px)
4 4
     
5
-}
5
+}
6
+
7
+.no-rights {
8
+    text-align: center;
9
+    margin-top: 200px;
10
+    font-size: 0.7em;
11
+    color: #888;
12
+}

+ 2
- 6
src/reducers/index.js Parādīt failu

@@ -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 Parādīt failu

@@ -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
+        ...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 Parādīt failu

@@ -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
- 8
src/util/getCodeUrl.js Parādīt failu

@@ -4,14 +4,7 @@ const appid = APPID
4 4
 
5 5
 export default function(path) {
6 6
 
7
-    console.log(window.location,'window.location')
8
-  const  redirect_uri = `${window.location.origin}/${window.location.hash}`
9
-
10
-
11
-//   window.location.origin = `${window.location.protocol}//${Taro.Current.router.path
12
-//       window.location.hostname
13
-//     }${window.location.port ? ':' + window.location.port : ''}`;
14
-    console.log(APPID,redirect_uri,'333')
7
+    const  redirect_uri = `${window.location.origin}${window.location.pathname}${window.location.hash}`
15 8
 
16 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`
17 10
     return url