Skip to content

Commit e4cdb37

Browse files
test
2 parents 0ca40ec + f088fd1 commit e4cdb37

File tree

14 files changed

+421
-0
lines changed

14 files changed

+421
-0
lines changed

src/actions/loginActions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Created by wangtun on 2016/7/21.
3+
*/
4+
import { createAction } from 'redux-actions';
5+
import loginActionEnum from '../constants/loginActionEnum'
6+
7+
const loginActions = {
8+
login: createAction(loginActionEnum.LOGIN_REQUEST)
9+
};
10+
11+
export default loginActions;

src/apis/api.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { fetchMethod, createFetch } from '../fetch/fetch'
2+
import appConfig from '../constants/appConfig'
3+
4+
const api = {
5+
test: (parameter)=> createFetch(
6+
`${appConfig.serviceRoot}service/man/userInfo/login`,
7+
fetchMethod.Get,
8+
{parameter: parameter}
9+
)
10+
};
11+
12+
function callApi(api, success, fail){
13+
api.then(response=> {
14+
if (response.ok) {
15+
return response;
16+
} else {
17+
throw new Error(`网络错误:${response.status}`);
18+
}
19+
})
20+
.then(response=>response.json())
21+
.then(jsonResult=> {
22+
if (jsonResult.errcode === 0) {
23+
if (success) {
24+
const data = jsonResult.data;
25+
success(data);
26+
}
27+
} else {
28+
throw new Error(`服务器错误:${jsonResult.errmsg}`);
29+
}
30+
})
31+
.catch(error=> {
32+
if (fail) {
33+
fail(error.message);
34+
}
35+
})
36+
}
37+
38+
export {
39+
api,
40+
callApi
41+
};
42+

src/components/android/Login2.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Created by wangtun on 2016/7/21.
3+
*/
4+
import React from 'react';
5+
import {View, Text, StyleSheet} from "react-native";
6+
import Button from "react-native-button";
7+
import {Actions} from "react-native-router-flux";
8+
9+
const styles = StyleSheet.create({
10+
container: {
11+
flex: 1,
12+
justifyContent: "center",
13+
alignItems: "center",
14+
backgroundColor: "#F5FCFF",
15+
},
16+
});
17+
18+
class Login2 extends React.Component {
19+
render(){
20+
return (
21+
<View style={styles.container}>
22+
<Text>Login2 page: {this.props.data}</Text>
23+
<Button onPress={Actions.pop}>Back</Button>
24+
<Button onPress={Actions.loginModal3}>Login 3</Button>
25+
</View>
26+
);
27+
}
28+
}
29+
30+
export default Login2;

src/components/android/Login3.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Created by wangtun on 2016/7/21.
3+
*/
4+
import React from 'react';
5+
import {View, Text, StyleSheet} from "react-native";
6+
import Button from "react-native-button";
7+
import {Actions} from "react-native-router-flux";
8+
9+
const styles = StyleSheet.create({
10+
container: {
11+
flex: 1,
12+
justifyContent: "center",
13+
alignItems: "center",
14+
backgroundColor: "#F5FCFF",
15+
},
16+
});
17+
18+
const popToRoot = () => {
19+
Actions.popTo("root");
20+
}
21+
22+
const popToLogin1 = () => {
23+
Actions.popTo("loginModal");
24+
}
25+
26+
const popToLogin2 = () => {
27+
Actions.popTo("loginModal2");
28+
}
29+
class Login3 extends React.Component {
30+
render(){
31+
return (
32+
<View style={styles.container}>
33+
<Text>Login2 page: {this.props.data}</Text>
34+
<Button onPress={Actions.pop}>Back</Button>
35+
<Button onPress={popToLogin1}>To Login</Button>
36+
<Button onPress={popToLogin2}>To Login2</Button>
37+
<Button onPress={popToRoot}>To Root</Button>
38+
</View>
39+
);
40+
}
41+
}
42+
43+
export default Login3;

src/constants/appConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const appConfig = {
2+
serviceRoot: 'http://192.168.4.188:9000/'
3+
};
4+
5+
export default appConfig;

src/constants/loginActionEnum.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Created by wangtun on 2016/7/21.
3+
*/
4+
const loginActionEnum = {
5+
LOGIN_REQUEST: 'LOGIN_REQUEST'
6+
};
7+
8+
export default loginActionEnum;

src/containers/android/Login.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Created by wangtun on 2016/7/21.
3+
*/
4+
import React from 'react';
5+
import {View, Text, StyleSheet} from "react-native";
6+
import { connect } from 'react-redux'
7+
import Button from "react-native-button";
8+
import loginActions from '../../actions/loginActions'
9+
10+
const styles = StyleSheet.create({
11+
container: {
12+
flex: 1,
13+
justifyContent: "center",
14+
alignItems: "center",
15+
backgroundColor: "#F5FCFF",
16+
},
17+
welcome: {
18+
fontSize: 20,
19+
textAlign: "center",
20+
margin: 10,
21+
},
22+
instructions: {
23+
textAlign: "center",
24+
color: "#333333",
25+
marginBottom: 5,
26+
},
27+
});
28+
29+
30+
const Login = (props) => {
31+
const {login} = props.actions;
32+
return (
33+
<View style={[styles.container, {backgroundColor:"gray"}]}>
34+
<Text>Login page: </Text>
35+
<Button onPress={login}>-cccccc</Button>
36+
</View>
37+
);
38+
};
39+
40+
function mapStateToProps(state) {
41+
return {
42+
state:{
43+
logined: state.loginReducer.logined
44+
}
45+
}
46+
}
47+
48+
function mapDispatchToProps(dispatch) {
49+
return {
50+
actions:{
51+
login:() => dispatch(loginActions.login())
52+
}
53+
}
54+
}
55+
56+
export default connect(
57+
mapStateToProps,
58+
mapDispatchToProps
59+
)(Login)

src/containers/android/app.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Created by wangtun on 2016/7/21.
3+
*/
4+
import 'babel-polyfill';
5+
import React, { Component } from 'react';
6+
import { createStore, applyMiddleware, compose } from 'redux';
7+
import { Provider,connect } from 'react-redux';
8+
import { Router, Scene } from 'react-native-router-flux';
9+
import login2 from './../../components/android/Login2';
10+
import login3 from './../../components/android/Login3';
11+
import login from './Login';
12+
13+
import routeReducerCreator from './../../reducers/routeReducerCreator';
14+
import helper from './../../utils/helper'
15+
import { api, callApi } from './../../apis/api'
16+
import Button from "react-native-button";
17+
import store from './../../store/store'
18+
19+
class App extends React.Component {
20+
constructor(props) {
21+
super(props);
22+
helper.bindMethod(this);
23+
}
24+
25+
testApi(){
26+
const parameter ={userNickName:"mayunfei",userPassword:"test"};
27+
callApi(
28+
api.test(parameter),
29+
(data)=>console.log(data),
30+
(err)=>console.log(err)
31+
);
32+
}
33+
34+
render () {
35+
return (
36+
<Provider store={store}>
37+
<Router createReducer={routeReducerCreator}>
38+
<Scene key="login" direction="vertical" initial={true}>
39+
<Scene key="loginModal" direction="vertical" component={login} title="Login" />
40+
<Scene
41+
key="loginModal2"
42+
hideNavBar
43+
component={login2}
44+
title="Login2"
45+
panHandlers={null}
46+
duration={1}
47+
/>
48+
<Scene
49+
key="loginModal3"
50+
hideNavBar
51+
component={login3}
52+
title="Login3"
53+
panHandlers={null}
54+
duration={1}
55+
/>
56+
</Scene>
57+
</Router>
58+
</Provider>
59+
);
60+
//
61+
//return(
62+
// <Button onPress={this.testApi}>测试服务</Button>
63+
//);
64+
}
65+
}
66+
67+
export default App;

src/fetch/fetch.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Created by 28203 on 2016/5/29 0029.
3+
*/
4+
import fetch from 'isomorphic-fetch'
5+
6+
const fetchMethod = {
7+
Get: "GET",
8+
Post: "POST"
9+
};
10+
11+
function jsonToQueryString(jsonObj) {
12+
let queryStr = "";
13+
Object.keys(jsonObj).map((key)=> {
14+
const value = JSON.stringify(jsonObj[key]);
15+
queryStr += `${key}=${value}&`;
16+
});
17+
18+
if (queryStr.endsWith("&")) {
19+
queryStr = queryStr.substr(0, queryStr.length - 1);
20+
}
21+
22+
return queryStr;
23+
}
24+
25+
function createFetch(url, method, jsonObj) {
26+
switch (method) {
27+
case fetchMethod.Get:
28+
{
29+
const queryStr = jsonToQueryString(jsonObj);
30+
const urlWithQueryStr = `${url}?${queryStr}`;
31+
const options = {
32+
method: fetchMethod.Get
33+
};
34+
return fetch(urlWithQueryStr, options);
35+
}
36+
break;
37+
case fetchMethod.Post:
38+
{
39+
const queryStr = jsonToQueryString(jsonObj);
40+
const options = {
41+
method: fetchMethod.Post,
42+
body: queryStr
43+
};
44+
return fetch(url, options);
45+
}
46+
break;
47+
default:
48+
throw new Error(`not support method ${method}`);
49+
break;
50+
}
51+
}
52+
53+
export {
54+
fetchMethod,
55+
createFetch
56+
};

src/reducers/loginReducer.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Created by wangtun on 2016/7/21.
3+
*/
4+
import { handleActions } from 'redux-actions'
5+
import loginActions from '../actions/loginActions'
6+
7+
const initialState = {
8+
logined: false
9+
};
10+
11+
const loginReducer = handleActions({
12+
[loginActions.login]: (state, action) => {
13+
const newState = Object.assign({}, state);
14+
newState.logined = true;
15+
return newState;
16+
}
17+
18+
}, initialState);
19+
20+
export default loginReducer;

0 commit comments

Comments
 (0)