Skip to content

Commit

Permalink
feat: 文章草稿可指定作者,默认为登录用户 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Sep 5, 2022
1 parent 3005034 commit e2a23e2
Show file tree
Hide file tree
Showing 25 changed files with 143 additions and 13 deletions.
25 changes: 25 additions & 0 deletions packages/admin/src/components/AuthorField/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getAllCollaboratorsList } from '@/services/van-blog/api';
import { ProFormSelect } from '@ant-design/pro-form';
export default function () {
return (
<>
<ProFormSelect
width="md"
id="author"
name="author"
label="作者"
placeholder="不填默认为登录者本人"
request={async () => {
const msg = await getAllCollaboratorsList();

return (
msg?.data?.map((item) => ({
label: item.nickname || item.name,
value: item.nickname || item.name,
})) || []
);
}}
/>
</>
);
}
5 changes: 3 additions & 2 deletions packages/admin/src/components/NewArticleModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@ant-design/pro-components';
import { Button, Modal } from 'antd';
import moment from 'moment';
import AuthorField from '../AuthorField';

export default function (props) {
const { onFinish } = props;
Expand All @@ -25,8 +26,7 @@ export default function (props) {
if (location.hostname == 'blog-demo.mereith.com') {
Modal.info({
title: '演示站禁止新建文章!',
content:
'本来是可以的,但有个人在演示站首页放黄色信息,所以关了这个权限了。',
content: '本来是可以的,但有个人在演示站首页放黄色信息,所以关了这个权限了。',
});
return;
}
Expand Down Expand Up @@ -55,6 +55,7 @@ export default function (props) {
placeholder="请输入标题"
rules={[{ required: true, message: '这是必填项' }]}
/>
<AuthorField />
<ProFormText
width="md"
id="topC"
Expand Down
2 changes: 2 additions & 0 deletions packages/admin/src/components/NewDraftModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@ant-design/pro-components';
import { Button } from 'antd';
import moment from 'moment';
import AuthorField from '../AuthorField';
export default function (props) {
const { onFinish } = props;
return (
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function (props) {
placeholder="请输入标题"
rules={[{ required: true, message: '这是必填项' }]}
/>
<AuthorField />
<ProFormSelect
mode="tags"
tokenSeparators={[',']}
Expand Down
2 changes: 2 additions & 0 deletions packages/admin/src/components/UpdateModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAllCategories, getTags, updateArticle, updateDraft } from '@/service
import { ModalForm, ProFormDateTimePicker, ProFormSelect, ProFormText } from '@ant-design/pro-form';
import { message, Modal } from 'antd';
import moment from 'moment';
import AuthorField from '../AuthorField';
export default function (props: {
currObj: any;
setLoading: any;
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function (props: {
placeholder="请输入标题"
rules={[{ required: true, message: '这是必填项' }]}
/>
<AuthorField />
<ProFormSelect
mode="tags"
tokenSeparators={[',']}
Expand Down
7 changes: 7 additions & 0 deletions packages/admin/src/pages/InitPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ const InitPage = () => {
label="登录用户名"
placeholder={'请输入登录用户名'}
></ProFormText>
{/* <ProFormText
name="nickname"
required={true}
rules={[{ required: true, message: '这是必填项' }]}
label="昵称"
placeholder={'请输入昵称(显示的名字)'}
></ProFormText> */}
<ProFormText.Password
name="password"
required={true}
Expand Down
10 changes: 9 additions & 1 deletion packages/admin/src/pages/SystemConfig/tabs/User.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ export default function () {
label="登录用户名"
placeholder={'请输入登录用户名'}
/>
{/* <ProFormText
width="lg"
name="nickname"
required={true}
rules={[{ required: true, message: '这是必填项' }]}
label="昵称"
placeholder={'请输入昵称(显示的名字)'}
></ProFormText> */}
<ProFormText.Password
width="lg"
name="password"
autocomplete="new-password"
required={true}
rules={[{ required: true, message: '这是必填项' }]}
autocomplete="new-password"
label="登录密码"
placeholder={'请输入登录密码'}
/>
Expand Down
5 changes: 5 additions & 0 deletions packages/admin/src/services/van-blog/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ export async function getTags() {
method: 'GET',
});
}
export async function getAllCollaboratorsList() {
return request(`/api/admin/collaborator/list`, {
method: 'GET',
});
}
export async function importAll() {
return request(`/api/admin/backup/import`, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Post,
Put,
Query,
Req,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
Expand All @@ -16,13 +17,17 @@ import { SortOrder } from 'src/types/sort';
import { ArticleProvider } from 'src/provider/article/article.provider';
import { AdminGuard } from 'src/provider/auth/auth.guard';
import { ISRProvider } from 'src/provider/isr/isr.provider';
import { UserProvider } from 'src/provider/user/user.provider';
import { MetaProvider } from 'src/provider/meta/meta.provider';
@ApiTags('article')
@UseGuards(...AdminGuard)
@Controller('/api/admin/article')
export class ArticleController {
constructor(
private readonly articleProvider: ArticleProvider,
private readonly isrProvider: ISRProvider,
private readonly userProvider: UserProvider,
private readonly metaProvider: MetaProvider,
) {}

@Get('/')
Expand Down Expand Up @@ -87,13 +92,17 @@ export class ArticleController {
}

@Post()
async create(@Body() createDto: CreateArticleDto) {
async create(@Req() req: any, @Body() createDto: CreateArticleDto) {
if (config.demo && config.demo == 'true') {
return {
statusCode: 401,
message: '演示站禁止创建文章!',
};
}
const author = req?.user?.nickname || undefined;
if (!createDto.author) {
createDto.author = author;
}
const data = await this.articleProvider.create(createDto);
this.isrProvider.activeAll('创建文章触发增量渲染!');
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ApiTags } from '@nestjs/swagger';
import { config } from 'src/config';

import { AdminGuard } from 'src/provider/auth/auth.guard';
import { MetaProvider } from 'src/provider/meta/meta.provider';

import { UserProvider } from 'src/provider/user/user.provider';
import { Collaborator } from 'src/types/collaborator';
Expand All @@ -21,7 +22,10 @@ import { Collaborator } from 'src/types/collaborator';
@UseGuards(...AdminGuard)
@Controller('/api/admin/collaborator/')
export class CollaboratorController {
constructor(private readonly userProvider: UserProvider) {}
constructor(
private readonly userProvider: UserProvider,
private readonly metaProvider: MetaProvider,
) {}
@Get()
async getAllCollaborators() {
const data = await this.userProvider.getAllCollaborators();
Expand All @@ -30,6 +34,22 @@ export class CollaboratorController {
data: data || [],
};
}
@Get('/list')
async getAllCollaboratorsList() {
// 管理员优先用作者名称吧
const siteInfo = await this.metaProvider.getSiteInfo();
const admin = await this.userProvider.getUser(true);
const adminUser = {
name: admin.name,
nickname: siteInfo.author,
id: 0,
};
const data = await this.userProvider.getAllCollaborators(true);
return {
statusCode: 200,
data: [adminUser, ...data] || [adminUser],
};
}
@Delete('/:id')
async deleteCollaboratorById(@Param('id') id: number) {
if (config.demo && config.demo == 'true') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Post,
Put,
Query,
Req,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
Expand Down Expand Up @@ -79,7 +80,11 @@ export class DraftController {
}

@Post()
async create(@Body() createDto: CreateDraftDto) {
async create(@Req() req: any, @Body() createDto: CreateDraftDto) {
const author = req?.user?.nickname || undefined;
if (!createDto.author) {
createDto.author = author;
}
const data = await this.draftProvider.create(createDto);
return {
statusCode: 200,
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/provider/article/article.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class ArticleProvider {
visited: 1,
private: 1,
hidden: 1,
author: 1,
};

adminView = {
Expand All @@ -61,6 +62,7 @@ export class ArticleProvider {
_id: 0,
viewer: 1,
visited: 1,
author: 1,
};

listView = {
Expand All @@ -77,6 +79,7 @@ export class ArticleProvider {
_id: 0,
viewer: 1,
visited: 1,
author: 1,
};

toPublic(oldArticles: Article[]) {
Expand Down
12 changes: 11 additions & 1 deletion packages/server/src/provider/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { config } from 'src/config/index';
import { MetaProvider } from '../meta/meta.provider';
import { UserProvider } from '../user/user.provider';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly userProvider: UserProvider) {
constructor(
private readonly userProvider: UserProvider,
private readonly metaProvider: MetaProvider,
) {
super({
// 获取请求header token值
jwtFromRequest: ExtractJwt.fromHeader('token'),
Expand All @@ -21,6 +25,12 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
if (payload.sub != 0) {
const user = await this.userProvider.getCollaboratorById(payload.sub);
moreDto.permissions = user.permissions;
moreDto.nickname = user.nickname;
} else {
const user = await this.userProvider.getUser();
const siteInfo = await this.metaProvider.getSiteInfo();
const authorName = siteInfo.author;
moreDto.nickname = authorName || user.nickname;
}
return { name: payload.username, id: payload.sub, ...moreDto };
}
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/provider/draft/draft.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class DraftProvider {
category: 1,
updatedAt: 1,
createdAt: 1,
author: 1,
id: 1,
_id: 0,
};
Expand All @@ -35,6 +36,7 @@ export class DraftProvider {
category: 1,
updatedAt: 1,
createdAt: 1,
author: 1,
id: 1,
_id: 0,
};
Expand All @@ -45,6 +47,7 @@ export class DraftProvider {
category: 1,
updatedAt: 1,
createdAt: 1,
author: 1,
id: 1,
_id: 0,
};
Expand Down Expand Up @@ -171,6 +174,7 @@ export class DraftProvider {
content: draft.content,
tags: draft.tags,
category: draft.category,
author: draft.author,
};
for (const [k, v] of Object.entries(options || {})) {
createArticleDto[k] = v;
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/provider/init/init.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class InitProvider {
id: 0,
name: user.username,
password: user.password,
mickname: user?.nickname || user.username,
type: 'admin',
});
await this.metaModel.create({
Expand Down
3 changes: 0 additions & 3 deletions packages/server/src/provider/meta/meta.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ export class MetaProvider {
async updateSiteInfo(updateSiteInfoDto: UpdateSiteInfoDto) {
// @ts-ignore eslint-disable-next-line @typescript-eslint/ban-ts-comment
const { name, password, ...updateDto } = updateSiteInfoDto;
if (name && name != '') {
this.userProvider.updateUser({ name: name, password });
}
const oldSiteInfo = await this.getSiteInfo();
return this.metaModel.updateOne(
{},
Expand Down
16 changes: 14 additions & 2 deletions packages/server/src/provider/user/user.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { Collaborator } from 'src/types/collaborator';
@Injectable()
export class UserProvider {
constructor(@InjectModel('User') private userModel: Model<UserDocument>) {}
async getUser() {
async getUser(isList?: boolean) {
if (isList) {
return await this.userModel.findOne(
{ id: 0 },
{ id: 1, name: 1, nickname: 1 },
);
}
return await this.userModel.findOne({ id: 0 }).exec();
}
async validateUser(name: string, password: string) {
Expand Down Expand Up @@ -43,7 +49,13 @@ export class UserProvider {
async getCollaboratorById(id: number) {
return await this.userModel.findOne({ id, type: 'collaborator' });
}
async getAllCollaborators() {
async getAllCollaborators(isList?: boolean) {
if (isList) {
return await this.userModel.find(
{ type: 'collaborator' },
{ id: 1, name: 1, nickname: 1 },
);
}
return await this.userModel.find({ type: 'collaborator' });
}

Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/scheme/article.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export class Article extends Document {
@Prop({ default: false })
hidden: boolean;

@Prop()
author: string;

@Prop({ default: false })
private: boolean;

Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/scheme/draft.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class Draft extends Document {
@Prop({ default: [] })
tags: string[];

@Prop()
author: string;

@Prop()
category: string;

Expand Down
Loading

0 comments on commit e2a23e2

Please sign in to comment.