完善BlogContent接口,完善ServerDtdResponse
This commit is contained in:
56
Server/src/APIs/GetBlogContent.ts
Normal file
56
Server/src/APIs/GetBlogContent.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { API } from "../Plugs/API/API";
|
||||
import ServerStdResponse from "../ServerStdResponse";
|
||||
import MySQLConnection from '../Plugs/MySQLConnection'
|
||||
import { Buffer } from 'buffer';
|
||||
import axios from "axios";
|
||||
|
||||
|
||||
// 获取博客内容
|
||||
class GetBlogContent extends API {
|
||||
constructor() {
|
||||
super('GET', '/blogContent');
|
||||
}
|
||||
private defaultAccessLevel = 6;
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
let { bloguuid } = data;
|
||||
if (!bloguuid || bloguuid.length != 32) {
|
||||
return res.json(ServerStdResponse.INVALID_PARAMS);
|
||||
}
|
||||
|
||||
let blogContentRes = await MySQLConnection.execute('SELECT * from blog WHERE access_level > ? AND uuid = ? ', [this.defaultAccessLevel, bloguuid]);
|
||||
if (!blogContentRes) {
|
||||
this.logger.error('查询时数据库发生错误');
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
if (blogContentRes.length != 1) {
|
||||
this.logger.warn('查询的博客不存在或不可见', bloguuid);
|
||||
return res.json(ServerStdResponse.BLOG.NOTFOUND);
|
||||
}
|
||||
// 返回处理后的数据
|
||||
try {
|
||||
const markdownUrl = blogContentRes[0].src;
|
||||
const response = await axios.get(markdownUrl);
|
||||
const base64Content = Buffer.from(response.data, 'utf-8').toString('base64');
|
||||
|
||||
MySQLConnection.execute('UPDATE blog SET visit_count = visit_count + 1 WHERE uuid = ?', [bloguuid]);
|
||||
return res.json({
|
||||
...ServerStdResponse.OK, data: {
|
||||
data: base64Content,
|
||||
info: {
|
||||
title: blogContentRes[0].title,
|
||||
description: blogContentRes[0].description,
|
||||
publish_time: blogContentRes[0].publish_time,
|
||||
visit_count: blogContentRes[0].visit_count,
|
||||
like_count: blogContentRes[0].like_count
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error('获取博客文章内容时发生错误', error)
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default GetBlogContent;
|
||||
@@ -5,6 +5,7 @@ import config from "../config";
|
||||
import GetTest from "../APIs/GetTest";
|
||||
import GetResourceList from "../APIs/GetResourceList";
|
||||
import GetBlogList from "../APIs/GetBlogList";
|
||||
import GetBlogContent from "../APIs/GetBlogContent";
|
||||
|
||||
class Server {
|
||||
private logger = new Logger('Server');
|
||||
@@ -20,6 +21,7 @@ class Server {
|
||||
this.apiLoader.add(GetTest);
|
||||
this.apiLoader.add(GetResourceList);
|
||||
this.apiLoader.add(GetBlogList);
|
||||
this.apiLoader.add(GetBlogContent);
|
||||
|
||||
this.apiLoader.start(config.apiPort);
|
||||
}
|
||||
|
||||
@@ -11,42 +11,24 @@ const ServerStdResponse = {
|
||||
code: -2,
|
||||
message: 'Invalid parameters'
|
||||
},
|
||||
INVALID_TOKEN: {
|
||||
code: -3,
|
||||
message: 'Invalid token'
|
||||
},
|
||||
SERVER_ERROR: {
|
||||
code: -4,
|
||||
code: -3,
|
||||
message: 'Server error'
|
||||
},
|
||||
API_NOT_FOUND: {
|
||||
code: -5,
|
||||
code: -4,
|
||||
message: 'API not found'
|
||||
},
|
||||
AUTH_ERROR: {
|
||||
code: -6,
|
||||
code: -5,
|
||||
message: 'Authentication error'
|
||||
},
|
||||
IDENTIFY_FAILED: {
|
||||
code: -7,
|
||||
message: 'Identify failed'
|
||||
},
|
||||
GOODS: {
|
||||
BLOG: {
|
||||
NOTFOUND: {
|
||||
code: -4001,
|
||||
message: 'Goods not found'
|
||||
message: 'Blog not found'
|
||||
}
|
||||
},
|
||||
ORDER: {
|
||||
NOTFOUND: {
|
||||
code: -5001,
|
||||
message: 'Order not found'
|
||||
},
|
||||
ALREADY_CANCEL: {
|
||||
code: -5002,
|
||||
message: 'Order already canceled'
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
|
||||
export default ServerStdResponse;
|
||||
Reference in New Issue
Block a user