userActions.js 5.05 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_REQUEST,
    PWD_MODIFY_SUCCESS,
    PWD_MODIFY_FAILURE,

    LOGIN_REQUEST,
    LOGIN_SUCCESS,
    LOGIN_FAILURE,

    LOGOUT_REQUEST,
    LOGOUT_SUCCESS,
    LOGOUT_FAILURE,

    SET_SESSION_TOKEN,

	PHONE_CALL,
	ABOUT_US,
	USER_ITEMS_PRESSED,

	CHECK_UPDATE_SUCCESS,
	CHECK_UPDATE_FAILURE,
	UPDATE_VERSION,

	SETUP_DEFAULT_SHOP,

	LOGOUT,

} = 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) {
    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
    };
}

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 modifySubmitRequest() {
     return {
        type: PWD_MODIFY_REQUEST
    };
}

export function modifySubmitSuccess() {
     return {
        type: PWD_MODIFY_SUCCESS
    };
}

export function modifySubmitFailure() {
     return {
        type: PWD_MODIFY_FAILURE
    };
}

export function pwdModifySubmit(pid, oldPwd, newPwd,newPwdConfirm) {
    let userService = new UserService();
    return dispatch => {
        dispatch(modifySubmitRequest());
        return userService.modifyPassword(pid, oldPwd, newPwd,newPwdConfirm)
            .then(function (json) {
				dispatch(modifySubmitSuccess());
				Alert.alert(
	                '修改成功',
	                '',
	                [{text: 'OK', onPress: () => {
						Actions.pop();
						setTimeout(() => {Actions.pop()}, 0);
					}}]
	            );
        })
        .catch((error) => {
            Alert.alert(
                error.message || '服务错误',
                '',
                [{text: 'OK'}]
            );
            dispatch(modifySubmitFailure(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));
						dispatch(setupHomeDefaultShop(json.shops[0].shopsId, json.shops[0].shopName));
						Actions.Drawer();
					});
			})
			.catch((error) => {
				Alert.alert(
					'登录消息',
					error.message?error.message:'登录失败',
					[{text: 'OK'}]
				);
				dispatch(loginFailure(error));
			});
	};

}

export function getStoredUserInfo() {

	return dispatch => {
		new UserService().getUserInfo()
			.then(userInfo => {
				if (userInfo) {
					dispatch(storedUserInfo(userInfo));
					dispatch(setupHomeDefaultShop(userInfo.shops[0].shopsId, userInfo.shops[0].shopName));
				}
			});

	};
}

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

export function setupHomeDefaultShop(shopId, shopName) {
	return {
		type: SETUP_DEFAULT_SHOP,
		payload: {
			shopId,
			shopName
		}
	}
}