Skip to content

Commit

Permalink
登陆模拟效果完善
Browse files Browse the repository at this point in the history
  • Loading branch information
hiphonezhu committed Aug 19, 2016
1 parent c4fa9d6 commit 393b2b6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 23 deletions.
21 changes: 14 additions & 7 deletions Demo12/src/actions/Login.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
'use strict';
import * as types from '../constants/ActionTypes';

// 模拟服务器返回的用户信息
let user = {
'name': 'admin',
'age': '24'
}

export function doLogin()
{
return dispatch => {
dispatch(isLogining());
// 模拟用户登录
let result = fetch('http://www.baidu.com')
let result = fetch('https://github.com/')
.then((res)=>{
dispatch(loginSuccess(true));
dispatch(loginSuccess(true, user));
}).catch((e)=>{
dispatch(loginSuccess(false));
dispatch(loginSuccess(false, null));
});
}
}

function isLogining()
{
return {
type: types.LOGIN_IN
type: types.LOGIN_IN_DOING
}
}

function loginSuccess(isSuccess)
function loginSuccess(isSuccess, user)
{
return{
type: types.LOGIN_IN_COMPLETED,
isSuccess: isSuccess
type: types.LOGIN_IN_DONE,
isSuccess: isSuccess,
user: user
}
}
5 changes: 3 additions & 2 deletions Demo12/src/constants/ActionTypes.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const LOGIN_IN = 'LOGIN_IN';
export const LOGIN_IN_COMPLETED = 'LOGIN_IN_COMPLETED';
export const LOGIN_IN_INIT = 'LOGIN_IN_INIT'; // 初始状态
export const LOGIN_IN_DOING = 'LOGIN_IN_DOING'; // 正在登陆
export const LOGIN_IN_DONE = 'LOGIN_IN_DONE'; // 登陆完成
26 changes: 20 additions & 6 deletions Demo12/src/pages/LoginPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@ class LoginPage extends Component {

shouldComponentUpdate(nextProps, nextState)
{
if (nextProps.isSuccess) {
this.props.navigator.push({
// 登陆完成,且成功登陆
if (nextProps.status === 'done' && nextProps.isSuccess) {
this.props.navigator.replace({
id: 'MainPage',
component: MainPage
component: MainPage,
passProps: {
user: nextProps.user
},
});
return false;
}
return true;
}

render() {
let tips;
if (this.props.status === 'init')
{
tips = '请点击登陆';
}
else if (this.props.status === 'doing')
{
tips = '正在登陆...';
}
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center', flexDirection: 'column'}}>
<Text>{'loading:' + this.props.loading + '\nisSuccess:' + this.props.isSuccess}</Text>
<Text>{tips}</Text>
<TouchableOpacity style={{backgroundColor: '#FF0000'}} onPress={this.handleLogin.bind(this)}>
<View style={{flexDirection: 'row', alignItems: 'center', justifyContent: 'center', width: 100, height: 50}}>
<Text style={{color: '#FFFFFF', fontSize: 20}}>登录</Text>
Expand All @@ -49,8 +62,9 @@ class LoginPage extends Component {
function select(store)
{
return {
loading: store.loginIn.loading,
isSuccess: store.loginIn.isSuccess
status: store.loginIn.status,
isSuccess: store.loginIn.isSuccess,
user: store.loginIn.user
}
}

Expand Down
4 changes: 2 additions & 2 deletions Demo12/src/pages/MainPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
export default class MainPage extends Component {
render() {
return (
<View style={styles.container}>
<Text>登陆成功,欢迎使用</Text>
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center', flexDirection: 'column'}}>
<Text>{'姓名:' + this.props.user.name + '\n年龄:' + this.props.user.age}</Text>
</View>
);
}
Expand Down
22 changes: 16 additions & 6 deletions Demo12/src/reducers/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@
import * as types from '../constants/ActionTypes';

const initialState = {
loading: false,
status: 'init',
isSuccess: false,
user: null,
}

export default function loginIn(state = initialState, action) {
switch (action.type) {
case types.LOGIN_IN:
case types.LOGIN_IN_INIT:
return Object.assign({}, state, {
loading: true
status: 'init',
isSuccess: false,
user: null
});
case types.LOGIN_IN_COMPLETED:
case types.LOGIN_IN_DOING:
return Object.assign({}, state, {
loading: false,
isSuccess: action.isSuccess
status: 'doing',
isSuccess: false,
user: null
});
case types.LOGIN_IN_DONE:
return Object.assign({}, state, {
status: 'done',
isSuccess: action.isSuccess,
user: action.user
})
default:
return state;
Expand Down

0 comments on commit 393b2b6

Please sign in to comment.