mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-19 18:14:38 +00:00
feat: 支持邮箱和手机号同时注册
This commit is contained in:
@@ -18,18 +18,18 @@ export const sendAuthCode = ({
|
|||||||
export const getTokenLogin = () => GET<UserType>('/user/tokenLogin');
|
export const getTokenLogin = () => GET<UserType>('/user/tokenLogin');
|
||||||
|
|
||||||
export const postRegister = ({
|
export const postRegister = ({
|
||||||
phone,
|
username,
|
||||||
password,
|
password,
|
||||||
code,
|
code,
|
||||||
inviterId
|
inviterId
|
||||||
}: {
|
}: {
|
||||||
phone: string;
|
username: string;
|
||||||
code: string;
|
code: string;
|
||||||
password: string;
|
password: string;
|
||||||
inviterId: string;
|
inviterId: string;
|
||||||
}) =>
|
}) =>
|
||||||
POST<ResLogin>('/user/register', {
|
POST<ResLogin>('/user/register', {
|
||||||
phone,
|
username,
|
||||||
code,
|
code,
|
||||||
inviterId,
|
inviterId,
|
||||||
password: createHashPassword(password)
|
password: createHashPassword(password)
|
||||||
|
@@ -79,15 +79,24 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: 'system',
|
role: 'system',
|
||||||
content: `服务端逻辑生成器.根据用户输入的需求,拆解成代码实现的步骤,并按格式返回: 1.\n2.\n3.\n ......
|
content: `服务端逻辑生成器.根据用户输入的需求,拆解成 laf 云函数实现的步骤,按格式返回: 1.\n2.\n3.\n ......
|
||||||
下面是一些例子:
|
下面是一些例子:
|
||||||
|
一个 hello world 例子
|
||||||
|
1. 返回字符串: "hello world"
|
||||||
|
|
||||||
|
计算圆的面积
|
||||||
|
1. 从 body 中获取半径 radius.
|
||||||
|
2. 校验 radius 是否为有效的数字.
|
||||||
|
3. 计算圆的面积.
|
||||||
|
4. 返回圆的面积: {area}
|
||||||
|
|
||||||
实现一个手机号发生注册验证码方法.
|
实现一个手机号发生注册验证码方法.
|
||||||
1. 从 query 中获取 phone.
|
1. 从 query 中获取 phone.
|
||||||
2. 校验手机号格式是否正确,不正确则返回错误原因:手机号格式错误.
|
2. 校验手机号格式是否正确,不正确则返回错误原因:手机号格式错误.
|
||||||
3. 给 phone 发送一个短信验证码,验证码长度为6位字符串,内容为:你正在注册laf,验证码为:code.
|
3. 给 phone 发送一个短信验证码,验证码长度为6位字符串,内容为:你正在注册laf,验证码为:code.
|
||||||
4. 数据库添加数据,表为"codes",内容为 {phone, code}.
|
4. 数据库添加数据,表为"codes",内容为 {phone, code}.
|
||||||
|
|
||||||
实现根据手机号注册账号,需要验证手机验证码.
|
实现一个云函数,使用手机号注册账号,需要验证手机验证码.
|
||||||
1. 从 body 中获取 phone 和 code.
|
1. 从 body 中获取 phone 和 code.
|
||||||
2. 校验手机号格式是否正确,不正确则返回错误原因:手机号格式错误.
|
2. 校验手机号格式是否正确,不正确则返回错误原因:手机号格式错误.
|
||||||
2. 获取数据库数据,表为"codes",查找是否有符合 phone, code 等于body参数的记录,没有的话返回错误原因:验证码不正确.
|
2. 获取数据库数据,表为"codes",查找是否有符合 phone, code 等于body参数的记录,没有的话返回错误原因:验证码不正确.
|
||||||
|
@@ -9,23 +9,17 @@ import { UserAuthTypeEnum } from '@/constants/common';
|
|||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
const { phone, code, password, inviterId } = req.body;
|
const { username, code, password, inviterId } = req.body;
|
||||||
|
|
||||||
if (!phone || !code || !password) {
|
if (!username || !code || !password) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
}
|
}
|
||||||
|
|
||||||
const reg = /^1[3456789]\d{9}$/;
|
|
||||||
|
|
||||||
if (!reg.test(phone)) {
|
|
||||||
throw new Error('手机号格式错误');
|
|
||||||
}
|
|
||||||
|
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
// 验证码校验. 注册只接收手机号
|
// 验证码校验
|
||||||
const authCode = await AuthCode.findOne({
|
const authCode = await AuthCode.findOne({
|
||||||
username: phone,
|
username,
|
||||||
code,
|
code,
|
||||||
type: UserAuthTypeEnum.register,
|
type: UserAuthTypeEnum.register,
|
||||||
expiredTime: { $gte: Date.now() }
|
expiredTime: { $gte: Date.now() }
|
||||||
@@ -37,15 +31,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
|
|
||||||
// 重名校验
|
// 重名校验
|
||||||
const authRepeat = await User.findOne({
|
const authRepeat = await User.findOne({
|
||||||
username: phone
|
username
|
||||||
});
|
});
|
||||||
|
|
||||||
if (authRepeat) {
|
if (authRepeat) {
|
||||||
throw new Error('手机号已被注册');
|
throw new Error('该用户已被注册');
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await User.create({
|
const response = await User.create({
|
||||||
username: phone,
|
username,
|
||||||
password,
|
password,
|
||||||
inviterId: inviterId ? inviterId : undefined
|
inviterId: inviterId ? inviterId : undefined
|
||||||
});
|
});
|
||||||
@@ -59,7 +53,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
|
|
||||||
// 删除验证码记录
|
// 删除验证码记录
|
||||||
await AuthCode.deleteMany({
|
await AuthCode.deleteMany({
|
||||||
username: phone
|
username
|
||||||
});
|
});
|
||||||
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
|
@@ -15,7 +15,7 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface RegisterType {
|
interface RegisterType {
|
||||||
phone: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
password2: string;
|
password2: string;
|
||||||
code: string;
|
code: string;
|
||||||
@@ -38,10 +38,10 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
|
|||||||
const { codeSending, sendCodeText, sendCode, codeCountDown } = useSendCode();
|
const { codeSending, sendCodeText, sendCode, codeCountDown } = useSendCode();
|
||||||
|
|
||||||
const onclickSendCode = useCallback(async () => {
|
const onclickSendCode = useCallback(async () => {
|
||||||
const check = await trigger('phone');
|
const check = await trigger('username');
|
||||||
if (!check) return;
|
if (!check) return;
|
||||||
sendCode({
|
sendCode({
|
||||||
username: getValues('phone'),
|
username: getValues('username'),
|
||||||
type: 'register'
|
type: 'register'
|
||||||
});
|
});
|
||||||
}, [getValues, sendCode, trigger]);
|
}, [getValues, sendCode, trigger]);
|
||||||
@@ -49,12 +49,12 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
|
|||||||
const [requesting, setRequesting] = useState(false);
|
const [requesting, setRequesting] = useState(false);
|
||||||
|
|
||||||
const onclickRegister = useCallback(
|
const onclickRegister = useCallback(
|
||||||
async ({ phone, password, code }: RegisterType) => {
|
async ({ username, password, code }: RegisterType) => {
|
||||||
setRequesting(true);
|
setRequesting(true);
|
||||||
try {
|
try {
|
||||||
loginSuccess(
|
loginSuccess(
|
||||||
await postRegister({
|
await postRegister({
|
||||||
phone,
|
username,
|
||||||
code,
|
code,
|
||||||
password,
|
password,
|
||||||
inviterId
|
inviterId
|
||||||
@@ -81,23 +81,24 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
|
|||||||
注册 FastGPT 账号
|
注册 FastGPT 账号
|
||||||
</Box>
|
</Box>
|
||||||
<form onSubmit={handleSubmit(onclickRegister)}>
|
<form onSubmit={handleSubmit(onclickRegister)}>
|
||||||
<FormControl mt={8} isInvalid={!!errors.phone}>
|
<FormControl mt={8} isInvalid={!!errors.username}>
|
||||||
<Input
|
<Input
|
||||||
placeholder="手机号"
|
placeholder="邮箱/手机号"
|
||||||
size={mediaLgMd}
|
size={mediaLgMd}
|
||||||
{...register('phone', {
|
{...register('username', {
|
||||||
required: '手机号不能为空',
|
required: '邮箱/手机号不能为空',
|
||||||
pattern: {
|
pattern: {
|
||||||
value: /^1[3456789]\d{9}$/,
|
value:
|
||||||
message: '手机号格式错误'
|
/(^1[3456789]\d{9}$)|(^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$)/,
|
||||||
|
message: '邮箱/手机号格式错误'
|
||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
></Input>
|
></Input>
|
||||||
<FormErrorMessage position={'absolute'} fontSize="xs">
|
<FormErrorMessage position={'absolute'} fontSize="xs">
|
||||||
{!!errors.phone && errors.phone.message}
|
{!!errors.username && errors.username.message}
|
||||||
</FormErrorMessage>
|
</FormErrorMessage>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormControl mt={8} isInvalid={!!errors.phone}>
|
<FormControl mt={8} isInvalid={!!errors.username}>
|
||||||
<Flex>
|
<Flex>
|
||||||
<Input
|
<Input
|
||||||
flex={1}
|
flex={1}
|
||||||
|
@@ -8,7 +8,8 @@ export const openaiError: Record<string, string> = {
|
|||||||
};
|
};
|
||||||
export const openaiError2: Record<string, string> = {
|
export const openaiError2: Record<string, string> = {
|
||||||
insufficient_quota: 'API 余额不足',
|
insufficient_quota: 'API 余额不足',
|
||||||
invalid_request_error: 'openai 接口异常'
|
billing_not_active: 'openai 账号异常',
|
||||||
|
invalid_request_error: '无效的 openai 请求'
|
||||||
};
|
};
|
||||||
export const proxyError: Record<string, boolean> = {
|
export const proxyError: Record<string, boolean> = {
|
||||||
ECONNABORTED: true,
|
ECONNABORTED: true,
|
||||||
|
@@ -9,6 +9,7 @@ import { generateVector } from './generateVector';
|
|||||||
import { connectRedis } from '../redis';
|
import { connectRedis } from '../redis';
|
||||||
import { VecModelDataPrefix } from '@/constants/redis';
|
import { VecModelDataPrefix } from '@/constants/redis';
|
||||||
import { customAlphabet } from 'nanoid';
|
import { customAlphabet } from 'nanoid';
|
||||||
|
import { openaiError2 } from '../errorCode';
|
||||||
import { ModelSplitDataSchema } from '@/types/mongoSchema';
|
import { ModelSplitDataSchema } from '@/types/mongoSchema';
|
||||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 12);
|
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 12);
|
||||||
|
|
||||||
@@ -102,6 +103,7 @@ export async function generateQA(next = false): Promise<any> {
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
const rawContent = res?.data.choices[0].message?.content || ''; // chatgpt 原本的回复
|
const rawContent = res?.data.choices[0].message?.content || ''; // chatgpt 原本的回复
|
||||||
const result = splitText(res?.data.choices[0].message?.content || ''); // 格式化后的QA对
|
const result = splitText(res?.data.choices[0].message?.content || ''); // 格式化后的QA对
|
||||||
|
console.log(`split result length: `, result.length);
|
||||||
// 计费
|
// 计费
|
||||||
pushSplitDataBill({
|
pushSplitDataBill({
|
||||||
isPay: !userApiKey && result.length > 0,
|
isPay: !userApiKey && result.length > 0,
|
||||||
@@ -115,6 +117,10 @@ export async function generateQA(next = false): Promise<any> {
|
|||||||
result
|
result
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log('QA 拆分错误');
|
||||||
|
return Promise.reject(err);
|
||||||
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -152,12 +158,7 @@ export async function generateQA(next = false): Promise<any> {
|
|||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
|
||||||
console.log(
|
console.log('生成QA成功,time:', `${(Date.now() - startTime) / 1000}s`);
|
||||||
'生成QA成功,time:',
|
|
||||||
`${(Date.now() - startTime) / 1000}s`,
|
|
||||||
'QA数量:',
|
|
||||||
resultList.length
|
|
||||||
);
|
|
||||||
|
|
||||||
generateQA(true);
|
generateQA(true);
|
||||||
generateVector();
|
generateVector();
|
||||||
@@ -171,12 +172,8 @@ export async function generateQA(next = false): Promise<any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 没有余额或者凭证错误时,拒绝任务
|
// 没有余额或者凭证错误时,拒绝任务
|
||||||
if (
|
if (dataId && openaiError2[error?.response?.data?.error?.type]) {
|
||||||
dataId &&
|
console.log(openaiError2[error?.response?.data?.error?.type], '删除QA任务');
|
||||||
(+error.response?.status === 401 ||
|
|
||||||
error?.response?.data?.error?.type === 'insufficient_quota')
|
|
||||||
) {
|
|
||||||
console.log('api 异常,删除QA任务');
|
|
||||||
|
|
||||||
await SplitData.findByIdAndUpdate(dataId, {
|
await SplitData.findByIdAndUpdate(dataId, {
|
||||||
textList: [],
|
textList: [],
|
||||||
|
@@ -3,6 +3,7 @@ import { VecModelDataIdx } from '@/constants/redis';
|
|||||||
import { vectorToBuffer } from '@/utils/tools';
|
import { vectorToBuffer } from '@/utils/tools';
|
||||||
import { ModelDataStatusEnum } from '@/constants/redis';
|
import { ModelDataStatusEnum } from '@/constants/redis';
|
||||||
import { openaiCreateEmbedding, getOpenApiKey } from '../utils/openai';
|
import { openaiCreateEmbedding, getOpenApiKey } from '../utils/openai';
|
||||||
|
import { openaiError2 } from '../errorCode';
|
||||||
|
|
||||||
export async function generateVector(next = false): Promise<any> {
|
export async function generateVector(next = false): Promise<any> {
|
||||||
if (process.env.queueTask !== '1') {
|
if (process.env.queueTask !== '1') {
|
||||||
@@ -91,11 +92,7 @@ export async function generateVector(next = false): Promise<any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 没有余额或者凭证错误时,拒绝任务
|
// 没有余额或者凭证错误时,拒绝任务
|
||||||
if (
|
if (dataId && openaiError2[error?.response?.data?.error?.type]) {
|
||||||
dataId &&
|
|
||||||
(+error.response?.status === 401 ||
|
|
||||||
error?.response?.data?.error?.type === 'insufficient_quota')
|
|
||||||
) {
|
|
||||||
console.log('删除向量生成任务记录');
|
console.log('删除向量生成任务记录');
|
||||||
const redis = await connectRedis();
|
const redis = await connectRedis();
|
||||||
redis.del(dataId);
|
redis.del(dataId);
|
||||||
|
Reference in New Issue
Block a user