前后端联调测试第一个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
}
export interface RegisterParams {
phone: string
username: string
password: string
verifyCode?: string
confirmPassword: string
phone: string
phoneCode: string
email: string
emailCode: string
}
// 定义后端返回的用户信息类型
export interface UserInfo {
id: number
@ -99,7 +103,7 @@ export const userApi = {
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)
},
// 发送验证码
sendVerifyCode(phone: string) {
return request.post<ApiResponse<void>>('/user/getEmailCode', { phone })
},
// 发送验证码
sendVerifyEmailCode(email: string) {
return request.post<ApiResponse>('/user/getEmailCode', { email })
},
// 发送验证码
sendVerifyPhoneCode(phone: string) {
return request.post<ApiResponse>('/user/getphoneCode', { phone })
},
// 获取当前登录用户信息
getCurrentUser() {

View File

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