Authored by 于良

增加接口请求的类

... ... @@ -123,6 +123,9 @@ dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules
compile project(":react-native-device-info")
compile fileTree(dir: "node_modules/react-native-device-info/android/libs", include: ["*.jar"])
}
// Run this once to be able to run the application with BUCK
... ...
... ... @@ -7,6 +7,8 @@ import com.facebook.react.shell.MainReactPackage;
import java.util.Arrays;
import java.util.List;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
public class MainActivity extends ReactActivity {
/**
... ... @@ -34,6 +36,7 @@ public class MainActivity extends ReactActivity {
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new RNDeviceInfo(),
new MainReactPackage()
);
}
... ...
rootProject.name = 'YH_Vendor'
include ':app'
// react-native-device-info dependency
include ':react-native-device-info'
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
'RCTVibration',
], :path => '../node_modules/react-native'
pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'
end
# Start the React Native JS packager server when running the project in Xcode.
... ...
PODS:
- React (0.24.1):
- React/Core (= 0.24.1)
- React/Core (0.24.1)
- React/RCTActionSheet (0.24.1):
- React/Core
... ... @@ -17,6 +19,8 @@ PODS:
- React/Core
- React/RCTWebSocket (0.24.1):
- React/Core
- RNDeviceInfo (0.9.2):
- React
DEPENDENCIES:
- React/Core (from `../node_modules/react-native`)
... ... @@ -28,12 +32,16 @@ DEPENDENCIES:
- React/RCTText (from `../node_modules/react-native`)
- React/RCTVibration (from `../node_modules/react-native`)
- React/RCTWebSocket (from `../node_modules/react-native`)
- RNDeviceInfo (from `../node_modules/react-native-device-info`)
EXTERNAL SOURCES:
React:
:path: ../node_modules/react-native
RNDeviceInfo:
:path: ../node_modules/react-native-device-info
SPEC CHECKSUMS:
React: 386bbe687faee56ab75bad6132813945d5857403
RNDeviceInfo: e3fe8d8fe52f74eab22b7d4784a4fdd2e9bf4a26
COCOAPODS: 0.39.0
... ...
... ... @@ -13,6 +13,7 @@
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
875F164E1CCF71820051CA82 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 875F164D1CCF71820051CA82 /* libReact.a */; };
87D1E5321CEC1BC70050ADA3 /* libRNDeviceInfo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 87D1E5311CEC1BC70050ADA3 /* libRNDeviceInfo.a */; };
BDB44E315EC65A19AFDFE3D5 /* libPods-YH_Vendor.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */; };
/* End PBXBuildFile section */
... ... @@ -39,6 +40,7 @@
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = YH_Vendor/Info.plist; sourceTree = "<group>"; };
13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = YH_Vendor/main.m; sourceTree = "<group>"; };
875F164D1CCF71820051CA82 /* libReact.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libReact.a; path = "Pods/../build/Debug-iphoneos/libReact.a"; sourceTree = "<group>"; };
87D1E5311CEC1BC70050ADA3 /* libRNDeviceInfo.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libRNDeviceInfo.a; path = "Pods/../build/Debug-iphoneos/libRNDeviceInfo.a"; sourceTree = "<group>"; };
A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-YH_Vendor.a"; sourceTree = BUILT_PRODUCTS_DIR; };
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>"; };
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 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
87D1E5321CEC1BC70050ADA3 /* libRNDeviceInfo.a in Frameworks */,
875F164E1CCF71820051CA82 /* libReact.a in Frameworks */,
BDB44E315EC65A19AFDFE3D5 /* libPods-YH_Vendor.a in Frameworks */,
);
... ... @@ -98,6 +101,7 @@
4629C9058F02534B1365A58F /* Frameworks */ = {
isa = PBXGroup;
children = (
87D1E5311CEC1BC70050ADA3 /* libRNDeviceInfo.a */,
875F164D1CCF71820051CA82 /* libReact.a */,
A5497DA9F4E284B45CC00DB9 /* libPods-YH_Vendor.a */,
);
... ...
'use strict';
import React from 'react-native';
let {
Component,
View,
Text
} = React;
import RNRF, {
Route,
Scene,
TabBar,
Actions
} from 'react-native-router-flux';
export default class Home extends Component {
render() {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>
<Text>Android Home</Text>
</View>
);
}
}
\ No newline at end of file
'use strict';
import React from 'react-native';
let {
Component,
View,
Text
} = React;
import RNRF, {
Route,
Scene,
TabBar,
Actions
} from 'react-native-router-flux';
export default class Home extends Component {
render() {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>
<Text>iOS Home</Text>
</View>
);
}
}
\ No newline at end of file
'use strict';
import React from 'react-native';
import PlainText from './PlainText';
let {
Component,
... ... @@ -9,8 +10,6 @@ let {
Platform
} = React;
import RNRF, {
Route,
Scene,
... ... @@ -19,21 +18,17 @@ import RNRF, {
} from 'react-native-router-flux';
export default class Home extends Component {
render() {
if (Platform.OS === 'ios') {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>
<Text>Platform iOS </Text>
</View>
);
} else {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center',}}>
<Text>Platform Android</Text>
</View>
);
}
return (
<PlainText
topText={'同品类中品牌排名\n真好玩'}
bottomText={'76'}
containerStyle={{backgroundColor: 'white', width: 200, top: 100}}
/>
);
}
}
\ No newline at end of file
... ...
'use strict';
import React from 'react-native';
let {
Component,
View,
Text,
Platform,
StyleSheet,
} = React;
export default class PlainText extends Component {
static propTypes = {
topText: React.PropTypes.string,
bottomText: React.PropTypes.string,
containerStyle: View.propTypes.style,
topTextStyle: View.propTypes.style,
bottomTextStyle: View.propTypes.style,
};
render() {
return (
<View style={[styles.container, this.props.containerStyle]}>
<Text style={[styles.topText, this.props.topTextStyle]}>{this.props.topText}</Text>
<Text style={[styles.bottomText, this.props.bottomTextStyle]}>{this.props.bottomText}</Text>
</View>
);
}
}
let styles = StyleSheet.create({
container: {
backgroundColor: 'white',
},
topText: {
fontSize: 14,
color: '#444444',
textAlign: 'center',
lineHeight: 20,
marginTop: 20,
marginBottom: 10,
backgroundColor: 'yellow',
},
bottomText: {
fontSize: 17,
color: '#D0021B',
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 20,
backgroundColor: 'yellow',
},
});
\ No newline at end of file
... ...
... ... @@ -25,6 +25,10 @@ export default keyMirror({
RESET_PASSWORD_SUCCESS: null,
RESET_PASSWORD_FAILURE: null,
HOME_OVERVIEW_REQUEST: null,
HOME_OVERVIEW_SUCCESS: null,
HOME_OVERVIEW_FAILURE: null,
/*
GET_PROFILE_REQUEST: null,
GET_PROFILE_SUCCESS: null,
... ...
module.exports = {
API: {
baseUrl: '',
clientSecret: '',
dev: {
// baseUrl: 'http://testapi.yoho.cn:28077',
baseUrl: 'http://service.yoho.cn',
privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
},
prd: {
baseUrl: 'http://service.yoho.cn',
privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
},
storeKey: {
SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY',
GUIDE_STATE_KEY: 'GUIDE_STATE_KEY',
... ...
... ... @@ -89,7 +89,6 @@ class App extends Component {
componentDidMount() {
this.props.actions.getDisplayState();
this.props.actions.login('lily', '111111');
}
render() {
... ...
... ... @@ -68,12 +68,8 @@ class HomeContainer extends Component {
}
var styles = StyleSheet.create({
let styles = StyleSheet.create({
webView: {
backgroundColor: 'gray',
},
});
... ...
... ... @@ -6,3 +6,50 @@
*/
'use strict';
import {Actions} from 'react-native-router-flux';
const {
HOME_OVERVIEW_REQUEST,
HOME_OVERVIEW_SUCCESS,
HOME_OVERVIEW_FAILURE,
} = require('../../constants/actionTypes').default;
export function overviewRequest(shopId) {
return {
type: HOME_OVERVIEW_REQUEST,
payload: shopId
};
}
export function overviewSuccess(json) {
return {
type: HOME_OVERVIEW_SUCCESS,
payload: json
};
}
export function overviewFailure(error) {
return {
type: HOME_OVERVIEW_FAILURE,
payload: error
};
}
export function overview(shopId) {
return dispatch => {
dispatch(overviewRequest());
dispatch(overviewSuccess({
rank: 76,
rise: true,
riseCount: 28,
goodsCount: 7600,
goodsAmount: 19800.00,
}));
};
}
... ...
... ... @@ -15,7 +15,16 @@ import {Record} from 'immutable';
* The fields we're concerned with
*/
let InitialState = Record({
isFetching: false,
error: null,
shopId: 0,
overview: new (Record({
rank: 0,
rise: true,
riseCount: 0,
goodsCount: 0,
goodsAmount: 0,
})),
});
export default InitialState;
... ...
... ... @@ -11,7 +11,13 @@
*/
import InitialState from './homeInitialState';
const {
HOME_OVERVIEW_REQUEST,
HOME_OVERVIEW_SUCCESS,
HOME_OVERVIEW_FAILURE,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
... ... @@ -25,7 +31,28 @@ export default function userReducer(state = initialState, action) {
switch (action.type) {
case HOME_OVERVIEW_REQUEST: {
let nextState = state.set('isFetching', true)
.set('error', null)
.set('shopId', action.payload.shopId);
return nextState;
}
case HOME_OVERVIEW_SUCCESS: {
let nextState = state.set('isFetching', false)
.set('error', null)
.setIn(['overview', 'rank'], action.payload.rank)
.setIn(['overview', 'rise'], action.payload.rise)
.setIn(['overview', 'riseCount'], action.payload.riseCount)
.setIn(['overview', 'goodsCount'], action.payload.goodsCount)
.setIn(['overview', 'goodsAmount'], action.payload.goodsAmount);
return nextState;
}
case HOME_OVERVIEW_FAILURE:
return state.set('isFetching', false)
.set('error', action.payload);
}
... ...
... ... @@ -53,25 +53,24 @@ export function loginFailure(error) {
}
export function login(account, password) {
return dispatch => {
dispatch(loginRequest());
return new UserService().login({
account: account,
password: password
})
.then(function (json) {
console.log(json);
return saveSessionToken(json)
.then(function () {
dispatch(loginSuccess(json));
Actions.Tabbar();
});
})
.catch((error) => {
console.error(error);
dispatch(loginFailure(error));
});
};
// return dispatch => {
// dispatch(loginRequest());
// return new UserService().login({
// account: account,
// password: password
// })
// .then(function (json) {
// return saveSessionToken(json)
// .then(function () {
// dispatch(loginSuccess(json));
// Actions.Tabbar();
// });
// })
// .catch((error) => {
// console.error(error);
// dispatch(loginFailure(error));
// });
// };
}
export function saveSessionToken(json) {
... ...
'use strict';
import React from 'react-native';
import CONFIG from '../constants/config';
import DeviceInfo from 'react-native-device-info';
import queryString from 'query-string';
import md5 from 'md5';
export default class Request {
constructor() {
this.config = process.env.NODE_ENV === `development` ? CONFIG.dev : CONFIG.prd;
this.baseUrl= this.config.baseUrl;
this.privateKey = this.config.privateKey;
}
async get(opts) {
try {
let response = await this._fetch({
method: 'GET',
url: opts.url,
});
let data = await this._parseResponse(response);
return data;
} catch(error) {
throw(error);
}
}
async post(opts) {
try {
let response = await this._fetch({
method: 'POST',
url: opts.url,
body: opts.body,
});
let data = await this._parseResponse(response);
return data;
} catch(error) {
throw(error);
}
}
async _parseResponse(response) {
if (response.status !== 200) {
let res = JSON.parse(response._bodyInit);
throw(res);
}
let json = await response.json();
if (json.code == 200) {
return json.data;
} else {
throw({code: json.code, message: json.message});
}
}
/**
* ### _fetch
*/
async _fetch(opts) {
let defaultOpts = {
method: 'GET',
url: null,
body: null,
callback: null
};
opts = {
...defaultOpts,
...opts
};
opts.body = this._createBody();
let reqOpts = {
method: opts.method,
headers: {
}
};
if (opts.method === 'GET' || opts.method === 'POST') {
reqOpts.headers['Accept'] = 'application/json';
}
let queryStrigPair = this._signParam(opts.body);
if (opts.method === 'GET' || opts.method === 'HEAD' || opts.method === 'DELETE') {
opts.url = opts.url + '?' + queryStrigPair;
}
if (opts.body && opts.method !== 'GET' && opts.method !== 'HEAD') {
reqOpts.body = queryStrigPair;
}
return await fetch(this.baseUrl + opts.url, reqOpts);
}
_publicParams() {
let app_version = DeviceInfo.getBuildNumber();
let os_version = DeviceInfo.getSystemVersion();
let client_type = React.Platform.OS === 'ios' ? 'iphone' : 'android';
let {height, width} = React.Dimensions.get('window');
let screen_size = width + 'x' + height;
return {
app_version,
os_version,
client_type,
screen_size,
};
}
_createBody(body) {
let defaultBody = this._publicParams();
let newBody = {
...defaultBody,
...body
};
return newBody;
}
_signParam(params) {
let private_key = this.privateKey;
let allParams = {
...params,
private_key
}
let paramsPair = queryString.stringify(allParams)
let client_secret = md5(paramsPair);
delete allParams.private_key;
let resultParams = {
...allParams,
client_secret
}
return queryString.stringify(resultParams);
}
};
... ...
'use strict';
import Request from './Request';
export default class UserService {
login(account, password) {
return fetch('http://testapi.yoho.cn:28078/gateway?method=app.passport.signin&account=zhiyuan&password=lzy111111');
constructor () {
this.api = new Request();
}
async login(account, password) {
return this.api.get({
url: '/gateway?method=app.passport.signin&account=zhiyuan&password=lzy111111'
});
//
}
}
\ No newline at end of file
... ...
... ... @@ -32,7 +32,7 @@ const logger = createLogger({
*/
const createStoreWithMiddleware = applyMiddleware(
thunk,
logger
// logger
)(createStore);
/**
... ...
... ... @@ -7,11 +7,15 @@
},
"dependencies": {
"apsl-react-native-button": "^2.4.2",
"frisbee": "^1.0.4",
"immutable": "^3.8.1",
"key-mirror": "^1.0.1",
"md5": "^2.1.0",
"object-assign": "^4.1.0",
"query-string": "^4.1.0",
"react": "^0.14.8",
"react-native": "^0.24.1",
"react-native-device-info": "^0.9.3",
"react-native-drawer": "^2.0.0",
"react-native-navbar": "^1.4.2",
"react-native-router-flux": "^3.22.20",
... ...