Authored by 于良

增加接口请求的类

@@ -123,6 +123,9 @@ dependencies { @@ -123,6 +123,9 @@ dependencies {
123 compile fileTree(dir: "libs", include: ["*.jar"]) 123 compile fileTree(dir: "libs", include: ["*.jar"])
124 compile "com.android.support:appcompat-v7:23.0.1" 124 compile "com.android.support:appcompat-v7:23.0.1"
125 compile "com.facebook.react:react-native:+" // From node_modules 125 compile "com.facebook.react:react-native:+" // From node_modules
  126 +
  127 + compile project(":react-native-device-info")
  128 + compile fileTree(dir: "node_modules/react-native-device-info/android/libs", include: ["*.jar"])
126 } 129 }
127 130
128 // Run this once to be able to run the application with BUCK 131 // Run this once to be able to run the application with BUCK
@@ -7,6 +7,8 @@ import com.facebook.react.shell.MainReactPackage; @@ -7,6 +7,8 @@ import com.facebook.react.shell.MainReactPackage;
7 import java.util.Arrays; 7 import java.util.Arrays;
8 import java.util.List; 8 import java.util.List;
9 9
  10 +import com.learnium.RNDeviceInfo.RNDeviceInfo;
  11 +
10 public class MainActivity extends ReactActivity { 12 public class MainActivity extends ReactActivity {
11 13
12 /** 14 /**
@@ -34,6 +36,7 @@ public class MainActivity extends ReactActivity { @@ -34,6 +36,7 @@ public class MainActivity extends ReactActivity {
34 @Override 36 @Override
35 protected List<ReactPackage> getPackages() { 37 protected List<ReactPackage> getPackages() {
36 return Arrays.<ReactPackage>asList( 38 return Arrays.<ReactPackage>asList(
  39 + new RNDeviceInfo(),
37 new MainReactPackage() 40 new MainReactPackage()
38 ); 41 );
39 } 42 }
1 rootProject.name = 'YH_Vendor' 1 rootProject.name = 'YH_Vendor'
2 2
3 include ':app' 3 include ':app'
  4 +
  5 +// react-native-device-info dependency
  6 +include ':react-native-device-info'
  7 +project(':react-native-device-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')
@@ -13,6 +13,8 @@ target 'YH_Vendor' do @@ -13,6 +13,8 @@ target 'YH_Vendor' do
13 'RCTVibration', 13 'RCTVibration',
14 ], :path => '../node_modules/react-native' 14 ], :path => '../node_modules/react-native'
15 15
  16 + pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'
  17 +
16 end 18 end
17 19
18 # Start the React Native JS packager server when running the project in Xcode. 20 # Start the React Native JS packager server when running the project in Xcode.
1 PODS: 1 PODS:
  2 + - React (0.24.1):
  3 + - React/Core (= 0.24.1)
2 - React/Core (0.24.1) 4 - React/Core (0.24.1)
3 - React/RCTActionSheet (0.24.1): 5 - React/RCTActionSheet (0.24.1):
4 - React/Core 6 - React/Core
@@ -17,6 +19,8 @@ PODS: @@ -17,6 +19,8 @@ PODS:
17 - React/Core 19 - React/Core
18 - React/RCTWebSocket (0.24.1): 20 - React/RCTWebSocket (0.24.1):
19 - React/Core 21 - React/Core
  22 + - RNDeviceInfo (0.9.2):
  23 + - React
20 24
21 DEPENDENCIES: 25 DEPENDENCIES:
22 - React/Core (from `../node_modules/react-native`) 26 - React/Core (from `../node_modules/react-native`)
@@ -28,12 +32,16 @@ DEPENDENCIES: @@ -28,12 +32,16 @@ DEPENDENCIES:
28 - React/RCTText (from `../node_modules/react-native`) 32 - React/RCTText (from `../node_modules/react-native`)
29 - React/RCTVibration (from `../node_modules/react-native`) 33 - React/RCTVibration (from `../node_modules/react-native`)
30 - React/RCTWebSocket (from `../node_modules/react-native`) 34 - React/RCTWebSocket (from `../node_modules/react-native`)
  35 + - RNDeviceInfo (from `../node_modules/react-native-device-info`)
31 36
32 EXTERNAL SOURCES: 37 EXTERNAL SOURCES:
33 React: 38 React:
34 :path: ../node_modules/react-native 39 :path: ../node_modules/react-native
  40 + RNDeviceInfo:
  41 + :path: ../node_modules/react-native-device-info
35 42
36 SPEC CHECKSUMS: 43 SPEC CHECKSUMS:
37 React: 386bbe687faee56ab75bad6132813945d5857403 44 React: 386bbe687faee56ab75bad6132813945d5857403
  45 + RNDeviceInfo: e3fe8d8fe52f74eab22b7d4784a4fdd2e9bf4a26
38 46
39 COCOAPODS: 0.39.0 47 COCOAPODS: 0.39.0
@@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
13 13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; }; 13 13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
14 13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; 14 13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
15 875F164E1CCF71820051CA82 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 875F164D1CCF71820051CA82 /* libReact.a */; }; 15 875F164E1CCF71820051CA82 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 875F164D1CCF71820051CA82 /* libReact.a */; };
  16 + 87D1E5321CEC1BC70050ADA3 /* libRNDeviceInfo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 87D1E5311CEC1BC70050ADA3 /* libRNDeviceInfo.a */; };
16 BDB44E315EC65A19AFDFE3D5 /* libPods-YH_Vendor.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */; }; 17 BDB44E315EC65A19AFDFE3D5 /* libPods-YH_Vendor.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */; };
17 /* End PBXBuildFile section */ 18 /* End PBXBuildFile section */
18 19
@@ -39,6 +40,7 @@ @@ -39,6 +40,7 @@
39 13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = YH_Vendor/Info.plist; sourceTree = "<group>"; }; 40 13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = YH_Vendor/Info.plist; sourceTree = "<group>"; };
40 13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = YH_Vendor/main.m; sourceTree = "<group>"; }; 41 13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = YH_Vendor/main.m; sourceTree = "<group>"; };
41 875F164D1CCF71820051CA82 /* libReact.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libReact.a; path = "Pods/../build/Debug-iphoneos/libReact.a"; sourceTree = "<group>"; }; 42 875F164D1CCF71820051CA82 /* libReact.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libReact.a; path = "Pods/../build/Debug-iphoneos/libReact.a"; sourceTree = "<group>"; };
  43 + 87D1E5311CEC1BC70050ADA3 /* libRNDeviceInfo.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libRNDeviceInfo.a; path = "Pods/../build/Debug-iphoneos/libRNDeviceInfo.a"; sourceTree = "<group>"; };
42 A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-YH_Vendor.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 44 A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-YH_Vendor.a"; sourceTree = BUILT_PRODUCTS_DIR; };
43 B44EE6A85A06E0E02E6B1F23 /* Pods-YH_Vendor.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-YH_Vendor.release.xcconfig"; path = "Pods/Target Support Files/Pods-YH_Vendor/Pods-YH_Vendor.release.xcconfig"; sourceTree = "<group>"; }; 45 B44EE6A85A06E0E02E6B1F23 /* Pods-YH_Vendor.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-YH_Vendor.release.xcconfig"; path = "Pods/Target Support Files/Pods-YH_Vendor/Pods-YH_Vendor.release.xcconfig"; sourceTree = "<group>"; };
44 E38B39E1C626434D633DF86C /* Pods-YH_Vendor.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-YH_Vendor.debug.xcconfig"; path = "Pods/Target Support Files/Pods-YH_Vendor/Pods-YH_Vendor.debug.xcconfig"; sourceTree = "<group>"; }; 46 E38B39E1C626434D633DF86C /* Pods-YH_Vendor.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-YH_Vendor.debug.xcconfig"; path = "Pods/Target Support Files/Pods-YH_Vendor/Pods-YH_Vendor.debug.xcconfig"; sourceTree = "<group>"; };
@@ -56,6 +58,7 @@ @@ -56,6 +58,7 @@
56 isa = PBXFrameworksBuildPhase; 58 isa = PBXFrameworksBuildPhase;
57 buildActionMask = 2147483647; 59 buildActionMask = 2147483647;
58 files = ( 60 files = (
  61 + 87D1E5321CEC1BC70050ADA3 /* libRNDeviceInfo.a in Frameworks */,
59 875F164E1CCF71820051CA82 /* libReact.a in Frameworks */, 62 875F164E1CCF71820051CA82 /* libReact.a in Frameworks */,
60 BDB44E315EC65A19AFDFE3D5 /* libPods-YH_Vendor.a in Frameworks */, 63 BDB44E315EC65A19AFDFE3D5 /* libPods-YH_Vendor.a in Frameworks */,
61 ); 64 );
@@ -98,6 +101,7 @@ @@ -98,6 +101,7 @@
98 4629C9058F02534B1365A58F /* Frameworks */ = { 101 4629C9058F02534B1365A58F /* Frameworks */ = {
99 isa = PBXGroup; 102 isa = PBXGroup;
100 children = ( 103 children = (
  104 + 87D1E5311CEC1BC70050ADA3 /* libRNDeviceInfo.a */,
101 875F164D1CCF71820051CA82 /* libReact.a */, 105 875F164D1CCF71820051CA82 /* libReact.a */,
102 A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */, 106 A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */,
103 ); 107 );
1 -'use strict';  
2 -  
3 -import React from 'react-native';  
4 -  
5 -let {  
6 - Component,  
7 - View,  
8 - Text  
9 -} = React;  
10 -  
11 -  
12 -  
13 -import RNRF, {  
14 - Route,  
15 - Scene,  
16 - TabBar,  
17 - Actions  
18 -} from 'react-native-router-flux';  
19 -  
20 -export default class Home extends Component {  
21 - render() {  
22 -  
23 -  
24 - return (  
25 - <View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>  
26 - <Text>Android Home</Text>  
27 - </View>  
28 - );  
29 - }  
30 -}  
1 -'use strict';  
2 -  
3 -import React from 'react-native';  
4 -  
5 -let {  
6 - Component,  
7 - View,  
8 - Text  
9 -} = React;  
10 -  
11 -  
12 -  
13 -import RNRF, {  
14 - Route,  
15 - Scene,  
16 - TabBar,  
17 - Actions  
18 -} from 'react-native-router-flux';  
19 -  
20 -export default class Home extends Component {  
21 - render() {  
22 -  
23 -  
24 - return (  
25 - <View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>  
26 - <Text>iOS Home</Text>  
27 - </View>  
28 - );  
29 - }  
30 -}  
1 'use strict'; 1 'use strict';
2 2
3 import React from 'react-native'; 3 import React from 'react-native';
  4 +import PlainText from './PlainText';
4 5
5 let { 6 let {
6 Component, 7 Component,
@@ -9,8 +10,6 @@ let { @@ -9,8 +10,6 @@ let {
9 Platform 10 Platform
10 } = React; 11 } = React;
11 12
12 -  
13 -  
14 import RNRF, { 13 import RNRF, {
15 Route, 14 Route,
16 Scene, 15 Scene,
@@ -19,21 +18,17 @@ import RNRF, { @@ -19,21 +18,17 @@ import RNRF, {
19 } from 'react-native-router-flux'; 18 } from 'react-native-router-flux';
20 19
21 export default class Home extends Component { 20 export default class Home extends Component {
  21 +
22 render() { 22 render() {
23 23
24 - if (Platform.OS === 'ios') {  
25 - return (  
26 - <View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>  
27 - <Text>Platform iOS </Text>  
28 - </View>  
29 - );  
30 - } else {  
31 - return (  
32 - <View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>  
33 - <Text>Platform Android</Text>  
34 - </View>  
35 - );  
36 - } 24 + return (
  25 + <PlainText
  26 + topText={'同品类中品牌排名\n真好玩'}
  27 + bottomText={'76'}
  28 + containerStyle={{backgroundColor: 'white', width: 200, top: 100}}
  29 + />
  30 +
  31 + );
37 32
38 } 33 }
39 } 34 }
  1 +'use strict';
  2 +
  3 +import React from 'react-native';
  4 +
  5 +
  6 +let {
  7 + Component,
  8 + View,
  9 + Text,
  10 + Platform,
  11 + StyleSheet,
  12 +} = React;
  13 +
  14 +
  15 +export default class PlainText extends Component {
  16 +
  17 + static propTypes = {
  18 + topText: React.PropTypes.string,
  19 + bottomText: React.PropTypes.string,
  20 + containerStyle: View.propTypes.style,
  21 + topTextStyle: View.propTypes.style,
  22 + bottomTextStyle: View.propTypes.style,
  23 + };
  24 +
  25 + render() {
  26 +
  27 + return (
  28 + <View style={[styles.container, this.props.containerStyle]}>
  29 + <Text style={[styles.topText, this.props.topTextStyle]}>{this.props.topText}</Text>
  30 + <Text style={[styles.bottomText, this.props.bottomTextStyle]}>{this.props.bottomText}</Text>
  31 + </View>
  32 + );
  33 + }
  34 +}
  35 +
  36 +let styles = StyleSheet.create({
  37 + container: {
  38 + backgroundColor: 'white',
  39 + },
  40 + topText: {
  41 + fontSize: 14,
  42 + color: '#444444',
  43 + textAlign: 'center',
  44 + lineHeight: 20,
  45 + marginTop: 20,
  46 + marginBottom: 10,
  47 + backgroundColor: 'yellow',
  48 + },
  49 + bottomText: {
  50 + fontSize: 17,
  51 + color: '#D0021B',
  52 + fontWeight: 'bold',
  53 + textAlign: 'center',
  54 + marginBottom: 20,
  55 + backgroundColor: 'yellow',
  56 + },
  57 +
  58 +});
@@ -25,6 +25,10 @@ export default keyMirror({ @@ -25,6 +25,10 @@ export default keyMirror({
25 RESET_PASSWORD_SUCCESS: null, 25 RESET_PASSWORD_SUCCESS: null,
26 RESET_PASSWORD_FAILURE: null, 26 RESET_PASSWORD_FAILURE: null,
27 27
  28 + HOME_OVERVIEW_REQUEST: null,
  29 + HOME_OVERVIEW_SUCCESS: null,
  30 + HOME_OVERVIEW_FAILURE: null,
  31 +
28 /* 32 /*
29 GET_PROFILE_REQUEST: null, 33 GET_PROFILE_REQUEST: null,
30 GET_PROFILE_SUCCESS: null, 34 GET_PROFILE_SUCCESS: null,
1 module.exports = { 1 module.exports = {
2 - API: {  
3 - baseUrl: '',  
4 - clientSecret: '', 2 + dev: {
  3 + // baseUrl: 'http://testapi.yoho.cn:28077',
  4 + baseUrl: 'http://service.yoho.cn',
  5 + privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
5 6
6 }, 7 },
  8 + prd: {
  9 + baseUrl: 'http://service.yoho.cn',
  10 + privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
  11 + },
7 storeKey: { 12 storeKey: {
8 SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY', 13 SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY',
9 GUIDE_STATE_KEY: 'GUIDE_STATE_KEY', 14 GUIDE_STATE_KEY: 'GUIDE_STATE_KEY',
@@ -89,7 +89,6 @@ class App extends Component { @@ -89,7 +89,6 @@ class App extends Component {
89 componentDidMount() { 89 componentDidMount() {
90 this.props.actions.getDisplayState(); 90 this.props.actions.getDisplayState();
91 91
92 - this.props.actions.login('lily', '111111');  
93 } 92 }
94 93
95 render() { 94 render() {
@@ -68,12 +68,8 @@ class HomeContainer extends Component { @@ -68,12 +68,8 @@ class HomeContainer extends Component {
68 } 68 }
69 69
70 70
71 -var styles = StyleSheet.create({ 71 +let styles = StyleSheet.create({
72 72
73 - webView: {  
74 - backgroundColor: 'gray',  
75 -  
76 - },  
77 73
78 }); 74 });
79 75
@@ -6,3 +6,50 @@ @@ -6,3 +6,50 @@
6 */ 6 */
7 'use strict'; 7 'use strict';
8 8
  9 +import {Actions} from 'react-native-router-flux';
  10 +
  11 +const {
  12 +
  13 + HOME_OVERVIEW_REQUEST,
  14 + HOME_OVERVIEW_SUCCESS,
  15 + HOME_OVERVIEW_FAILURE,
  16 +
  17 +} = require('../../constants/actionTypes').default;
  18 +
  19 +export function overviewRequest(shopId) {
  20 + return {
  21 + type: HOME_OVERVIEW_REQUEST,
  22 + payload: shopId
  23 + };
  24 +}
  25 +
  26 +export function overviewSuccess(json) {
  27 + return {
  28 + type: HOME_OVERVIEW_SUCCESS,
  29 + payload: json
  30 + };
  31 +}
  32 +
  33 +export function overviewFailure(error) {
  34 + return {
  35 + type: HOME_OVERVIEW_FAILURE,
  36 + payload: error
  37 + };
  38 +}
  39 +
  40 +export function overview(shopId) {
  41 +
  42 + return dispatch => {
  43 + dispatch(overviewRequest());
  44 +
  45 + dispatch(overviewSuccess({
  46 + rank: 76,
  47 + rise: true,
  48 + riseCount: 28,
  49 + goodsCount: 7600,
  50 + goodsAmount: 19800.00,
  51 + }));
  52 + };
  53 +
  54 +}
  55 +
@@ -15,7 +15,16 @@ import {Record} from 'immutable'; @@ -15,7 +15,16 @@ import {Record} from 'immutable';
15 * The fields we're concerned with 15 * The fields we're concerned with
16 */ 16 */
17 let InitialState = Record({ 17 let InitialState = Record({
18 - 18 + isFetching: false,
  19 + error: null,
  20 + shopId: 0,
  21 + overview: new (Record({
  22 + rank: 0,
  23 + rise: true,
  24 + riseCount: 0,
  25 + goodsCount: 0,
  26 + goodsAmount: 0,
  27 + })),
19 }); 28 });
20 29
21 export default InitialState; 30 export default InitialState;
@@ -11,7 +11,13 @@ @@ -11,7 +11,13 @@
11 */ 11 */
12 import InitialState from './homeInitialState'; 12 import InitialState from './homeInitialState';
13 13
  14 +const {
14 15
  16 + HOME_OVERVIEW_REQUEST,
  17 + HOME_OVERVIEW_SUCCESS,
  18 + HOME_OVERVIEW_FAILURE,
  19 +
  20 +} = require('../../constants/actionTypes').default;
15 21
16 const initialState = new InitialState; 22 const initialState = new InitialState;
17 23
@@ -25,7 +31,28 @@ export default function userReducer(state = initialState, action) { @@ -25,7 +31,28 @@ export default function userReducer(state = initialState, action) {
25 31
26 switch (action.type) { 32 switch (action.type) {
27 33
28 - 34 + case HOME_OVERVIEW_REQUEST: {
  35 + let nextState = state.set('isFetching', true)
  36 + .set('error', null)
  37 + .set('shopId', action.payload.shopId);
  38 + return nextState;
  39 + }
  40 +
  41 + case HOME_OVERVIEW_SUCCESS: {
  42 + let nextState = state.set('isFetching', false)
  43 + .set('error', null)
  44 + .setIn(['overview', 'rank'], action.payload.rank)
  45 + .setIn(['overview', 'rise'], action.payload.rise)
  46 + .setIn(['overview', 'riseCount'], action.payload.riseCount)
  47 + .setIn(['overview', 'goodsCount'], action.payload.goodsCount)
  48 + .setIn(['overview', 'goodsAmount'], action.payload.goodsAmount);
  49 + return nextState;
  50 + }
  51 +
  52 + case HOME_OVERVIEW_FAILURE:
  53 + return state.set('isFetching', false)
  54 + .set('error', action.payload);
  55 +
29 56
30 } 57 }
31 58
@@ -53,25 +53,24 @@ export function loginFailure(error) { @@ -53,25 +53,24 @@ export function loginFailure(error) {
53 } 53 }
54 54
55 export function login(account, password) { 55 export function login(account, password) {
56 - return dispatch => {  
57 - dispatch(loginRequest());  
58 - return new UserService().login({  
59 - account: account,  
60 - password: password  
61 - })  
62 - .then(function (json) {  
63 - console.log(json);  
64 - return saveSessionToken(json)  
65 - .then(function () {  
66 - dispatch(loginSuccess(json));  
67 - Actions.Tabbar();  
68 - });  
69 - })  
70 - .catch((error) => {  
71 - console.error(error);  
72 - dispatch(loginFailure(error));  
73 - });  
74 - }; 56 + // return dispatch => {
  57 + // dispatch(loginRequest());
  58 + // return new UserService().login({
  59 + // account: account,
  60 + // password: password
  61 + // })
  62 + // .then(function (json) {
  63 + // return saveSessionToken(json)
  64 + // .then(function () {
  65 + // dispatch(loginSuccess(json));
  66 + // Actions.Tabbar();
  67 + // });
  68 + // })
  69 + // .catch((error) => {
  70 + // console.error(error);
  71 + // dispatch(loginFailure(error));
  72 + // });
  73 + // };
75 } 74 }
76 75
77 export function saveSessionToken(json) { 76 export function saveSessionToken(json) {
  1 +'use strict';
  2 +
  3 +import React from 'react-native';
  4 +import CONFIG from '../constants/config';
  5 +import DeviceInfo from 'react-native-device-info';
  6 +import queryString from 'query-string';
  7 +import md5 from 'md5';
  8 +
  9 +
  10 +export default class Request {
  11 +
  12 + constructor() {
  13 +
  14 + this.config = process.env.NODE_ENV === `development` ? CONFIG.dev : CONFIG.prd;
  15 +
  16 + this.baseUrl= this.config.baseUrl;
  17 + this.privateKey = this.config.privateKey;
  18 + }
  19 +
  20 +
  21 + async get(opts) {
  22 + try {
  23 + let response = await this._fetch({
  24 + method: 'GET',
  25 + url: opts.url,
  26 + });
  27 +
  28 + let data = await this._parseResponse(response);
  29 + return data;
  30 +
  31 + } catch(error) {
  32 + throw(error);
  33 + }
  34 + }
  35 +
  36 + async post(opts) {
  37 + try {
  38 + let response = await this._fetch({
  39 + method: 'POST',
  40 + url: opts.url,
  41 + body: opts.body,
  42 + });
  43 +
  44 + let data = await this._parseResponse(response);
  45 + return data;
  46 +
  47 + } catch(error) {
  48 + throw(error);
  49 + }
  50 + }
  51 +
  52 + async _parseResponse(response) {
  53 + if (response.status !== 200) {
  54 + let res = JSON.parse(response._bodyInit);
  55 + throw(res);
  56 + }
  57 +
  58 + let json = await response.json();
  59 + if (json.code == 200) {
  60 + return json.data;
  61 + } else {
  62 + throw({code: json.code, message: json.message});
  63 + }
  64 + }
  65 +
  66 + /**
  67 + * ### _fetch
  68 + */
  69 + async _fetch(opts) {
  70 + let defaultOpts = {
  71 + method: 'GET',
  72 + url: null,
  73 + body: null,
  74 + callback: null
  75 + };
  76 + opts = {
  77 + ...defaultOpts,
  78 + ...opts
  79 + };
  80 + opts.body = this._createBody();
  81 +
  82 + let reqOpts = {
  83 + method: opts.method,
  84 + headers: {
  85 +
  86 + }
  87 + };
  88 +
  89 + if (opts.method === 'GET' || opts.method === 'POST') {
  90 + reqOpts.headers['Accept'] = 'application/json';
  91 + }
  92 +
  93 + let queryStrigPair = this._signParam(opts.body);
  94 +
  95 + if (opts.method === 'GET' || opts.method === 'HEAD' || opts.method === 'DELETE') {
  96 + opts.url = opts.url + '?' + queryStrigPair;
  97 + }
  98 +
  99 + if (opts.body && opts.method !== 'GET' && opts.method !== 'HEAD') {
  100 + reqOpts.body = queryStrigPair;
  101 + }
  102 +
  103 + return await fetch(this.baseUrl + opts.url, reqOpts);
  104 +
  105 + }
  106 +
  107 + _publicParams() {
  108 + let app_version = DeviceInfo.getBuildNumber();
  109 + let os_version = DeviceInfo.getSystemVersion();
  110 + let client_type = React.Platform.OS === 'ios' ? 'iphone' : 'android';
  111 + let {height, width} = React.Dimensions.get('window');
  112 + let screen_size = width + 'x' + height;
  113 +
  114 + return {
  115 + app_version,
  116 + os_version,
  117 + client_type,
  118 + screen_size,
  119 + };
  120 + }
  121 +
  122 + _createBody(body) {
  123 + let defaultBody = this._publicParams();
  124 + let newBody = {
  125 + ...defaultBody,
  126 + ...body
  127 + };
  128 +
  129 + return newBody;
  130 + }
  131 +
  132 + _signParam(params) {
  133 + let private_key = this.privateKey;
  134 + let allParams = {
  135 + ...params,
  136 + private_key
  137 + }
  138 + let paramsPair = queryString.stringify(allParams)
  139 + let client_secret = md5(paramsPair);
  140 +
  141 + delete allParams.private_key;
  142 +
  143 + let resultParams = {
  144 + ...allParams,
  145 + client_secret
  146 + }
  147 +
  148 + return queryString.stringify(resultParams);
  149 + }
  150 +};
  151 +
1 'use strict'; 1 'use strict';
2 2
  3 +import Request from './Request';
  4 +
  5 +
3 export default class UserService { 6 export default class UserService {
4 7
5 - login(account, password) {  
6 - return fetch('http://testapi.yoho.cn:28078/gateway?method=app.passport.signin&account=zhiyuan&password=lzy111111'); 8 + constructor () {
  9 + this.api = new Request();
  10 + }
  11 +
  12 + async login(account, password) {
  13 + return this.api.get({
  14 + url: '/gateway?method=app.passport.signin&account=zhiyuan&password=lzy111111'
  15 + });
  16 + //
  17 +
7 } 18 }
8 19
9 } 20 }
@@ -32,7 +32,7 @@ const logger = createLogger({ @@ -32,7 +32,7 @@ const logger = createLogger({
32 */ 32 */
33 const createStoreWithMiddleware = applyMiddleware( 33 const createStoreWithMiddleware = applyMiddleware(
34 thunk, 34 thunk,
35 - logger 35 + // logger
36 )(createStore); 36 )(createStore);
37 37
38 /** 38 /**
@@ -7,11 +7,15 @@ @@ -7,11 +7,15 @@
7 }, 7 },
8 "dependencies": { 8 "dependencies": {
9 "apsl-react-native-button": "^2.4.2", 9 "apsl-react-native-button": "^2.4.2",
  10 + "frisbee": "^1.0.4",
10 "immutable": "^3.8.1", 11 "immutable": "^3.8.1",
11 "key-mirror": "^1.0.1", 12 "key-mirror": "^1.0.1",
  13 + "md5": "^2.1.0",
12 "object-assign": "^4.1.0", 14 "object-assign": "^4.1.0",
  15 + "query-string": "^4.1.0",
13 "react": "^0.14.8", 16 "react": "^0.14.8",
14 "react-native": "^0.24.1", 17 "react-native": "^0.24.1",
  18 + "react-native-device-info": "^0.9.3",
15 "react-native-drawer": "^2.0.0", 19 "react-native-drawer": "^2.0.0",
16 "react-native-navbar": "^1.4.2", 20 "react-native-navbar": "^1.4.2",
17 "react-native-router-flux": "^3.22.20", 21 "react-native-router-flux": "^3.22.20",