Skip to content

Commit

Permalink
🚚 修改获取当前登录的用户信息接口,使其 URI / 响应值类型 更符合接口用途,并增加新的获取用户信息的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
cfdxkk committed Dec 6, 2023
1 parent 8181413 commit 87b0a49
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 20 deletions.
25 changes: 20 additions & 5 deletions src/controller/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { checkUserTokenService, getUserAvatarUploadSignedUrlService, getUserInfoByUidService, updateOrCreateUserInfoService, updateUserEmailService, userExistsCheckService, userLoginService, userRegistrationService } from '../service/UserService.js'
import { checkUserTokenService, getSelfUserInfoService, getUserAvatarUploadSignedUrlService, getUserInfoByUidService, updateOrCreateUserInfoService, updateUserEmailService, userExistsCheckService, userLoginService, userRegistrationService } from '../service/UserService.js'
import { koaCtx, koaNext } from '../type/koaTypes.js'
import { UpdateOrCreateUserInfoRequestDto, UpdateUserEmailRequestDto, UserExistsCheckRequestDto, UserLoginRequestDto, UserLogoutResponseDto, UserRegistrationRequestDto } from './UserControllerDto.js'
import { GetUserInfoByUidRequestDto, UpdateOrCreateUserInfoRequestDto, UpdateUserEmailRequestDto, UserExistsCheckRequestDto, UserLoginRequestDto, UserLogoutResponseDto, UserRegistrationRequestDto } from './UserControllerDto.js'

/**
* 用户注册
Expand Down Expand Up @@ -114,16 +114,31 @@ export const updateOrCreateUserInfoController = async (ctx: koaCtx, next: koaNex
await next()
}

/**
* 获取当前登录的用户信息
* @param ctx context
* @param next context
* @return GetSelfUserInfoResponseDto 当前登录的用户信息,如果获取成功则 success: true,不成功则 success: false
*/
export const getSelfUserInfoController = async (ctx: koaCtx, next: koaNext) => {
const uid = parseInt(ctx.cookies.get('uid'), 10)
const token = ctx.cookies.get('token')
ctx.body = await getSelfUserInfoService(uid, token)
await next()
}

/**
* 获取用户信息
* @param ctx context
* @param next context
* @return GetUserInfoByUidResponseDto 通过 uid 获取到的用户信息,如果获取成功则 success: true,不成功则 success: false
*/
export const getUserInfoByUidController = async (ctx: koaCtx, next: koaNext) => {
const uid = parseInt(ctx.cookies.get('uid'), 10)
const token = ctx.cookies.get('token')
ctx.body = await getUserInfoByUidService(uid, token)
const uid = ctx.query.uid as string
const getUserInfoByUidRequest: GetUserInfoByUidRequestDto = {
uid: uid ? parseInt(uid, 10) : -1, // WARN -1 代表这个 UID 是永远无法查找到结果
}
ctx.body = await getUserInfoByUidService(getUserInfoByUidRequest)
await next()
}

Expand Down
37 changes: 34 additions & 3 deletions src/controller/UserControllerDto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,40 @@ export type UpdateOrCreateUserInfoResponseDto = {
}

/**
* 通过 UID 获取用户信息的请求结果
* 获取当前登录的用户信息的请求响应
*/
export type GetSelfUserInfoResponseDto = {
/** 执行结果,程序执行成功,返回 true,程序执行失败,返回 false */
success: boolean;
/** 附加的文本消息 */
message?: string;
/** 请求结果 */
result?: {
/** 用户名 */
username?: string;
/** 用户头像的链接 */
avatar?: string;
/** 用户背景图片的链接 */
userBannerImage?: string;
/** 用户的个性签名 */
signature?: string;
/** 用户的性别,男、女和自定义(字符串) */
gender?: string;
/** 用户的个人标签 */
label?: UserLabelSchema[];
};
}

/**
* 通过 UID 获取用户信息的请求载荷
*/
export type GetUserInfoByUidRequestDto = {
/** 目标用户的 UID */
uid: number;
}

/**
* 通过 UID 获取用户信息的请求响应
*/
export type GetUserInfoByUidResponseDto = {
/** 执行结果,程序执行成功,返回 true,程序执行失败,返回 false */
Expand Down Expand Up @@ -198,7 +231,6 @@ export type CheckUserTokenResponseDto = {
userTokenOk?: boolean;
}


/**
* 用户登出的响应
*/
Expand All @@ -209,7 +241,6 @@ export type UserLogoutResponseDto = {
message?: string;
}


/**
* 获取用于用户上传头像的预签名 URL, 上传限时 60 秒
*/
Expand Down
8 changes: 6 additions & 2 deletions src/route/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Router from 'koa-router'
import { emitDanmakuController, getDanmakuListByKvidController } from '../controller/DanmakuController.js'
import { helloWorld } from '../controller/HelloWorld.js'
import { checkUserTokenController, getUserAvatarUploadSignedUrlController, getUserInfoByUidController, updateOrCreateUserInfoController, updateUserEmailController, userExistsCheckController, userLoginController, userLogoutController, userRegistrationController } from '../controller/UserController.js'
import { checkUserTokenController, getSelfUserInfoController, getUserAvatarUploadSignedUrlController, getUserInfoByUidController, updateOrCreateUserInfoController, updateUserEmailController, userExistsCheckController, userLoginController, userLogoutController, userRegistrationController } from '../controller/UserController.js'
import { getThumbVideoController, getVideoByKvidController, updateVideoController } from '../controller/VideoController.js'

const router = new Router()
Expand Down Expand Up @@ -62,10 +62,14 @@ router.post('/user/update/info', updateOrCreateUserInfoController) // 更新或
// ]
// }

router.get('/user/info', getUserInfoByUidController) // 根据 uid 获取用户信息

router.get('/user/self', getSelfUserInfoController) // 获取当前登录的用户信息
// https://localhost:9999/user/info
// cookie: uid, token

router.get('/user/info', getUserInfoByUidController) // 根据 uid 获取用户信息
// https://localhost:9999/user/info?uid=10

router.get('/user/check', checkUserTokenController) // 根据 uid, token 校验用户
// https://localhost:9999/user/check
// cookie: uid, token
Expand Down
71 changes: 61 additions & 10 deletions src/service/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InferSchemaType, Schema } from 'mongoose'
import { generateSaltedHash } from '../common/HashTool.js'
import { generateSecureRandomString } from '../common/RandomTool.js'
import { BeforeHashPasswordDataType, CheckUserTokenResponseDto, GetUserAvatarUploadSignedUrlResultDto, GetUserInfoByUidResponseDto, UpdateOrCreateUserInfoRequestDto, UpdateOrCreateUserInfoResponseDto, UpdateUserEmailRequestDto, UpdateUserEmailResponseDto, UserExistsCheckRequestDto, UserExistsCheckResponseDto, UserLoginRequestDto, UserLoginResponseDto, UserRegistrationRequestDto, UserRegistrationResponseDto } from '../controller/UserControllerDto.js'
import { BeforeHashPasswordDataType, CheckUserTokenResponseDto, GetSelfUserInfoResponseDto, GetUserAvatarUploadSignedUrlResultDto, GetUserInfoByUidRequestDto, GetUserInfoByUidResponseDto, UpdateOrCreateUserInfoRequestDto, UpdateOrCreateUserInfoResponseDto, UpdateUserEmailRequestDto, UpdateUserEmailResponseDto, UserExistsCheckRequestDto, UserExistsCheckResponseDto, UserLoginRequestDto, UserLoginResponseDto, UserRegistrationRequestDto, UserRegistrationResponseDto } from '../controller/UserControllerDto.js'
import { findOneAndUpdateData4MongoDB, insertData2MongoDB, selectDataFromMongoDB, updateData4MongoDB } from '../dbPool/DbClusterPool.js'
import { DbPoolResultsType, QueryType, SelectType } from '../dbPool/DbClusterPoolTypes.js'
import { UserAuthSchema, UserInfoSchema } from '../dbPool/schema/UserSchema.js'
Expand Down Expand Up @@ -351,12 +351,12 @@ export const updateOrCreateUserInfoService = async (updateOrCreateUserInfoReques
}

/**
* 通过 uid 获取用户信息
* 获取当前登录的用户信息
* @param uid 用户 ID
* @param token 用户 token
* @returns 获取用户信息的请求结果
* @returns 获取到的当前登录的用户信息
*/
export const getUserInfoByUidService = async (uid: number, token: string): Promise<GetUserInfoByUidResponseDto> => {
export const getSelfUserInfoService = async (uid: number, token: string): Promise<GetSelfUserInfoResponseDto> => {
try {
if (uid !== null && uid !== undefined && token) {
if (await checkUserToken(uid, token)) {
Expand All @@ -367,12 +367,12 @@ export const getUserInfoByUidService = async (uid: number, token: string): Promi
uid,
}
const getUserInfoSelect: SelectType<UserInfo> = {
label: 1,
username: 1,
avatar: 1,
userBannerImage: 1,
signature: 1,
gender: 1,
label: 1, // 用户标签
username: 1, // 用户名
avatar: 1, // 用户头像
userBannerImage: 1, // 用户的背景图
signature: 1, // 用户的个性签名
gender: 1, // 用户的性别
}
try {
const userInfoResult = await selectDataFromMongoDB(getUserInfoWhere, getUserInfoSelect, schema, collectionName)
Expand Down Expand Up @@ -406,6 +406,57 @@ export const getUserInfoByUidService = async (uid: number, token: string): Promi
}
}

/**
* 通过 uid 获取用户信息
* @param uid 用户 ID
* @returns 获取用户信息的请求结果
*/
export const getUserInfoByUidService = async (getUserInfoByUidRequest: GetUserInfoByUidRequestDto): Promise<GetUserInfoByUidResponseDto> => {
try {
const uid = getUserInfoByUidRequest?.uid
if (uid !== null && uid !== undefined) {
const { collectionName, schema: userInfoSchema } = UserInfoSchema
const schema = new Schema(userInfoSchema)
type UserInfo = InferSchemaType<typeof schema>
const getUserInfoWhere: QueryType<UserInfo> = {
uid,
}
const getUserInfoSelect: SelectType<UserInfo> = {
label: 1, // 用户标签
username: 1, // 用户名
avatar: 1, // 用户头像
userBannerImage: 1, // 用户的背景图
signature: 1, // 用户的个性签名
gender: 1, // 用户的性别
}
try {
const userInfoResult = await selectDataFromMongoDB(getUserInfoWhere, getUserInfoSelect, schema, collectionName)
if (userInfoResult && userInfoResult.success) {
const result = userInfoResult?.result
if (result?.length === 1 && result?.[0]) {
return { success: true, message: '获取用户信息成功', result: result[0] }
} else {
console.error('ERROR', '获取用户信息时失败,获取到的结果长度不为 1')
return { success: false, message: '获取用户信息时失败,结果异常' }
}
} else {
console.error('ERROR', '获取用户信息时失败,获取到的结果为空')
return { success: false, message: '获取用户信息时失败,结果为空' }
}
} catch (error) {
console.error('ERROR', '获取用户信息时失败,查询数据时出错:0', error)
return { success: false, message: '获取用户信息时失败' }
}
} else {
console.error('ERROR', '获取用户信息时失败,uid 或 token 为空')
return { success: false, message: '获取用户信息时失败,必要的参数为空' }
}
} catch (error) {
console.error('ERROR', '获取用户信息时失败,未知错误:', error)
return { success: false, message: '获取用户信息时失败,未知错误' }
}
}

/**
* 更新用户头像,并获取用于用户上传头像的预签名 URL, 上传限时 60 秒
* @param uid 用户 ID
Expand Down

0 comments on commit 87b0a49

Please sign in to comment.