前后端联调测试第一个ts接口

This commit is contained in:
Guwan 2024-12-07 14:24:47 +08:00
parent 3b612cf022
commit a0cd9dc031
2 changed files with 27 additions and 21 deletions

View File

@ -58,13 +58,17 @@ export interface LoginParams {
verifyCode?: string verifyCode?: string
} }
export interface RegisterParams { export interface RegisterParams {
phone: string username: string
password: string password: string
verifyCode?: string confirmPassword: string
phone: string
phoneCode: string
email: string
emailCode: string
} }
// 定义后端返回的用户信息类型 // 定义后端返回的用户信息类型
export interface UserInfo { export interface UserInfo {
id: number id: number
@ -99,7 +103,7 @@ export const userApi = {
register(data: RegisterParams) { register(data: RegisterParams) {
return request.post<ApiResponse<{ token: string }>>('/auth/login', data) return request.post<ApiResponse>('/user/register', data)
}, },
@ -108,15 +112,17 @@ export const userApi = {
return request.post<ApiResponse<{ token: string }>>('/auth/login', data) return request.post<ApiResponse<{ token: string }>>('/auth/login', data)
}, },
// 发送验证码
sendVerifyCode(phone: string) {
return request.post<ApiResponse<void>>('/user/getEmailCode', { phone })
},
// 发送验证码 // 发送验证码
sendVerifyEmailCode(email: string) { sendVerifyEmailCode(email: string) {
return request.post<ApiResponse>('/user/getEmailCode', { email }) return request.post<ApiResponse>('/user/getEmailCode', { email })
}, },
// 发送验证码
sendVerifyPhoneCode(phone: string) {
return request.post<ApiResponse>('/user/getphoneCode', { phone })
},
// 获取当前登录用户信息 // 获取当前登录用户信息
getCurrentUser() { getCurrentUser() {

View File

@ -198,15 +198,16 @@ import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import { userApi } from '@/api/user' import { userApi } from '@/api/user'
const router = useRouter() const router = useRouter()
const registerFormRef = ref<FormInstance>() const registerFormRef = ref<FormInstance>()
const loading = ref(false) const loading = ref(false)
const registerForm = reactive({ const registerForm = reactive({
username: '', username: '',
phone: '',
password: '', password: '',
confirmPassword: '', confirmPassword: '',
phone: '',
phoneCode: '', phoneCode: '',
email: '', email: '',
emailCode: '' emailCode: ''
@ -266,15 +267,16 @@ const handleRegister = async () => {
loading.value = true loading.value = true
try { try {
// //
// const response = await register(registerForm) const response = await userApi.register(registerForm)
console.log(response)
// //
await new Promise(resolve => setTimeout(resolve, 1000)) // await new Promise(resolve => setTimeout(resolve, 1000))
ElMessage.success('注册成功') ElMessage.success('注册成功')
router.push('/login') router.push('/login')
} catch (error) { } catch (error) {
ElMessage.error('注册失败,请稍后重试') //ElMessage.error('')
} finally { } finally {
loading.value = false loading.value = false
} }
@ -309,11 +311,11 @@ const startEmailCooldown = () => {
} }
// //
const sendPhoneCode = () => { const sendPhoneCode = async () => {
if (phoneCooldown.value > 0 || !registerForm.phone) { // if (phoneCooldown.value > 0 || !registerForm.phone) {
ElMessage.warning('请先输入手机号') // ElMessage.warning('')
return // return
} // }
// //
if (!/^1[3-9]\d{9}$/.test(registerForm.phone)) { if (!/^1[3-9]\d{9}$/.test(registerForm.phone)) {
@ -321,7 +323,7 @@ const sendPhoneCode = () => {
return return
} }
// await userApi.sendVerifyPhoneCode(registerForm.phone)
ElMessage.success('手机验证码已发送') ElMessage.success('手机验证码已发送')
startPhoneCooldown() startPhoneCooldown()
} }
@ -329,9 +331,7 @@ const sendPhoneCode = () => {
// //
const sendEmailCode = async () => { const sendEmailCode = async () => {
if (emailCooldown.value > 0 || !registerForm.email) return if (emailCooldown.value > 0 || !registerForm.email) return
const result = await userApi.sendVerifyEmailCode(registerForm.email) await userApi.sendVerifyEmailCode(registerForm.email)
console.log(result)
console.log(result.code)
ElMessage.success('邮箱验证码已发送') ElMessage.success('邮箱验证码已发送')
startEmailCooldown() startEmailCooldown()
} }