Login & Register

This commit is contained in:
Limo Saplf
2019-09-01 21:56:21 +08:00
parent d9a8408535
commit 4480fc8e19
12 changed files with 231 additions and 23 deletions

View File

@@ -5,7 +5,7 @@ import { delay } from 'dva/saga';
import { PageState, AreaCode, AreaCodes } from '@/types/common';
import { getAreaCodes, getUserInfo } from '@/services/common';
import { validPhoneInput, validPwdInput, validPhone, validPwd } from '@/utils/validate';
import { checkExistence, CheckResult, signIn } from './service';
import { checkExistence, CheckResult, signIn, signUp, sendCode } from './service';
import { formatMessage } from 'umi-plugin-locale';
export enum PageStep {
@@ -23,7 +23,12 @@ export interface CheckModelState {
areacode: AreaCode | undefined, // 选中的地区
phone: string;
password: string;
pwdConfirm: string;
inviter: string;
verifyCode: string;
submitting: boolean;
sendingCode: boolean;
codeTimer: number;
}
const model: Model = {
@@ -36,7 +41,12 @@ const model: Model = {
areacode: undefined,
phone: '',
password: '',
pwdConfirm: '',
inviter: '',
verifyCode: '',
submitting: false,
sendingCode: false,
codeTimer: -1,
} as CheckModelState,
effects: {
@@ -71,6 +81,15 @@ const model: Model = {
}
},
*timer({ payload }: any, { put }) {
yield put({ type: 'onUpdateState', codeTimer: payload });
yield delay(1000);
const next = payload - 1;
if (next >= 0) {
yield put({ type: 'timer', payload: next });
}
},
*submitCheck(_, { put, call, select }) {
const { areacode, phone } = yield select((state: any) => state.check);
if (!areacode) return message.warn(formatMessage({ id: 'check.msgArea' }));
@@ -97,26 +116,61 @@ const model: Model = {
const { areacode, phone, password } = yield select((state: any) => state.check);
if (!areacode) return message.warn(formatMessage({ id: 'check.msgArea' }));
if (!validPhone(phone)) return message.warn(formatMessage({ id: 'check.msgPhone' }));
if (!validPwd(password)) return message.warn(formatMessage({ id: 'check.msgPwd' }))
if (!validPwd(password)) return message.warn(formatMessage({ id: 'check.msgPwd' }));
yield put({ type: 'onUpdateState', submitting: true });
try {
const token = yield call(signIn, areacode, phone, password);
yield put({ type: 'user/updateToken', payload: token });
yield delay(0);
const user = yield call(getUserInfo, true);
yield put({ type: 'user/updateUser', payload: user });
} catch(e) {
message.error(e.message);
yield put({ type: 'user/logout' });
} finally {
yield put({ type: 'onUpdateState', submitting: false });
}
},
*submitRegister(_, { put }) {
*submitRegister(_, { put, select, call }) {
const { areacode, phone, password, verifyCode, inviter, pwdConfirm } = yield select((state: any) => state.check);
if (!areacode) return message.warn(formatMessage({ id: 'check.msgArea' }));
if (!validPhone(phone)) return message.warn(formatMessage({ id: 'check.msgPhone' }));
if (!verifyCode) return message.warn(formatMessage({ id: 'check.msgCode' }));
if (!validPwd(password)) return message.warn(formatMessage({ id: 'check.msgPwd' }));
if (password !== pwdConfirm) return message.warn(formatMessage({ id: 'check.msgConfirm' }));
// if (!inviter) return message.warn(formatMessage({ id: 'check.msgInviter' }));
yield put({ type: 'onUpdateState', submitting: true });
yield delay(800);
yield put({ type: 'onUpdateState', submitting: false });
yield put({ type: 'onUpdateState', pageStep: PageStep.Check });
try {
const token = yield call(signUp, areacode, phone, password, verifyCode, inviter);
yield put({ type: 'user/updateToken', payload: token });
yield delay(0);
const user = yield call(getUserInfo, true);
yield put({ type: 'user/updateUser', payload: user });
} catch(e) {
message.error(e.message);
yield put({ type: 'user/logout' });
} finally {
yield put({ type: 'onUpdateState', submitting: false });
}
},
*sendCode(_, { select, put, call }) {
const { areacode, phone } = yield select((state: any) => state.check);
if (!areacode) return message.warn(formatMessage({ id: 'check.msgArea' }));
if (!validPhone(phone)) return message.warn(formatMessage({ id: 'check.msgPhone' }));
yield put({ type: 'onUpdateState', sendingCode: true });
try {
yield call(sendCode, areacode, phone);
yield put({ type: 'timer', payload: 60 });
} catch(e) {
message.error(e.message);
} finally {
yield put({ type: 'onUpdateState', sendingCode: false });
}
},
},