feat: register api
This commit is contained in:
parent
7471639bbc
commit
0687cbab99
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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>
|
||||
|
||||
<!-- 回复的插槽 -->
|
||||
|
|
Loading…
Reference in a new issue