From 3132387d3aaa54a9e5986470f7445616af07ca6d Mon Sep 17 00:00:00 2001 From: am6737 <91421697+am6737@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:36:20 +0800 Subject: [PATCH] =?UTF-8?q?pref(user):=20=E6=8E=A5=E5=85=A5=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95=20(#28)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/user.ts | 5 ++++ src/pages/account/login.tsx | 45 ++++++++++++++++++++++++---- src/pages/account/register.tsx | 54 +++++++++++++++++++++++++++++----- src/stores/user.ts | 4 +-- 4 files changed, 92 insertions(+), 16 deletions(-) diff --git a/src/api/user.ts b/src/api/user.ts index d38d39b2..c7081861 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -29,6 +29,11 @@ export function loginApi(data: LoginParams): Promise { /** * 注册接口 * @param data + * @param data.nickname - 用户名 + * @param data.email - 邮箱 + * @param data.password - 密码 + * @param data.confirm_password - 确认密码 + * @param data.public_key - 公钥 * @returns */ export function registerApi(data: RegisterParams): Promise { diff --git a/src/pages/account/login.tsx b/src/pages/account/login.tsx index ceee41f1..528411e1 100644 --- a/src/pages/account/login.tsx +++ b/src/pages/account/login.tsx @@ -1,31 +1,56 @@ -import React from 'react' +import React, { useEffect, useState } from 'react' import { LockOutlined, MailOutlined } from '@ant-design/icons' -import { Button, Form, Input, Avatar, Flex, Checkbox } from 'antd' +import { Button, Form, Input, Avatar, Flex, Checkbox, message } from 'antd' import { $t } from '@/i18n' import clsx from 'clsx' -import { NavigateOptions, useNavigate } from 'react-router' +import { NavigateOptions, useLocation, useNavigate } from 'react-router' import useUserStore from '@/stores/user' import { createFingerprint } from '@/utils/fingerprint' const Login: React.FC = () => { const userStore = useUserStore() const navigate = useNavigate() + const location = useLocation() + const [form] = Form.useForm() + + const [messageApi, contextHolder] = message.useMessage() + const [loading, setLoading] = useState(false) + + useEffect(() => { + if (location.state) { + const { email, password } = location.state as { email: string; password: string } + form.setFieldsValue({ email, password }) + } + }, [location.state, form]) const onFinish = async (values: any) => { console.log('Received values of form: ', values) + setLoading(true) try { - await userStore.login({ + const res = await userStore.login({ driver_id: createFingerprint(), driver_token: 'ff1005', email: values.email, password: values.password, platform: 'android' }) + if (res.code !== 200) { + messageApi.open({ + type: 'error', + content: res.msg + }) + return + } navigate('/dashboard', { replace: true }) } catch (error: any) { - console.log('login error: ', error.message) + messageApi.open({ + type: 'error', + content: error + }) + } finally { + setLoading(false) } } @@ -39,6 +64,7 @@ const Login: React.FC = () => { return ( <> + {contextHolder} { } />
{ - diff --git a/src/pages/account/register.tsx b/src/pages/account/register.tsx index d2910df9..e3c0dce5 100644 --- a/src/pages/account/register.tsx +++ b/src/pages/account/register.tsx @@ -1,18 +1,49 @@ -import React from 'react' +import React, { useState } from 'react' import { LockOutlined, MailOutlined, UserOutlined } from '@ant-design/icons' -import { Button, Form, Input, Avatar, Flex } from 'antd' +import { Button, Form, Input, Avatar, Flex, message } from 'antd' import { $t } from '@/i18n' import clsx from 'clsx' import { NavigateOptions, useNavigate } from 'react-router' +import { registerApi } from '@/api/user' +import useUserStore from '@/stores/user' const Register: React.FC = () => { const navigate = useNavigate() + const userStore = useUserStore() + const [messageApi, contextHolder] = message.useMessage() + const [loading, setLoading] = useState(false) - const onFinish = (values: any) => { - console.log('Received values of form: ', values) - toLogin({ - replace: true - }) + const onFinish = async (values: any) => { + setLoading(true) + try { + const { code, data, msg } = await registerApi({ + nickname: values.nickname, + email: values.email, + password: values.password, + confirm_password: values.confirm_password + // public_key: values.public_key + }) + if (code !== 200) { + messageApi.open({ + type: 'error', + content: msg + }) + return + } + userStore.update({ userId: data?.user_id }) + // await cretaeIdentity(data.user_id, fromData.email) + toLogin({ + replace: true, + state: { email: values.email, password: values.password } + }) + } catch { + messageApi.open({ + type: 'error', + content: '注册失败,请稍后重试' + }) + } finally { + setLoading(false) + } } const toQRCode = (options?: NavigateOptions | undefined) => { @@ -25,6 +56,7 @@ const Register: React.FC = () => { return ( + {contextHolder} { } type="password" placeholder={$t('确认密码')} /> - diff --git a/src/stores/user.ts b/src/stores/user.ts index 1adb9eb2..959179f9 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -14,7 +14,7 @@ const actions = (set: any): UserStoreMethods => ({ update: async (options) => set(options), login: async (params: LoginParams) => { try { - const { code, data } = await loginApi(params) + const { code, data, msg } = await loginApi(params) console.log(code, data) if (code === 200) { set({ @@ -24,7 +24,7 @@ const actions = (set: any): UserStoreMethods => ({ }) return Promise.resolve(data) } - return Promise.reject(data) + return Promise.reject(msg) } catch (error) { console.log('错误', error) return Promise.reject(error)