userActions.js 4.58 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import UserService from '../../services/UserService';
import {Alert, Linking} from 'react-native';

const {

	DELETE_TOKEN_REQUEST,
    DELETE_TOKEN_SUCCESS,

	GET_STORED_USER_INFO,

	ACCOUNT_CHANGE,
	ACCOUNT_CLEAN,
	PWD_CHANGE,
	PWD_DISPLAY,
    PWD_MODIFY,
    PWD_MODIFY_OLD,
    PWD_MODIFY_NEW,
    PWD_MODIFY_REPEAT,
    PWD_MODIFY_SUBMIT,

    LOGIN_REQUEST,
    LOGIN_SUCCESS,
    LOGIN_FAILURE,

    LOGOUT_REQUEST,
    LOGOUT_SUCCESS,
    LOGOUT_FAILURE,

    SET_SESSION_TOKEN,

    RESET_PASSWORD_REQUEST,
    RESET_PASSWORD_SUCCESS,
    RESET_PASSWORD_FAILURE,


	PHONE_CALL,
	ABOUT_US,
	USER_ITEMS_PRESSED,

	CHECK_UPDATE_SUCCESS,
	CHECK_UPDATE_FAILURE,
	UPDATE_VERSION,

} = require('../../constants/actionTypes').default;

export function userItemsPressed(item) {
	switch (item.index) {
		case 0:
            Linking.openURL('tel:025-87781000');
			return {
				type: PHONE_CALL,

			};
		case 1:
			Actions.AboutUs();
			return {
				type: ABOUT_US,
			};

	}
}

export function checkUpdate() {

	return (dispatch) => {
		return new UserService().checkUpdate()
			.then((json) => {
				dispatch(checkUpdateSuccess(json))
			})
			.catch((error) => {
				dispatch(checkUpdateFailure(error))
			});
	};
}

export function checkUpdateSuccess(json) {
	return {
		type: CHECK_UPDATE_SUCCESS,
		payload: json,
	};
}

export function checkUpdateFailure(error) {
	return {
		type: CHECK_UPDATE_FAILURE,
		payload: error,
	};
}

export function updateVersion(url) {
	Linking.openURL(url);
	return {
		type: UPDATE_VERSION,
	}
}

export function loginRequest() {

    return {
        type: LOGIN_REQUEST
    };
}

export function loginSuccess(json) {
    console.log(json);
    return {
        type: LOGIN_SUCCESS,
        payload: json
    };
}

export function loginFailure(error) {
    return {
        type: LOGIN_FAILURE,
        payload: error
    };
}
export function accountChange(account) {
    return {
        type: ACCOUNT_CHANGE,
		payload: account
    };
}

export function accountClean() {
    return {
        type: ACCOUNT_CLEAN,
    };
}

export function pwdChange(pwd) {
    return {
        type: PWD_CHANGE,
		payload: pwd
    };
}
export function pwdDisplay(ishow) {
    return {
        type: PWD_DISPLAY,
		payload: ishow
    };
}

export function logout() {
    new UserService().deleteUserInfo();
    Actions.pop();
    Actions.Login();
    return {
        type: LOGOUT_REQUEST
    };
}

export function pwdModify() {
    Actions.Modify();
    return {
        type: PWD_MODIFY
    };
}

export function pwdModifyOld(pwd) {

    return {
        type: PWD_MODIFY_OLD,
        payload: pwd
    };
}

export function pwdModifyNew(pwd) {
       return {
        type: PWD_MODIFY_NEW,
        payload: pwd
    };
}

export function pwdModifyRepeat(pwd) {

    return {
        type: PWD_MODIFY_REPEAT,
        payload: pwd
    };
}

export function pwdModifySubmit(pid, oldPwd, newPwd) {
    let userService = new UserService();
    return dispatch => {
        dispatch(loginRequest());
        return userService.modifyPassword(pid, oldPwd, newPwd)
            .then(function (json) {
                Alert.alert(
                    '修改成功',
                    '',
                    [
                        {text: 'OK', onPress: () => Actions.pop()},
                    ]
                );
            dispatch(modifySubmitDone());
            
        })
        .catch((error) => {
            Alert.alert(
                    error.message,
                    '',
                    [{text: 'OK'}]
                );
                dispatch(loginFailure(error));
        });
    };
}


export function login(account, password) {
	let userService = new UserService();
	return dispatch => {
		dispatch(loginRequest());
		return userService.login(account,password)
			.then(function (json) {
				return userService.storeUserInfo(json)
					.then(() => {
						dispatch(loginSuccess(json));
						Actions.Drawer();
					});
			})
			.catch((error) => {
				Alert.alert(
					error.message?error.message:'登录失败',
					'',
					[{text: 'OK'}]
				);
				dispatch(loginFailure(error));
			});
	};

}


export function modifySubmitDone() {
    Actions.pop();
     return {
        type: PWD_MODIFY_SUBMIT
    };
}

export function getStoredUserInfo() {

	return dispatch => {
		new UserService().getUserInfo()
			.then(userInfo => {
				if (userInfo) {
					dispatch(storedUserInfo(userInfo));
				}
			});

	};
}

export function storedUserInfo(userInfo) {
	return {
		type: GET_STORED_USER_INFO,
		payload: userInfo
	}
}