Authored by 于良

代码目录结构调整

@@ -22,7 +22,7 @@ import RNRF, { @@ -22,7 +22,7 @@ import RNRF, {
22 Actions 22 Actions
23 } from 'react-native-router-flux'; 23 } from 'react-native-router-flux';
24 24
25 -import configureStore from './lib/configureStore'; 25 +import configureStore from './store/configureStore';
26 26
27 import { 27 import {
28 setPlatform, 28 setPlatform,
@@ -8,22 +8,9 @@ export default keyMirror({ @@ -8,22 +8,9 @@ export default keyMirror({
8 GET_GUIDE_DISPLAY: null, 8 GET_GUIDE_DISPLAY: null,
9 SET_GUIDE_DISPLAY: null, 9 SET_GUIDE_DISPLAY: null,
10 10
11 - /*  
12 - SESSION_TOKEN_REQUEST: null,  
13 - SESSION_TOKEN_SUCCESS: null,  
14 - SESSION_TOKEN_FAILURE: null,  
15 -  
16 DELETE_TOKEN_REQUEST: null, 11 DELETE_TOKEN_REQUEST: null,
17 DELETE_TOKEN_SUCCESS: null, 12 DELETE_TOKEN_SUCCESS: null,
18 13
19 - ON_LOGIN_STATE_CHANGE: null,  
20 - LOGOUT: null,  
21 -  
22 - ON_AUTH_FORM_FIELD_CHANGE: null,  
23 - SIGNUP_REQUEST: null,  
24 - SIGNUP_SUCCESS: null,  
25 - SIGNUP_FAILURE: null,  
26 -  
27 LOGIN_REQUEST: null, 14 LOGIN_REQUEST: null,
28 LOGIN_SUCCESS: null, 15 LOGIN_SUCCESS: null,
29 LOGIN_FAILURE: null, 16 LOGIN_FAILURE: null,
@@ -32,15 +19,13 @@ export default keyMirror({ @@ -32,15 +19,13 @@ export default keyMirror({
32 LOGOUT_SUCCESS: null, 19 LOGOUT_SUCCESS: null,
33 LOGOUT_FAILURE: null, 20 LOGOUT_FAILURE: null,
34 21
35 - LOGGED_IN: null,  
36 - LOGGED_OUT: null,  
37 -  
38 SET_SESSION_TOKEN: null, 22 SET_SESSION_TOKEN: null,
39 23
40 RESET_PASSWORD_REQUEST: null, 24 RESET_PASSWORD_REQUEST: null,
41 RESET_PASSWORD_SUCCESS: null, 25 RESET_PASSWORD_SUCCESS: null,
42 RESET_PASSWORD_FAILURE: null, 26 RESET_PASSWORD_FAILURE: null,
43 27
  28 + /*
44 GET_PROFILE_REQUEST: null, 29 GET_PROFILE_REQUEST: null,
45 GET_PROFILE_SUCCESS: null, 30 GET_PROFILE_SUCCESS: null,
46 GET_PROFILE_FAILURE: null, 31 GET_PROFILE_FAILURE: null,
  1 +module.exports = {
  2 + API: {
  3 + baseUrl: '',
  4 + clientSecret: '',
  5 +
  6 + },
  7 + storeKey: {
  8 + SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY',
  9 + GUIDE_STATE_KEY: 'GUIDE_STATE_KEY',
  10 + }
  11 +}
@@ -16,6 +16,7 @@ import {Map} from 'immutable'; @@ -16,6 +16,7 @@ import {Map} from 'immutable';
16 * Project actions 16 * Project actions
17 */ 17 */
18 import * as guideActions from '../reducers/guide/guideActions'; 18 import * as guideActions from '../reducers/guide/guideActions';
  19 +import * as userActions from '../reducers/user/userActions';
19 20
20 /** 21 /**
21 * The components we need from ReactNative 22 * The components we need from ReactNative
@@ -36,6 +37,7 @@ from 'react-native'; @@ -36,6 +37,7 @@ from 'react-native';
36 */ 37 */
37 const actions = [ 38 const actions = [
38 guideActions, 39 guideActions,
  40 + userActions,
39 ]; 41 ];
40 42
41 /** 43 /**
@@ -86,6 +88,8 @@ class App extends Component { @@ -86,6 +88,8 @@ class App extends Component {
86 88
87 componentDidMount() { 89 componentDidMount() {
88 this.props.actions.getDisplayState(); 90 this.props.actions.getDisplayState();
  91 +
  92 + this.props.actions.login('lily', '111111');
89 } 93 }
90 94
91 render() { 95 render() {
@@ -17,7 +17,7 @@ const { @@ -17,7 +17,7 @@ const {
17 SET_PLATFORM, 17 SET_PLATFORM,
18 SET_VERSION, 18 SET_VERSION,
19 SHOW_DOT 19 SHOW_DOT
20 -} = require ('../../lib/constants').default; 20 +} = require('../../constants/actionTypes').default;
21 21
22 /** 22 /**
23 * ## Set the platformState 23 * ## Set the platformState
@@ -18,7 +18,7 @@ const { @@ -18,7 +18,7 @@ const {
18 SET_PLATFORM, 18 SET_PLATFORM,
19 SET_VERSION, 19 SET_VERSION,
20 SHOW_DOT 20 SHOW_DOT
21 -} = require('../../lib/constants').default; 21 +} = require('../../constants/actionTypes').default;
22 22
23 const initialState = new InitialState; 23 const initialState = new InitialState;
24 24
@@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
14 const { 14 const {
15 GET_GUIDE_DISPLAY, 15 GET_GUIDE_DISPLAY,
16 SET_GUIDE_DISPLAY 16 SET_GUIDE_DISPLAY
17 -} = require ('../../lib/constants').default; 17 +} = require('../../constants/actionTypes').default;
18 18
19 19
20 import {Actions} from 'react-native-router-flux'; 20 import {Actions} from 'react-native-router-flux';
@@ -17,7 +17,7 @@ import InitialState from './guideInitialState'; @@ -17,7 +17,7 @@ import InitialState from './guideInitialState';
17 const { 17 const {
18 GET_GUIDE_DISPLAY, 18 GET_GUIDE_DISPLAY,
19 SET_GUIDE_DISPLAY 19 SET_GUIDE_DISPLAY
20 -} = require('../../lib/constants').default; 20 +} = require('../../constants/actionTypes').default;
21 21
22 const initialState = new InitialState; 22 const initialState = new InitialState;
23 23
@@ -6,3 +6,74 @@ @@ -6,3 +6,74 @@
6 */ 6 */
7 'use strict'; 7 'use strict';
8 8
  9 +import {Actions} from 'react-native-router-flux';
  10 +import AppAuthToken from '../../services/AppAuthToken';
  11 +import UserService from '../../services/UserService';
  12 +
  13 +const {
  14 +
  15 + DELETE_TOKEN_REQUEST,
  16 + DELETE_TOKEN_SUCCESS,
  17 +
  18 + LOGIN_REQUEST,
  19 + LOGIN_SUCCESS,
  20 + LOGIN_FAILURE,
  21 +
  22 + LOGOUT_REQUEST,
  23 + LOGOUT_SUCCESS,
  24 + LOGOUT_FAILURE,
  25 +
  26 + SET_SESSION_TOKEN,
  27 +
  28 + RESET_PASSWORD_REQUEST,
  29 + RESET_PASSWORD_SUCCESS,
  30 + RESET_PASSWORD_FAILURE,
  31 +
  32 +} = require('../../constants/actionTypes').default;
  33 +
  34 +
  35 +export function loginRequest() {
  36 + return {
  37 + type: LOGIN_REQUEST
  38 + };
  39 +}
  40 +
  41 +export function loginSuccess(json) {
  42 + return {
  43 + type: LOGIN_SUCCESS,
  44 + payload: json
  45 + };
  46 +}
  47 +
  48 +export function loginFailure(error) {
  49 + return {
  50 + type: LOGIN_FAILURE,
  51 + payload: error
  52 + };
  53 +}
  54 +
  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 + };
  75 +}
  76 +
  77 +export function saveSessionToken(json) {
  78 + return new AppAuthToken().storeSessionToken(json);
  79 +}
@@ -7,15 +7,29 @@ @@ -7,15 +7,29 @@
7 /** 7 /**
8 * ## Import immutable record 8 * ## Import immutable record
9 */ 9 */
10 -import {Record} from 'immutable'; 10 +import {List, Record} from 'immutable';
11 11
12 /** 12 /**
13 * ## InitialState 13 * ## InitialState
14 * 14 *
  15 + * shops: List of
  16 + * new (Record({
  17 + * id: '',
  18 + * name: '',
  19 + * }))
  20 + *
15 * The fields we're concerned with 21 * The fields we're concerned with
16 */ 22 */
17 let InitialState = Record({ 23 let InitialState = Record({
18 - 24 + error: null,
  25 + isValid: false,
  26 + isFetching: false,
  27 + profile: new (Record({
  28 + account: '',
  29 + pid: '',
  30 + sessionKey: '',
  31 + })),
  32 + shops: List(),
19 }); 33 });
20 34
21 export default InitialState; 35 export default InitialState;
@@ -11,7 +11,28 @@ @@ -11,7 +11,28 @@
11 */ 11 */
12 import InitialState from './userInitialState'; 12 import InitialState from './userInitialState';
13 13
  14 +import Immutable, {List, Record} from 'immutable';
14 15
  16 +const {
  17 +
  18 + DELETE_TOKEN_REQUEST,
  19 + DELETE_TOKEN_SUCCESS,
  20 +
  21 + LOGIN_REQUEST,
  22 + LOGIN_SUCCESS,
  23 + LOGIN_FAILURE,
  24 +
  25 + LOGOUT_REQUEST,
  26 + LOGOUT_SUCCESS,
  27 + LOGOUT_FAILURE,
  28 +
  29 + SET_SESSION_TOKEN,
  30 +
  31 + RESET_PASSWORD_REQUEST,
  32 + RESET_PASSWORD_SUCCESS,
  33 + RESET_PASSWORD_FAILURE,
  34 +
  35 +} = require('../../constants/actionTypes').default;
15 36
16 const initialState = new InitialState; 37 const initialState = new InitialState;
17 38
@@ -25,9 +46,46 @@ export default function userReducer(state = initialState, action) { @@ -25,9 +46,46 @@ export default function userReducer(state = initialState, action) {
25 46
26 switch (action.type) { 47 switch (action.type) {
27 48
  49 + case LOGOUT_REQUEST:
  50 + case LOGIN_REQUEST:
  51 + case RESET_PASSWORD_REQUEST: {
  52 + let nextState = state.set('isFetching', true)
  53 + .set('error', null);
  54 + return nextState;
  55 + }
  56 +
  57 + case LOGIN_SUCCESS: {
  58 + const {account, pid, sessionKey, shops} = action.payload;
  59 + let ShopRecord = Record({ id: '', name: '' })
  60 + let shopsList = Immutable.fromJS(shops, function(key, value) {
  61 + return new ShopRecord(value);
  62 + });
  63 + let nextState = state.set('isFetching', false)
  64 + .set('error', null)
  65 + .setIn(['profile', 'account'], account)
  66 + .setIn(['profile', 'pid'], pid)
  67 + .setIn(['profile', 'sessionKey'], sessionKey)
  68 + .set('shops', shopsList);
  69 + return nextState;
  70 + }
28 71
  72 + case LOGOUT_SUCCESS:
  73 + case RESET_PASSWORD_SUCCESS:
  74 + return state.set('isFetching', false);
29 75
  76 + case LOGOUT_FAILURE:
  77 + case LOGIN_FAILURE:
  78 + case RESET_PASSWORD_FAILURE:
  79 + return state.set('isFetching', false)
  80 + .set('error', action.payload);
  81 +
  82 + case DELETE_TOKEN_REQUEST:
  83 + case DELETE_TOKEN_SUCCESS:
  84 + return state;
30 } 85 }
31 86
  87 + /**
  88 + * ## Default
  89 + */
32 return state; 90 return state;
33 } 91 }
  1 +'use strict';
  2 +
  3 +import store from 'react-native-simple-store';
  4 +import CONFIG from '../constants/config';
  5 +
  6 +export default class AppAuthToken {
  7 + /**
  8 + * ## AppAuthToken
  9 + *
  10 + * set the key from the config
  11 + */
  12 + constructor () {
  13 + this.SESSION_TOKEN_KEY = CONFIG.storeKey.SESSION_TOKEN_KEY;
  14 + }
  15 +
  16 + /**
  17 + * ### storeSessionToken
  18 + * Store the session key
  19 + */
  20 + storeSessionToken(sessionToken) {
  21 + return store.save(this.SESSION_TOKEN_KEY, {
  22 + sessionToken: sessionToken
  23 + });
  24 +
  25 + }
  26 + /**
  27 + * ### getSessionToken
  28 + */
  29 + getSessionToken() {
  30 + return store.get(this.SESSION_TOKEN_KEY);
  31 + }
  32 + /**
  33 + * ### deleteSessionToken
  34 + * Deleted during log out
  35 + */
  36 + deleteSessionToken() {
  37 + return store.delete(this.SESSION_TOKEN_KEY);
  38 + }
  39 +}
  40 +
  1 +'use strict';
  2 +
  3 +export default class UserService {
  4 +
  5 + login(account, password) {
  6 + return fetch('http://testapi.yoho.cn:28078/gateway?method=app.passport.signin&account=zhiyuan&password=lzy111111');
  7 + }
  8 +
  9 +}