完善BlogContent接口,完善ServerDtdResponse
This commit is contained in:
@@ -18,6 +18,8 @@
|
|||||||
"typescript": "^5.4.5"
|
"typescript": "^5.4.5"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@types/node-fetch": "^2.6.11",
|
||||||
|
"axios": "^1.7.5",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
"ioredis": "^5.4.1",
|
"ioredis": "^5.4.1",
|
||||||
|
|||||||
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 GetTest from "../APIs/GetTest";
|
||||||
import GetResourceList from "../APIs/GetResourceList";
|
import GetResourceList from "../APIs/GetResourceList";
|
||||||
import GetBlogList from "../APIs/GetBlogList";
|
import GetBlogList from "../APIs/GetBlogList";
|
||||||
|
import GetBlogContent from "../APIs/GetBlogContent";
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
private logger = new Logger('Server');
|
private logger = new Logger('Server');
|
||||||
@@ -20,6 +21,7 @@ class Server {
|
|||||||
this.apiLoader.add(GetTest);
|
this.apiLoader.add(GetTest);
|
||||||
this.apiLoader.add(GetResourceList);
|
this.apiLoader.add(GetResourceList);
|
||||||
this.apiLoader.add(GetBlogList);
|
this.apiLoader.add(GetBlogList);
|
||||||
|
this.apiLoader.add(GetBlogContent);
|
||||||
|
|
||||||
this.apiLoader.start(config.apiPort);
|
this.apiLoader.start(config.apiPort);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,42 +11,24 @@ const ServerStdResponse = {
|
|||||||
code: -2,
|
code: -2,
|
||||||
message: 'Invalid parameters'
|
message: 'Invalid parameters'
|
||||||
},
|
},
|
||||||
INVALID_TOKEN: {
|
|
||||||
code: -3,
|
|
||||||
message: 'Invalid token'
|
|
||||||
},
|
|
||||||
SERVER_ERROR: {
|
SERVER_ERROR: {
|
||||||
code: -4,
|
code: -3,
|
||||||
message: 'Server error'
|
message: 'Server error'
|
||||||
},
|
},
|
||||||
API_NOT_FOUND: {
|
API_NOT_FOUND: {
|
||||||
code: -5,
|
code: -4,
|
||||||
message: 'API not found'
|
message: 'API not found'
|
||||||
},
|
},
|
||||||
AUTH_ERROR: {
|
AUTH_ERROR: {
|
||||||
code: -6,
|
code: -5,
|
||||||
message: 'Authentication error'
|
message: 'Authentication error'
|
||||||
},
|
},
|
||||||
IDENTIFY_FAILED: {
|
BLOG: {
|
||||||
code: -7,
|
|
||||||
message: 'Identify failed'
|
|
||||||
},
|
|
||||||
GOODS: {
|
|
||||||
NOTFOUND: {
|
NOTFOUND: {
|
||||||
code: -4001,
|
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;
|
} as const;
|
||||||
|
|
||||||
export default ServerStdResponse;
|
export default ServerStdResponse;
|
||||||
Reference in New Issue
Block a user