feat: register api

This commit is contained in:
KUN1007 2023-09-21 11:17:20 +08:00
parent 7471639bbc
commit 0687cbab99
6 changed files with 106 additions and 11 deletions

View file

@ -1,13 +1,37 @@
import { fetchPost } from '@/utils/request'
import type * as Login from './types/login'
const loginURLs = {
login: `/login/login`,
register: `/login/register`,
verificationCode: `/auth/email/send`,
}
// 获取用户登录数据
export const postLoginDataApi = async (
loginData: Login.LoginRequestData
): Promise<Login.LoginResponseData> => {
const response = await fetchPost<Login.LoginResponseData>(
'/login/login',
loginURLs.login,
loginData
)
return response
}
// 发送验证码,在注册时需要
export const sendVerificationCodeMailApi = async (
email: Login.VerificationCodeMailRequestData
): Promise<void> => {
await fetchPost<void>(loginURLs.verificationCode, email)
}
// 注册用户,注册成功直接登陆,返回登陆数据
export const postRegisterDataApi = async (
registerData: Login.RegisterRequestData
): Promise<Login.LoginResponseData> => {
const response = await fetchPost<Login.LoginResponseData>(
loginURLs.register,
registerData
)
return response
}

View file

@ -6,6 +6,20 @@ export interface LoginRequestData {
password: string
}
// 注册请求数据的格式
export interface RegisterRequestData {
name: string
email: string
password: string
code: string
ip?: string
}
// 发送验证码请求数据格式
export interface VerificationCodeMailRequestData {
email: string
}
// 登录响应数据的格式
export type LoginResponseData = KUNGalgameResponseData<{
uid: number

View file

@ -30,6 +30,16 @@ export interface TopicDetail {
share: number[]
}
// 更新话题的请求数据
export interface TopicUpdateRequestData {
tid: number
uid: number
title: string
content: string
tags: string[]
category: string[]
}
// 回复的请求数据
export interface TopicReplyRequestData {
tid: number

View file

@ -2,9 +2,19 @@
*
*/
import { defineStore } from 'pinia'
import { LoginRequestData, LoginResponseData } from '@/api/index'
import { postLoginDataApi } from '@/api/index'
import {
LoginRequestData,
LoginResponseData,
RegisterRequestData,
VerificationCodeMailRequestData,
} from '@/api'
import {
postLoginDataApi,
postRegisterDataApi,
sendVerificationCodeMailApi,
} from '@/api'
// 用户信息接口
interface UserState {
uid: number
name: string
@ -26,29 +36,60 @@ export const useKUNGalgameUserStore = defineStore({
}),
getters: {},
actions: {
// 设置用户信息
setUserInfo(uid: number, name: string, avatar: string): void {
this.uid = uid
this.name = name
this.avatar = avatar
},
// 设置用户 token
setToken(token: string, refreshToken: string): void {
this.token = token
this.refreshToken = refreshToken
},
login(LoginRequestData: LoginRequestData): Promise<LoginResponseData> {
// 登陆
login(request: LoginRequestData): Promise<LoginResponseData> {
return new Promise((resolve, reject) => {
// 这里是向后端发请求的函数
postLoginDataApi({
name: LoginRequestData.name,
password: LoginRequestData.password,
})
postLoginDataApi(request)
.then((res) => {
if (res.data) {
this.setUserInfo(res.data.uid, res.data.name, res.data.avatar)
this.setToken(res.data.token, res.data.refreshToken)
} else
(e: any) => {
throw new Error('500 Server ERROR', e)
(error: Error) => {
throw new Error('500 Server ERROR', error)
}
resolve(res)
})
.catch((error) => {
reject(error)
})
})
},
// 发送验证码
sendCode(request: VerificationCodeMailRequestData): Promise<void> {
return new Promise((resolve, reject) => {
sendVerificationCodeMailApi(request)
.then((res) => {
resolve(res)
})
.catch((error) => {
reject(error)
})
})
},
// 注册
register(request: RegisterRequestData): Promise<LoginResponseData> {
return new Promise((resolve, reject) => {
postRegisterDataApi(request)
.then((res) => {
if (res.data) {
this.setUserInfo(res.data.uid, res.data.name, res.data.avatar)
this.setToken(res.data.token, res.data.refreshToken)
} else
(error: Error) => {
throw new Error('500 Server ERROR', error)
}
resolve(res)
})

View file

@ -32,6 +32,7 @@ const props = defineProps<{
rUser: TopicUserInfo
}>()
//
const handelReply = async () => {
// 便
replyDraft.value.tid = tid
@ -44,6 +45,9 @@ const handelReply = async () => {
await nextTick()
isEdit.value = true
}
//
const handleClickEdit = () => {}
</script>
<template>
@ -92,7 +96,9 @@ const handelReply = async () => {
</li>
<!-- 编辑 -->
<li>
<span class="icon"><Icon icon="line-md:pencil-twotone-alt" /></span>
<span @click="handleClickEdit" class="icon">
<Icon icon="line-md:pencil-twotone-alt" />
</span>
</li>
<!-- 回复的插槽 -->