使用pg数据库重构
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
|
||||
// 删除博客
|
||||
@@ -10,13 +10,13 @@ class DelBlog extends API {
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
let { id } = data;
|
||||
if (!id) {
|
||||
let { uuid } = data;
|
||||
if (!uuid) {
|
||||
return res.json(ServerStdResponse.PARAMS_MISSING);
|
||||
}
|
||||
let execRes = await MySQLConnection.execute('DELETE FROM blog WHERE `id` = ?', [id]);
|
||||
let execRes = await Database.query('DELETE FROM blog WHERE uuid = $1', [uuid]);
|
||||
|
||||
if (!execRes || execRes.affectedRows != 1) {
|
||||
if (!execRes) {
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
return res.json({ ...ServerStdResponse.OK });
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
|
||||
// 删除资源
|
||||
@@ -10,13 +10,13 @@ class DelResource extends API {
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
let { id } = data;
|
||||
if (!id) {
|
||||
let { uuid } = data;
|
||||
if (!uuid) {
|
||||
return res.json(ServerStdResponse.PARAMS_MISSING);
|
||||
}
|
||||
let execRes = await MySQLConnection.execute('DELETE FROM resource WHERE `id` = ?', [id]);
|
||||
let execRes = await Database.query('DELETE FROM resource WHERE uuid = $1', [uuid]);
|
||||
|
||||
if (!execRes || execRes.affectedRows != 1) {
|
||||
if (!execRes) {
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
return res.json({ ...ServerStdResponse.OK });
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
import { Blog } from "@/Types/Schema";
|
||||
|
||||
// 获取博客列表
|
||||
class GetBlogs extends API {
|
||||
@@ -10,8 +11,7 @@ class GetBlogs extends API {
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
// const { uuid } = data._jwt;
|
||||
let resourcesRes = await MySQLConnection.execute("SELECT * FROM blog ORDER BY id DESC");
|
||||
let resourcesRes = await Database.query<Blog>("SELECT * FROM blog ORDER BY created_at DESC");
|
||||
if (!resourcesRes) {
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { API, RequestData } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
import jwt from "jsonwebtoken";
|
||||
import config from "../../config";
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
import { Resource } from "@/Types/Schema";
|
||||
|
||||
// 获取资源列表
|
||||
class GetResources extends API {
|
||||
@@ -11,7 +12,7 @@ class GetResources extends API {
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
// const { uuid } = data._jwt;
|
||||
let resourcesRes = await MySQLConnection.execute("SELECT * FROM resource");
|
||||
let resourcesRes = await Database.query<Resource>("SELECT * FROM resource ORDER BY type, recommand, created_at DESC");
|
||||
if (!resourcesRes) {
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import MountUserAgent from "../../Plugs/Middleware/MountUserAgent";
|
||||
import MountIP from "../../Plugs/Middleware/MountIP";
|
||||
import CheckCaptchaPassed from "../../Plugs/Middleware/CheckCaptchaPassed";
|
||||
import config from "../../config";
|
||||
import jwt from 'jsonwebtoken'
|
||||
import crypto from 'crypto'
|
||||
import { User } from "@/Types/Schema";
|
||||
|
||||
// 登录
|
||||
class Login extends API {
|
||||
constructor() {
|
||||
super('POST', '/console/login', CheckCaptchaPassed, MountUserAgent, MountIP);
|
||||
super('POST', '/console/login', MountUserAgent, MountIP);
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
@@ -21,28 +21,28 @@ class Login extends API {
|
||||
}
|
||||
|
||||
// 检查用户是否存在
|
||||
let userInfoRes = await MySQLConnection.execute('SELECT * FROM user WHERE username = ?', [username]);
|
||||
if(!userInfoRes){
|
||||
let userInfoRes = await Database.query<User>('SELECT * FROM user WHERE username = $1', [username]);
|
||||
if (!userInfoRes) {
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
if (userInfoRes.length != 1) {
|
||||
return res.json(ServerStdResponse.USER.NOTFOUND);
|
||||
}
|
||||
userInfoRes = userInfoRes[0];
|
||||
const UserInfo = userInfoRes[0];
|
||||
// 检查密码是否正确
|
||||
if(crypto.createHash('sha256').update(`${userInfoRes.salt}${password}`).digest('hex') != userInfoRes.password){
|
||||
if (crypto.createHash('sha256').update(`${UserInfo.salt}${password}`).digest('hex') != UserInfo.password) {
|
||||
return res.json(ServerStdResponse.USER.PASSWORD_ERROR);
|
||||
}
|
||||
|
||||
// 准备jwtToken
|
||||
const jwtPayload = {
|
||||
uuid: userInfoRes.uuid,
|
||||
uuid: UserInfo.uuid,
|
||||
loginTime: Date.now()
|
||||
}
|
||||
let jwtToken = jwt.sign(jwtPayload, config.jwt.secret, { expiresIn: config.jwt.expiresIn });
|
||||
|
||||
// 写入登录日志
|
||||
MySQLConnection.execute('INSERT INTO user_login_log (user_uuid, ip, user_agent, time) VALUES (?,?,?,?)', [userInfoRes.uuid, _ip, _userAgent, Date.now()]);
|
||||
Database.query('INSERT INTO user_login_log (user_uuid, ip, user_agent, time) VALUES ($1,$2,$3,$4)', [UserInfo.uuid, _ip, _userAgent, Date.now()]);
|
||||
return res.json({ ...ServerStdResponse.OK, data: { token: jwtToken } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
import crypto from 'crypto'
|
||||
import { Blog } from "@/Types/Schema";
|
||||
|
||||
// 保存博客
|
||||
class SaveBlog extends API {
|
||||
@@ -11,21 +12,21 @@ class SaveBlog extends API {
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
let { id, uuid, title, description, publish_time, src, access_level } = data;
|
||||
if (!title || !description || !publish_time || !src || !access_level) {
|
||||
let { uuid, title, description, created_at, src, access_level } = data;
|
||||
if (!title || !description || !created_at || !src || !access_level) {
|
||||
return res.json(ServerStdResponse.PARAMS_MISSING);
|
||||
}
|
||||
let execRes: any;
|
||||
if (id) {
|
||||
if (uuid) {
|
||||
// 保存
|
||||
execRes = await MySQLConnection.execute('UPDATE blog SET title = ?, description = ?, publish_time = ?, src = ?, access_level = ? WHERE `id` = ?', [title, description, publish_time, src, access_level, id]);
|
||||
execRes = await Database.query<Blog>('UPDATE blog SET title = $1, description = $2, created_at = $3, src = $4, access_level = $5 WHERE uuid = $6', [title, description, created_at, src, access_level, uuid]);
|
||||
} else {
|
||||
// 新建
|
||||
const uuid = crypto.createHash('md5').update(`${Math.random()}${Date.now()}`).digest('hex');
|
||||
execRes = await MySQLConnection.execute('INSERT INTO blog (uuid, title, description, src, publish_time, access_level, visit_count, like_count) VALUES (?,?,?,?,?,?,?,?)', [uuid, title, description, src, publish_time, access_level, 0, 0]);
|
||||
execRes = await Database.query<Blog>('INSERT INTO blog (uuid, title, description, src, created_at, access_level, visit_count, like_count) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)', [uuid, title, description, src, created_at, access_level, 0, 0]);
|
||||
}
|
||||
|
||||
if (!execRes || execRes.affectedRows != 1) {
|
||||
if (!execRes) {
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
return res.json({ ...ServerStdResponse.OK });
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
import { Resource } from "@/Types/Schema";
|
||||
import Crypto from 'crypto'
|
||||
|
||||
// 保存资源
|
||||
class SaveResource extends API {
|
||||
@@ -10,20 +12,21 @@ class SaveResource extends API {
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
let { id, type, recommand, title, describe, icon_src, addition, src } = data;
|
||||
let { uuid, type, recommand, title, describe, icon_src, addition, src } = data;
|
||||
if (!type || !recommand || !title || !describe || !icon_src || !addition || !src) {
|
||||
return res.json(ServerStdResponse.PARAMS_MISSING);
|
||||
}
|
||||
let execRes: any;
|
||||
if (id) {
|
||||
if (uuid) {
|
||||
// 保存
|
||||
execRes = await MySQLConnection.execute('UPDATE resource SET `type` = ?, `recommand` = ?, `title` = ?, `describe` = ?, `addition` = ?, `icon_src` = ?, `src` = ? WHERE `id` = ?', [type, recommand, title, describe, addition, icon_src, src, id]);
|
||||
execRes = await Database.query<Resource>('UPDATE resource SET "type" = $1, "recommand" = $2, "title" = $3, "describe" = $4, "addition" = $5, "icon_src" = $6, "src" = $7 WHERE "uuid" = $8', [type, recommand, title, describe, addition, icon_src, src, uuid]);
|
||||
} else {
|
||||
// 新建
|
||||
execRes = await MySQLConnection.execute('INSERT INTO resource (`type`, `recommand`, `title`, `describe`, `addition`, `icon_src`, `src`) VALUES (?,?,?,?,?,?,?)', [type, recommand, title, describe, addition, icon_src, src]);
|
||||
uuid = Crypto.createHash('md5').update(`${Math.random()}${Date.now()}`).digest('hex');
|
||||
execRes = await Database.query<Resource>('INSERT INTO resource ("uuid","type", "recommand", "title", "describe", "addition", "icon_src", "src", "created_at") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)', [uuid, type, recommand, title, describe, addition, icon_src, src, new Date()]);
|
||||
}
|
||||
|
||||
if (!execRes || execRes.affectedRows != 1) {
|
||||
if (!execRes) {
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
return res.json({ ...ServerStdResponse.OK });
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import Database from '../../Plugs/Database'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
import crypto from 'crypto'
|
||||
import { Blog } from "@/Types/Schema";
|
||||
|
||||
// 设置博客密码
|
||||
class SetBlogPasswd extends API {
|
||||
@@ -16,7 +17,7 @@ class SetBlogPasswd extends API {
|
||||
return res.json(ServerStdResponse.PARAMS_MISSING);
|
||||
}
|
||||
const encrypt_p = crypto.createHash('sha256').update(passwd).digest('hex');
|
||||
MySQLConnection.execute('UPDATE blog SET encrypt_p = ? WHERE uuid = ?', [encrypt_p, uuid]);
|
||||
Database.query<Blog>('UPDATE blog SET encrypt_p = $1 WHERE uuid = $2', [encrypt_p, uuid]);
|
||||
return res.json({ ...ServerStdResponse.OK });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user