| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- import { useIntl } from "react-intl";
- import {
- ProForm,
- ProFormDependency,
- type ProFormInstance,
- ProFormText,
- } from "@ant-design/pro-components";
- import { Button, message, Modal, Result } from "antd";
- import { useNavigate } from "react-router";
- import { EyeInvisibleOutlined, EyeTwoTone } from "@ant-design/icons";
- import { get, post } from "../../../request";
- import LangSelect from "../../general/LangSelect";
- import { useRef, useState } from "react";
- import type {
- IInviteResponse,
- ISignInResponse,
- ISignUpRequest,
- } from "../../../api/Auth";
- export interface IAccountForm {
- email: string;
- username: string;
- nickname: string;
- password: string;
- password2: string;
- lang: string;
- }
- interface IAccountInfo {
- email?: boolean;
- }
- export const AccountInfo = ({ email = true }: IAccountInfo) => {
- const intl = useIntl();
- const [nickname, setNickname] = useState<string>();
- return (
- <>
- {email ? (
- <ProForm.Group>
- <ProFormText
- width="md"
- name="email"
- required
- label={intl.formatMessage({
- id: "forms.fields.email.label",
- })}
- rules={[{ required: true, max: 255, min: 4 }]}
- disabled
- />
- </ProForm.Group>
- ) : (
- <></>
- )}
- <ProForm.Group>
- <ProFormText
- width="md"
- name="username"
- required
- fieldProps={{
- onChange: (event) => {
- setNickname(event.target.value);
- },
- }}
- label={intl.formatMessage({
- id: "forms.fields.username.label",
- })}
- rules={[
- { required: true, max: 32, min: 6 },
- {
- pattern: new RegExp("^[0-9a-zA-Z_]{1,}", "g"),
- message: "只允许数字,字母,下划线",
- },
- ]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormText.Password
- width="md"
- name="password"
- fieldProps={{
- type: "password",
- iconRender: (visible) =>
- visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />,
- }}
- required
- label={intl.formatMessage({
- id: "forms.fields.password.label",
- })}
- rules={[{ required: true, max: 32, min: 6 }]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormText.Password
- width="md"
- name="password2"
- fieldProps={{
- type: "password",
- iconRender: (visible) =>
- visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />,
- }}
- required
- label={intl.formatMessage({
- id: "forms.fields.confirm-password.label",
- })}
- rules={[{ required: true, max: 32, min: 6 }]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormDependency name={["username"]}>
- {({ username }) => {
- return (
- <ProFormText
- width="md"
- fieldProps={{
- placeholder: username,
- value: nickname ? nickname : username,
- onChange: (event) => {
- setNickname(event.target.value);
- },
- }}
- name="nickname"
- required
- label={intl.formatMessage({
- id: "forms.fields.nickname.label",
- })}
- rules={[{ required: false, max: 32, min: 4 }]}
- />
- );
- }}
- </ProFormDependency>
- </ProForm.Group>
- <ProForm.Group>
- <LangSelect label="常用的译文语言" />
- </ProForm.Group>
- </>
- );
- };
- export const SignUpSuccess = () => {
- const intl = useIntl();
- const navigate = useNavigate();
- return (
- <Result
- status="success"
- title="注册成功"
- subTitle={
- <Button
- type="primary"
- onClick={() => navigate("/anonymous/users/sign-in")}
- >
- {intl.formatMessage({
- id: "nut.users.sign-in.title",
- })}
- </Button>
- }
- />
- );
- };
- export const onSignIn = async (token: string, values: IAccountForm) => {
- if (values.password !== values.password2) {
- Modal.error({ title: "两次密码不同" });
- return false;
- }
- const url = "/v2/sign-up";
- const data = {
- token: token,
- username: values.username,
- nickname:
- values.nickname && values.nickname.trim() !== ""
- ? values.nickname
- : values.username,
- email: values.email,
- password: values.password,
- lang: values.lang,
- };
- console.info("api request", url, data);
- const signUp = await post<ISignUpRequest, ISignInResponse>(
- "/v2/sign-up",
- data
- );
- console.info("api response", signUp);
- return signUp;
- };
- interface IWidget {
- token?: string;
- }
- const SignUpWidget = ({ token }: IWidget) => {
- const [success, setSuccess] = useState(false);
- const formRef = useRef<ProFormInstance | undefined>(undefined);
- return success ? (
- <SignUpSuccess />
- ) : (
- <ProForm<IAccountForm>
- formRef={formRef}
- onFinish={async (values: IAccountForm) => {
- if (typeof token === "undefined") {
- return;
- }
- const signUp = await onSignIn(token, values);
- if (signUp) {
- if (signUp.ok) {
- setSuccess(true);
- } else {
- message.error(signUp.message);
- }
- }
- }}
- request={async () => {
- const url = `/v2/invite/${token}`;
- console.info("api request", url);
- const res = await get<IInviteResponse>(url);
- console.debug("api response", res.data);
- return {
- id: res.data.id,
- username: "",
- nickname: "",
- password: "",
- password2: "",
- email: res.data.email,
- lang: "zh-Hans",
- };
- }}
- >
- <AccountInfo />
- </ProForm>
- );
- };
- export default SignUpWidget;
|