完善BlogContent接口,完善ServerDtdResponse

This commit is contained in:
2024-08-30 12:46:35 +08:00
parent 8a6ee6d98c
commit 223fbf39b2
4 changed files with 65 additions and 23 deletions

View File

@@ -18,6 +18,8 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@types/node-fetch": "^2.6.11",
"axios": "^1.7.5",
"cors": "^2.8.5",
"express": "^4.19.2",
"ioredis": "^5.4.1",

View 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;

View File

@@ -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);
}

View File

@@ -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;