53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { createRoute, OpenAPIHono } from '@hono/zod-openapi'
|
|
import { AuthService } from '@/server/modules/auth/auth.service'
|
|
import { UserService } from '@/server/modules/users/user.service'
|
|
import { z } from 'zod'
|
|
import { ErrorSchema } from '@/server/utils/errorHandler'
|
|
import { AppDataSource } from '@/server/data-source'
|
|
import { AuthContext } from '@/server/types/context'
|
|
import { SMS } from '@/server/utils/sms.aliyun'
|
|
|
|
const GenerateSMSRndCodeSchema = z.object({
|
|
phone: z.string().regex(/^1[3-9]\d{9}$/).openapi({
|
|
example: '13800138000',
|
|
description: '手机号'
|
|
})
|
|
})
|
|
|
|
const SMSRndCodeResponseSchema = z.object({
|
|
success: z.boolean().openapi({
|
|
example: true,
|
|
description: '是否成功'
|
|
})
|
|
})
|
|
|
|
const userService = new UserService(AppDataSource)
|
|
const authService = new AuthService(userService)
|
|
|
|
const generateFixedCodeRoute = createRoute({
|
|
method: 'get',
|
|
path: '/phone-code/sms/{phone}',
|
|
request: {
|
|
params: GenerateSMSRndCodeSchema
|
|
},
|
|
responses: {
|
|
200: {
|
|
description: '发送成功',
|
|
content: {
|
|
'application/json': {
|
|
schema: SMSRndCodeResponseSchema
|
|
}
|
|
}
|
|
},
|
|
}
|
|
})
|
|
|
|
const app = new OpenAPIHono<AuthContext>().openapi(generateFixedCodeRoute, async (c) => {
|
|
const { phone } = c.req.valid('param')
|
|
const code = await authService.generateRandCode(phone)
|
|
// TODO: 发送短信
|
|
const result = await SMS.sendVerificationSMS(phone, code)
|
|
return c.json({ success: result }, 200)
|
|
})
|
|
|
|
export default app |