提交审核post
This commit is contained in:
父節點
edf4e11eb0
當前提交
959bf13156
21
src/helpers/entity-fill.ts
Normal file
21
src/helpers/entity-fill.ts
Normal file
@ -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()
|
||||
return instance
|
||||
}
|
||||
|
||||
export function getTimestampInSeconds() {
|
||||
return Math.floor(Date.now() / 1000)
|
||||
}
|
||||
|
@ -1,7 +1,13 @@
|
||||
import 'reflect-metadata'
|
||||
import {Service} from "typedi"
|
||||
import {IPost} from "../types"
|
||||
import {db} from "../includes"
|
||||
|
||||
@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 {IUser} from "../types"
|
||||
import {db} from "../includes"
|
||||
import {fillIUser} from "../helpers/entity-fill"
|
||||
|
||||
@Service()
|
||||
export class UserModel {
|
||||
@ -17,12 +18,7 @@ export class UserModel {
|
||||
if (!user.length) {
|
||||
return null
|
||||
}
|
||||
return <IUser>{
|
||||
id: user[0].id,
|
||||
username: user[0].username,
|
||||
realName: user[0].realName,
|
||||
role: user[0].role
|
||||
}
|
||||
return fillIUser(user[0])
|
||||
}
|
||||
|
||||
async findUserById(id: number) {
|
||||
@ -30,11 +26,6 @@ export class UserModel {
|
||||
if (!user.length) {
|
||||
return null
|
||||
}
|
||||
return <IUser>{
|
||||
id: user[0].id,
|
||||
username: user[0].username,
|
||||
realName: user[0].realName,
|
||||
role: user[0].role
|
||||
}
|
||||
return fillIUser(user[0])
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import express from "express"
|
||||
import {body} from "express-validator"
|
||||
import * as Validator from "validator"
|
||||
import {hasValidationErrors} from "../includes"
|
||||
import {JWTUserPayload} from "../types"
|
||||
|
||||
let postService = Container.get(PostService)
|
||||
|
||||
@ -12,22 +13,10 @@ let postRouter = express.Router()
|
||||
postRouter.post('/submit',
|
||||
body('content').notEmpty().withMessage('请输入内容')
|
||||
.escape(),
|
||||
body('image').isJSON().withMessage('图片必须为JSON格式').bail().custom((input, {req}) => {
|
||||
let arr: string[] = JSON.parse(input)
|
||||
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) => {
|
||||
body('image').isURL({host_whitelist: [/.*\.360buyimg\.com/]}).withMessage('仅支援*.360buyimg.com下的图片'),
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
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 {Service} from "typedi"
|
||||
import {Inject, Service} from "typedi"
|
||||
import {PostModel} from "../models/post-model"
|
||||
import {JWTUserPayload} from "../types"
|
||||
import {getTimestampInSeconds, resultJson} from "../includes"
|
||||
|
||||
@Service()
|
||||
export class PostService {
|
||||
async submit(content: string, imageArr: string[]) {
|
||||
console.log(content)
|
||||
console.log(imageArr)
|
||||
@Inject()
|
||||
postModel!: PostModel
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
20
src/types.ts
20
src/types.ts
@ -3,15 +3,31 @@ export interface IResultJson {
|
||||
data: any
|
||||
}
|
||||
|
||||
export type TUserRole = 1 | 2
|
||||
|
||||
export interface IUser {
|
||||
id?: number,
|
||||
username: string,
|
||||
realName: string,
|
||||
role: 1 | 2
|
||||
role: TUserRole
|
||||
}
|
||||
|
||||
|
||||
export interface JWTUserPayload {
|
||||
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
|
||||
}
|
||||
|
載入中…
x
新增問題並參考
Block a user