Commit a4d9fd27 authored by 2breakegg's avatar 2breakegg

error.messages

parent 96f13a64
import { login, forgot, register, reset, activate, checkUserExists } from '../services/auth' import { message } from 'antd';
import { message } from 'antd' import { routerRedux } from 'dva/router';
import { routerRedux } from 'dva/router' import { activate, checkUserExists, forgot, login, register, reset } from '../services/auth';
import config from '../config'
export default { export default {
...@@ -30,23 +28,26 @@ export default { ...@@ -30,23 +28,26 @@ export default {
}, },
checkEmail(state, action) { checkEmail(state, action) {
return { return {
...state, ...{ ...state,
...{
checkEmail: 'validating', checkEmail: 'validating',
} },
}; };
}, },
checkUsername(state, action) { checkUsername(state, action) {
return { return {
...state, ...{ ...state,
...{
checkUsername: 'validating', checkUsername: 'validating',
} },
}; };
}, },
activate(state, action) { activate(state, action) {
return { return {
...state, ...{ ...state,
...{
activateState: true, activateState: true,
} },
}; };
}, },
check(state, action) { check(state, action) {
...@@ -56,86 +57,99 @@ export default { ...@@ -56,86 +57,99 @@ export default {
}, },
register(state, action) { register(state, action) {
return { return {
...state, ...{ ...state,
...{
isRegisterSubmit: true, isRegisterSubmit: true,
} }
}; };
}, },
registerSuccess(state, action) { registerSuccess(state, action) {
return { return {
...state, ...{ ...state,
...{
isRegisterSubmit: false, isRegisterSubmit: false,
} },
}; };
}, },
registerFail(state, action) { registerFail(state, action) {
return { return {
...state, ...{ ...state,
...{
isRegisterSubmit: false, isRegisterSubmit: false,
} },
}; };
}, },
login(state, action) { login(state, action) {
return { return {
...state, ...{ ...state,
...{
isLoginSubmit: true, isLoginSubmit: true,
} },
}; };
}, },
loginSuccess(state, action) { loginSuccess(state, action) {
return { return {
...state, ...action.payload, ...{ ...state,
...action.payload,
...{
isLoginSubmit: false, isLoginSubmit: false,
} },
}; };
}, },
loginFail(state, action) { loginFail(state, action) {
return { return {
...state, ...{ ...state,
...{
isLoginSubmit: false, isLoginSubmit: false,
} },
}; };
}, },
forgot(state, action) { forgot(state, action) {
return { return {
...state, ...{ ...state,
...{
isForgotSubmit: true, isForgotSubmit: true,
} },
}; };
}, },
forgotSuccess(state, action) { forgotSuccess(state, action) {
return { return {
...state, ...{ ...state,
...{
isForgotSubmit: false, isForgotSubmit: false,
} },
}; };
}, },
forgotFail(state, action) { forgotFail(state, action) {
return { return {
...state, ...{ ...state,
...{
isForgotSubmit: false, isForgotSubmit: false,
} },
}; };
}, },
reset(state, action) { reset(state, action) {
return { return {
...state, ...{ ...state,
...{
isResetSubmit: true, isResetSubmit: true,
} },
}; };
}, },
resetSuccess(state, action) { resetSuccess(state, action) {
return { return {
...state, ...{ ...state,
...{
isResetSubmit: false, isResetSubmit: false,
} },
}; };
}, },
resetFail(state, action) { resetFail(state, action) {
return { return {
...state, ...{ ...state,
...{
isResetSubmit: false, isResetSubmit: false,
} },
}; };
}, },
}, },
...@@ -193,7 +207,7 @@ export default { ...@@ -193,7 +207,7 @@ export default {
} }
} catch (error) { } catch (error) {
yield put({ type: 'loginFail' }); yield put({ type: 'loginFail' });
message.error(messages[error.message]); message.error(messages[error.message] || error.message);
} }
}, },
*forgot({ payload }, { call, put, select }) { *forgot({ payload }, { call, put, select }) {
...@@ -206,7 +220,7 @@ export default { ...@@ -206,7 +220,7 @@ export default {
} }
} catch (error) { } catch (error) {
yield put({ type: 'forgotFail' }); yield put({ type: 'forgotFail' });
message.error(messages[error.message], 3); message.error(messages[error.message] || error.message, 3);
} }
}, },
*register({ payload }, { call, put, select }) { *register({ payload }, { call, put, select }) {
...@@ -223,7 +237,7 @@ export default { ...@@ -223,7 +237,7 @@ export default {
} }
} catch (error) { } catch (error) {
yield put({ type: 'registerFail' }); yield put({ type: 'registerFail' });
message.error(messages[error.message], 3); message.error(messages[error.message] || error.message, 3);
} }
}, },
*reset({ payload }, { call, put, select }) { *reset({ payload }, { call, put, select }) {
...@@ -236,10 +250,9 @@ export default { ...@@ -236,10 +250,9 @@ export default {
} }
} catch (error) { } catch (error) {
yield put({ type: 'resetFail' }); yield put({ type: 'resetFail' });
message.error(messages[error.message], 3); message.error(messages[error.message] || error.message, 3);
} }
}, },
}, },
subscriptions: { subscriptions: {},
}
}; };
import { routerRedux } from 'dva/router' import { message } from 'antd';
import { updateProfile, updateAccount } from '../services/user' import { routerRedux } from 'dva/router';
import { getAuthUser } from '../services/auth' import { getAuthUser } from '../services/auth';
import { message } from 'antd' import { updateAccount, updateProfile } from '../services/user';
import { handleSSO } from '../utils/sso' import { handleSSO } from '../utils/sso';
export default { export default {
namespace: 'user', namespace: 'user',
state: { state: {
token: "", token: '',
user: {}, user: {},
isUpdateSubmit: false isUpdateSubmit: false,
}, },
reducers: { reducers: {
loginFromStorage(state, action) { loginFromStorage(state, action) {
return { return {
...state, ...action.payload ...state, ...action.payload
} };
}, },
loginSuccess(state, action) { loginSuccess(state, action) {
return { return {
...state, ...action.payload.data ...state, ...action.payload.data
} };
}, },
updateProfile(state, action) { updateProfile(state, action) {
return { return {
...state, ...{ ...state,
isUpdateSubmit: true ...{
} isUpdateSubmit: true,
} },
};
}, },
updateProfileSuccess(state, action) { updateProfileSuccess(state, action) {
return { return {
...state, ...action.payload, ...{ ...state,
isUpdateSubmit: false ...action.payload,
} ...{
} isUpdateSubmit: false,
},
};
}, },
updateProfileFail(state, action) { updateProfileFail(state, action) {
return { return {
...state, ...{ ...state,
isUpdateSubmit: false ...{
} isUpdateSubmit: false,
} },
};
}, },
updateAccount(state, action) { updateAccount(state, action) {
return { return {
...state, ...{ ...state,
isUpdateSubmit: true ...{
} isUpdateSubmit: true,
} },
};
}, },
updateAccountSuccess(state, action) { updateAccountSuccess(state, action) {
return { return {
...state, ...action.payload, ...{ ...state,
isUpdateSubmit: false ...action.payload,
} ...{
} isUpdateSubmit: false,
},
};
}, },
updateAccountFail(state, action) { updateAccountFail(state, action) {
return { return {
...state, ...{ ...state,
isUpdateSubmit: false ...{
} isUpdateSubmit: false,
} },
};
}, },
storeToken(state, action) { storeToken(state, action) {
return { return {
...state, ...action.payload ...state, ...action.payload,
} };
}, },
preLoginSuccess(state, action) { preLoginSuccess(state, action) {
return { return {
...state, ...action.payload ...state, ...action.payload,
} };
}, },
getAuthUserSuccess(state, action) { getAuthUserSuccess(state, action) {
return { return {
...state, ...action.payload ...state, ...action.payload,
} };
} },
}, },
effects: { effects: {
*loginSuccess({ payload }, { call, put }) { *loginSuccess({ payload }, { call, put }) {
const { data: {user, token} } = payload const { data: { user, token } } = payload;
if(!payload.data) { if (!payload.data) {
message.error("error ") message.error('error ');
} }
if(token) { if (token) {
yield put({ type: 'storeToken', payload: { token }}) yield put({ type: 'storeToken', payload: { token } });
localStorage.setItem("token", token) localStorage.setItem('token', token);
} }
if(user) { if (user) {
if(handleSSO(user)){ if (handleSSO(user)) {
return return;
} }
if(user.active) { if (user.active) {
yield put(routerRedux.replace("/profiles")) yield put(routerRedux.replace('/profiles'));
// message.info("登录成功") // message.info("登录成功")
} else { } else {
yield put(routerRedux.replace(`/verify`)) yield put(routerRedux.replace('/verify'));
} }
} }
}, },
*getAuthUser({ payload }, { call, put, select }) { *getAuthUser({ payload }, { call, put, select }) {
const { token } = payload const { token } = payload;
try { try {
let { data } = yield call(getAuthUser, { token }) let { data } = yield call(getAuthUser, { token });
if(data) { if (data) {
yield put({ type: 'getAuthUserSuccess', payload: { user: data, token }}) yield put({ type: 'getAuthUserSuccess', payload: { user: data, token } });
} }
}catch(error) { } catch (error) {
message.error(error.message) message.error(error.message);
} }
}, },
*preLogin({ payload }, { call, put, select }) { *preLogin({ payload }, { call, put, select }) {
const {token } = payload const { token } = payload;
if(!token) { if (!token) {
yield put(routerRedux.replace("/signin")) yield put(routerRedux.replace('/signin'));
} }
try { try {
let { data } = yield call(getAuthUser, { token }) let { data } = yield call(getAuthUser, { token });
if (data) { if (data) {
if(handleSSO(data)){ if (handleSSO(data)) {
return return;
} }
if(data.active) { if (data.active) {
yield put(routerRedux.replace("/profiles")) yield put(routerRedux.replace('/profiles'));
}else { } else {
yield put(routerRedux.replace("/verify")) yield put(routerRedux.replace('/verify'));
} }
} }
} catch (error) { } catch (error) {
yield put(routerRedux.replace("/signin")) yield put(routerRedux.replace('/signin'));
message.error(error.message) message.error(error.message);
} }
}, },
*updateProfile({ payload }, { call, put, select }) { *updateProfile({ payload }, { call, put, select }) {
message.destroy() message.destroy();
let token = yield select(state => state.user.token) let token = yield select(state => state.user.token);
let { messages } = yield select(state => state.common) let { messages } = yield select(state => state.common);
try { try {
let { data } = yield call(updateProfile, {...payload, token}) let { data } = yield call(updateProfile, { ...payload, token });
if (data) { if (data) {
yield put({ type: 'updateProfileSuccess', payload: { user: data, token } }) yield put({ type: 'updateProfileSuccess', payload: { user: data, token } });
message.info(messages["update_success"]) message.info(messages.update_success);
} }
} catch (error) { } catch (error) {
yield put({ type: 'updateProfileFail' }) yield put({ type: 'updateProfileFail' });
message.error(error.message) message.error(error.message);
} }
}, },
*updateEmail({ payload }, { call, put, select }) { *updateEmail({ payload }, { call, put, select }) {
let { messages } = yield select(state => state.common) let { messages } = yield select(state => state.common);
try { try {
let token = yield select(state => state.user.token) let token = yield select(state => state.user.token);
let { data } = yield call(updateAccount, {...payload, token}) let { data } = yield call(updateAccount, { ...payload, token });
if (data) { if (data) {
yield put({ type: 'updateAccountSuccess', payload: { user: data, token } }) yield put({ type: 'updateAccountSuccess', payload: { user: data, token } });
message.info(messages['A-verification-email-has-been-sent-to-you,please-check-the-mail-to-complete.']) message.info(messages['A-verification-email-has-been-sent-to-you,please-check-the-mail-to-complete.']);
} }
} catch (error) { } catch (error) {
yield put({ type: 'updateAccountFail' }) yield put({ type: 'updateAccountFail' });
message.error(messages[error.message]) message.error(messages[error.message] || error.message);
} }
}, },
*updateAccount({ payload }, { call, put, select }) { *updateAccount({ payload }, { call, put, select }) {
let { messages } = yield select(state => state.common) let { messages } = yield select(state => state.common);
try { try {
let token = yield select(state => state.user.token) let token = yield select(state => state.user.token);
let { data } = yield call(updateAccount, {...payload, token}) let { data } = yield call(updateAccount, { ...payload, token });
if (data) { if (data) {
yield put({ type: 'updateAccountSuccess', payload: { user: data, token } }) yield put({ type: 'updateAccountSuccess', payload: { user: data, token } });
message.info(messages["update_success"]) message.info(messages.update_success);
} }
} catch (error) { } catch (error) {
yield put({ type: 'updateAccountFail' }) yield put({ type: 'updateAccountFail' });
message.error(messages[error.message]) message.error(messages[error.message] || error.message);
} }
}, },
}, },
subscriptions: { subscriptions: {
setup({ dispatch, history }) { setup({ dispatch, history }) {
let token = localStorage.getItem("token") let token = localStorage.getItem('token');
if (token) { if (token) {
dispatch({type: 'getAuthUser', payload: { token }}) dispatch({ type: 'getAuthUser', payload: { token } });
} }
history.listen(({ pathname, query }) => { history.listen(({ pathname, query }) => {
if(pathname == '/') { if (pathname == '/') {
dispatch({ type: 'preLogin', payload: { token } }) dispatch({ type: 'preLogin', payload: { token } });
} }
}) });
} },
}, },
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment