提交审核post

This commit is contained in:
juzeon 2021-07-25 19:29:05 +08:00
父節點 edf4e11eb0
當前提交 959bf13156
共有 7 個檔案被更改,包括 74 行新增35 行删除

查看文件

@ -0,0 +1,21 @@
import {IPost, IUser} from "../types"
export function fillIUser(single: any) {
return <IUser>{
id: single.id,
username: single.username,
realName: single.realName,
role: single.role
}
}
export function fillIPost(single: any) {
return <IPost>{
id: single.id,
userId: single.userId,
content: single.content,
image: single.image,
status: single.status,
time: single.time
}
}

查看文件

@ -73,3 +73,7 @@ export function getAxiosInstance() {
instance.defaults.jar = new tough.CookieJar() instance.defaults.jar = new tough.CookieJar()
return instance return instance
} }
export function getTimestampInSeconds() {
return Math.floor(Date.now() / 1000)
}

查看文件

@ -1,7 +1,13 @@
import 'reflect-metadata' import 'reflect-metadata'
import {Service} from "typedi" import {Service} from "typedi"
import {IPost} from "../types"
import {db} from "../includes"
@Service() @Service()
export class PostModel { export class PostModel {
async insertPost(post: IPost) {
return db.query('insert into posts (userId,content,image,status,time) values(?,?,?,?,?)', [
post.userId, post.content, post.image, post.status, post.time
])
}
} }

查看文件

@ -2,6 +2,7 @@ import 'reflect-metadata'
import {Service} from "typedi" import {Service} from "typedi"
import {IUser} from "../types" import {IUser} from "../types"
import {db} from "../includes" import {db} from "../includes"
import {fillIUser} from "../helpers/entity-fill"
@Service() @Service()
export class UserModel { export class UserModel {
@ -17,12 +18,7 @@ export class UserModel {
if (!user.length) { if (!user.length) {
return null return null
} }
return <IUser>{ return fillIUser(user[0])
id: user[0].id,
username: user[0].username,
realName: user[0].realName,
role: user[0].role
}
} }
async findUserById(id: number) { async findUserById(id: number) {
@ -30,11 +26,6 @@ export class UserModel {
if (!user.length) { if (!user.length) {
return null return null
} }
return <IUser>{ return fillIUser(user[0])
id: user[0].id,
username: user[0].username,
realName: user[0].realName,
role: user[0].role
}
} }
} }

查看文件

@ -5,6 +5,7 @@ import express from "express"
import {body} from "express-validator" import {body} from "express-validator"
import * as Validator from "validator" import * as Validator from "validator"
import {hasValidationErrors} from "../includes" import {hasValidationErrors} from "../includes"
import {JWTUserPayload} from "../types"
let postService = Container.get(PostService) let postService = Container.get(PostService)
@ -12,22 +13,10 @@ let postRouter = express.Router()
postRouter.post('/submit', postRouter.post('/submit',
body('content').notEmpty().withMessage('请输入内容') body('content').notEmpty().withMessage('请输入内容')
.escape(), .escape(),
body('image').isJSON().withMessage('图片必须为JSON格式').bail().custom((input, {req}) => { body('image').isURL({host_whitelist: [/.*\.360buyimg\.com/]}).withMessage('仅支援*.360buyimg.com下的图片'),
let arr: string[] = JSON.parse(input) async (req: express.Request, res: express.Response) => {
if(!arr.length){
throw new Error('图片数量至少为1')
}
for (let url of arr) {
if (!Validator.default.isURL(url, {host_whitelist: [/.*\.360buyimg\.com/]})) {
throw new Error('必须是360buyimg.com下的图片')
}
}
req.body.imageArr = arr
return true
}),
(req: express.Request, res: express.Response) => {
if (hasValidationErrors(req, res)) return if (hasValidationErrors(req, res)) return
res.json(postService.submit(req.body.content, req.body.imageArr)) res.json(await postService.submit(req.user as JWTUserPayload,req.body.content, req.body.image))
} }
) )

查看文件

@ -1,10 +1,22 @@
import 'reflect-metadata' import 'reflect-metadata'
import {Service} from "typedi" import {Inject, Service} from "typedi"
import {PostModel} from "../models/post-model"
import {JWTUserPayload} from "../types"
import {getTimestampInSeconds, resultJson} from "../includes"
@Service() @Service()
export class PostService { export class PostService {
async submit(content: string, imageArr: string[]) { @Inject()
console.log(content) postModel!: PostModel
console.log(imageArr)
async submit(jwtUserPayload: JWTUserPayload, content: string, image: string) {
let packet = await this.postModel.insertPost({
userId: jwtUserPayload.id,
content: content,
image: image,
status: 1,
time: getTimestampInSeconds()
})
return resultJson.success(packet.insertId)
} }
} }

查看文件

@ -3,15 +3,31 @@ export interface IResultJson {
data: any data: any
} }
export type TUserRole = 1 | 2
export interface IUser { export interface IUser {
id?: number, id?: number,
username: string, username: string,
realName: string, realName: string,
role: 1 | 2 role: TUserRole
} }
export interface JWTUserPayload { export interface JWTUserPayload {
id: number, id: number,
role: 1 | 2 role: TUserRole
}
export type TPostPending = 1
export type TPostPublished = 2
export type TPostRejected = 3
export type TPostStatus = TPostPending | TPostPublished | TPostRejected
export interface IPost {
id?: number,
userId: number,
content: string,
image: string,
status: TPostStatus,
time: number
} }