first commit

This commit is contained in:
编码猿
2024-09-27 02:06:13 +08:00
commit 852d94fbb9
36760 changed files with 3274413 additions and 0 deletions

View File

@@ -0,0 +1,164 @@
import { Article } from '@/entity/Article';
import { Categories } from '@/entity/Categories';
import { Tag } from '@/entity/Tag';
import { Comment } from '@/entity/Comment';
import { User } from '@/entity/User';
import { Service, Inject } from 'typedi'
import { Like } from "typeorm"
import { Entity } from '@/utils/orm'
import { Segment } from '@/utils/segment'
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { Article as ResArticle, comment as commentType, categories} from '@/m_res/Article';
import { CommentParams } from '@/model/request/Comment';
import { Token } from '@/m_req/Token'
import moment from 'moment';
@Service()
export class ArticleDao {
@Inject()
orm!: Entity
@Inject()
json!: JsonResult<any>;
@Inject()
segment!: Segment
async pushCommentDao(params: CommentParams, token: Token) {
let res = await this.orm.source()
.insert()
.into(Comment)
.values({
comment_text: params.comment_text,
comment_time: moment().valueOf(),
comment_father_id: params.comment_father_id,
article_id: params.article_id,
user_id: token.user_id,
user_reply_id: params.user_reply_id
})
.execute();
return this.json.success(null, msgList.pushMessageSuccess)
}
async GetArticleDao(text: string): Promise<res<Array<ResArticle>>> {
let res: ResArticle[] = []
if (text == undefined) {
res = <ResArticle[]>await this.orm.repository(Article).getMany();
} else {
res = <ResArticle[]><unknown>await this.orm.get(Article).findBy({
article_title: Like(`%${text}%`),
})
}
res.forEach(v => {
v.article_text = v.article_text.substr(0,100)
})
return this.json.success(res);
}
async GetArticleInfoDao(id: number): Promise<res<null>> {
let data = await this.orm.get(Article).findOne({ where: { article_id: id } })
if (data != null) {
let newData: ResArticle = <ResArticle><unknown>data
newData.categories_id = await this.orm.get(Categories).find({
where: (<string>data.categories_id).split(",").map(v => {
return { categories_id: Number(v) }
})
})
newData.tag_id = await this.orm.get(Tag).find({
where: (<string>data.tag_id).split(",").map(v => {
return { tag_id: Number(v) }
})
})
let comment = <commentType[]><unknown>await this.orm.get(Comment).find({
where: { article_id: id }
})
for (let i = 0; i < comment.length; i++) {
let me = await this.orm.get(User).findOne({
select: { user_name: true, user_img: true },
where: { user_id: comment[i].user_id }
})
let you = await this.orm.get(User).findOne({
select: { user_name: true },
where: { user_id: comment[i].user_reply_id }
})
if (me != null || you != null) {
for (const key in me) {
comment[i][key] = me[key]
}
for (const key in you) {
comment[i]["user_reply_name"] = you[key]
}
}
}
let leaveOne: commentType[] = [];
comment.forEach(v => {
if (v.comment_father_id == 0) {
v.comment_children = []
leaveOne.push(<commentType><unknown>v)
}
})
leaveOne.forEach(v => {
comment.forEach(j => {
if (v.comment_id == j.comment_father_id) {
// @ts-ignore
v.comment_children.push(j)
}
})
})
newData.article_comment = leaveOne;
return this.json.success(newData)
}
return this.json.error(null, "id不存在")
}
async GetCategoriesListDao(): Promise<res<categories[]>>{
return this.json.success(await this.orm.get(Categories).find({
order: {
categories_id: "DESC"
},
}))
}
async GetCategoriesInfoDao(id: number): Promise<res<ResArticle[]>>{
let res = await this.orm.get(Article).findBy({
categories_id: Like(`%${id}%`),
})
return this.json.success(res)
}
async SearchDao(word: string): Promise<res<ResArticle[]>>{
let sql: string = ""
this.segment.ik(word).forEach(v => {
sql += ` or article_title like '%${v.w}%'`
})
let res = await this.orm.repository(Article)
.where(sql.replace("or", ""))
.getMany()
return this.json.success(res)
}
}

View File

@@ -0,0 +1,27 @@
import { User, selectUserField } from '@/entity/User';
import { User as UserType } from '@/m_res/user'
import { Service, Inject } from 'typedi'
import { Entity } from '@/utils/orm'
import { addUser } from '@/model/request/User';
import moment from 'moment';
import md5 from 'md5';
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import jsonwebtoken from 'jsonwebtoken'
import config from '@/config'
import { Link } from '@/entity/Link';
@Service()
export class LinkDao {
@Inject()
orm!: Entity
@Inject()
json!: JsonResult<any>;
async GetAllLink(): Promise<res<Link[]>> {
let res = await this.orm.repository(Link).getMany();
return this.json.success(res);
}
}

View File

@@ -0,0 +1,54 @@
import { Service, Inject } from 'typedi'
import { Entity } from '@/utils/orm'
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { Menu } from '@/entity/Menu';
import { addMenuParams } from "@/model/request/Menu";
@Service()
export class MenuDao {
@Inject()
orm!: Entity
@Inject()
json!: JsonResult<any>;
async addMenuDao(params: addMenuParams): Promise<res<null>> {
let res = await this.orm.source()
.insert()
.into(Menu)
.values({
menu_text: params.text,
menu_icon: params.icon,
menu_href: params.path,
menu_type: params.type,
menu_father_id: params.father_id,
menu_status: 0
})
.execute();
return this.json.success(null, msgList.pushMessageSuccess)
}
async GetAllMenuDao(): Promise<res<Menu[]>> {
let res = await this.orm.repository(Menu).getMany();
let fa: Menu[] = []
res.forEach(v => {
if (v.menu_type == 1) {
fa.push(<Menu>v)
v.children = []
}
})
res.forEach(j => {
fa.forEach(v => {
if (j.menu_father_id == v.menu_id) {
v.children.push(<Menu>j)
}
})
})
return this.json.success(fa);
}
}

View File

@@ -0,0 +1,84 @@
import { User, selectUserField } from '@/entity/User';
import { User as UserType } from '@/m_res/user'
import { Service, Inject } from 'typedi'
import { Entity } from '@/utils/orm'
import { addUser } from '@/model/request/User';
import moment from 'moment';
import md5 from 'md5';
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import jsonwebtoken from 'jsonwebtoken'
import config from '@/config'
@Service()
export class UserDao {
@Inject()
entity!: Entity
@Inject()
json!: JsonResult<any>;
async GetOneUser(id: number): Promise<res<UserType | null>> {
let data = <UserType>await this.entity.get(User).findOne({
select: selectUserField,
where: { user_id: id }
});
if (data) {
return this.json.success(data)
} else {
return this.json.success(null,msgList.NoFound)
}
}
async addUserDao(params: addUser): Promise<res<null>> {
let isHave = await this.entity.get(User).findOne({
where: { "user_name": params.name }
});
console.log("isHave: ", isHave);
if (isHave != null) {
return this.json.success(null, msgList.UserNameExist)
} else {
await this.entity.source()
.insert()
.into(User)
.values([
{
user_name: params.name,
user_img: "https://upload.jianshu.io/users/upload_avatars/3136195/484e32c3504a.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240",
user_pwd: md5(params.pwd),
user_type: 0,
user_addtime: moment().valueOf()
},
])
.execute()
return this.json.success(null, msgList.addUserSuccess)
}
}
async LoginDao(params: addUser): Promise<res<UserType | null>> {
// const user = await this.entity.get(User).findOneBy({
// user_name: params.name,
// user_pwd: md5(params.pwd),
// })
const user = await this.entity.repository(User)
.where("user.user_name = :user_name", { user_name: params.name })
.andWhere('user.user_pwd = :user_pwd', { user_pwd: md5(params.pwd) })
.getOne()
if (user) {
var token = jsonwebtoken.sign({
user_id: user.user_id,
user_name: user.user_name
}, config.jwtKey);
return this.json.success(token)
} else {
return this.json.error(null,msgList.PasswordError)
}
}
}