Skip to content

Commit

Permalink
pref(user): 接入注册优化登录 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
am6737 authored Jun 3, 2024
1 parent 1115d30 commit 3132387
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export function loginApi(data: LoginParams): Promise<ResponseData> {
/**
* 注册接口
* @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<ResponseData> {
Expand Down
45 changes: 39 additions & 6 deletions src/pages/account/login.tsx
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand All @@ -39,6 +64,7 @@ const Login: React.FC = () => {

return (
<>
{contextHolder}
<Flex
className="w-screen h-screen"
vertical
Expand All @@ -53,6 +79,7 @@ const Login: React.FC = () => {
}
/>
<Form
form={form}
className={clsx('w-2/3 mobile:min-w-[350px] mobile:w-1/6')}
layout="vertical"
initialValues={{ email: '[email protected]', password: '123456qq' }}
Expand Down Expand Up @@ -114,7 +141,13 @@ const Login: React.FC = () => {
</Flex>
</Form.Item>
<Form.Item>
<Button size="large" type="primary" htmlType="submit" className="w-full">
<Button
size="large"
type="primary"
htmlType="submit"
className="w-full"
loading={loading}
>
{$t('登陆')}
</Button>
</Form.Item>
Expand Down
54 changes: 46 additions & 8 deletions src/pages/account/register.tsx
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -25,6 +56,7 @@ const Register: React.FC = () => {

return (
<Flex className="w-screen h-screen" vertical justify="center" align="center" gap="large">
{contextHolder}
<Avatar
size={120}
src={
Expand Down Expand Up @@ -101,7 +133,13 @@ const Register: React.FC = () => {
<Input prefix={<LockOutlined />} type="password" placeholder={$t('确认密码')} />
</Form.Item>
<Form.Item>
<Button size="large" type="primary" htmlType="submit" className="w-full">
<Button
size="large"
type="primary"
htmlType="submit"
className="w-full"
loading={loading}
>
{$t('注册')}
</Button>
</Form.Item>
Expand Down
4 changes: 2 additions & 2 deletions src/stores/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
Expand Down

0 comments on commit 3132387

Please sign in to comment.