465 lines
10 KiB
JavaScript
465 lines
10 KiB
JavaScript
var F = Object.defineProperty;
|
|
var j = (o, t, a) => t in o ? F(o, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : o[t] = a;
|
|
var d = (o, t, a) => j(o, typeof t != "symbol" ? t + "" : t, a);
|
|
import f from "fs";
|
|
import v from "dayjs";
|
|
import R from "lodash";
|
|
import H from "cuid";
|
|
import { dirname as q, join as w } from "path";
|
|
import { JSONFile as m, Low as D } from "lowdb";
|
|
import { fileURLToPath as U } from "url";
|
|
import x from "yaml";
|
|
class y {
|
|
static initConfig() {
|
|
this.config = x.parse(f.readFileSync("./config.yml").toString());
|
|
}
|
|
}
|
|
d(y, "config", null);
|
|
const b = q(U(
|
|
import.meta.url
|
|
)), h = (o, t) => {
|
|
let a = "";
|
|
return y.config && y.config.db && y.config.db[o] ? a = y.config.path + y.config.db[o] : a = t, a;
|
|
}, E = async () => {
|
|
let o = h("db", w(b, "db.json"));
|
|
const t = new m(o), a = new D(t);
|
|
return await a.read(), a;
|
|
}, N = async () => {
|
|
let o = h("resourceDB", w(b, "resourceDB.json"));
|
|
const t = new m(o), a = new D(t);
|
|
return await a.read(), a;
|
|
}, P = async () => {
|
|
let o = h("backupDB", w(b, "backupDB.json"));
|
|
const t = new m(o), a = new D(t);
|
|
return await a.read(), a;
|
|
}, _ = async () => {
|
|
let o = h("logDB", w(b, "logDB.json"));
|
|
const t = new m(o), a = new D(t);
|
|
return await a.read(), a;
|
|
};
|
|
class g {
|
|
static async init() {
|
|
this.baseDB = await E(), this.resourceDB = await N(), this.backupDB = await P(), this.logDB = await _();
|
|
}
|
|
static getBackupDb() {
|
|
return this.backupDB;
|
|
}
|
|
static getLogDb() {
|
|
return this.logDB;
|
|
}
|
|
}
|
|
d(g, "baseDB", null), d(g, "resourceDB", null), d(g, "backupDB", null), d(g, "logDB", null);
|
|
class k {
|
|
static async addDataLog({
|
|
tableName: t,
|
|
type: a,
|
|
data: i
|
|
}) {
|
|
const e = await g.getLogDb();
|
|
return p.create({
|
|
db: e,
|
|
name: "dataLog",
|
|
data: {
|
|
dataId: i.id,
|
|
type: a,
|
|
tableName: t,
|
|
data: i,
|
|
// 待生效
|
|
status: 0,
|
|
createTime: /* @__PURE__ */ new Date()
|
|
}
|
|
});
|
|
}
|
|
static async modifyDataLogStatus({
|
|
id: t
|
|
}) {
|
|
const a = await g.getLogDb(), {
|
|
data: i,
|
|
code: e
|
|
} = await p.retrieveSingle({
|
|
db: a,
|
|
name: "dataLog",
|
|
params: {
|
|
id: t
|
|
}
|
|
});
|
|
e == 0 && await p.update({
|
|
db: a,
|
|
name: "dataLog",
|
|
data: {
|
|
...i,
|
|
// 生效
|
|
status: 1
|
|
}
|
|
});
|
|
}
|
|
}
|
|
class L {
|
|
constructor({
|
|
name: t,
|
|
type: a
|
|
}) {
|
|
d(this, "name", "");
|
|
d(this, "type", "");
|
|
d(this, "id", null);
|
|
this.name = t, this.type = a;
|
|
}
|
|
async addDataLog({
|
|
data: t
|
|
}) {
|
|
const {
|
|
name: a,
|
|
type: i
|
|
} = this;
|
|
if (a != "dataLog") {
|
|
const {
|
|
data: e
|
|
} = await k.addDataLog({
|
|
tableName: a,
|
|
type: i,
|
|
data: t
|
|
});
|
|
this.id = e.id;
|
|
}
|
|
}
|
|
async modifyDataLogStatus() {
|
|
const {
|
|
name: t,
|
|
id: a
|
|
} = this;
|
|
t != "dataLog" && a && await k.modifyDataLogStatus({
|
|
id: a
|
|
});
|
|
}
|
|
}
|
|
class p {
|
|
/**
|
|
* 读取列表数据
|
|
* @param {*} param0
|
|
* @returns
|
|
*/
|
|
static async retrieveList({
|
|
db: t,
|
|
name: a,
|
|
params: i
|
|
}) {
|
|
await t.read();
|
|
const e = t.data[a].filter((r) => {
|
|
let u = !0;
|
|
for (let c in i)
|
|
if (!["page", "pageSize"].includes(c) && (typeof r[c] == "string" ? u = r[c].indexOf(i[c]) > -1 : typeof r[c] == "number" ? u = r[c] == i[c] : r[c] instanceof Array && (u = r[c].includes(i[c])), !u))
|
|
break;
|
|
return u;
|
|
}), {
|
|
page: s,
|
|
pageSize: n
|
|
} = i;
|
|
let l = e;
|
|
return s && n && (l = e.slice((s - 1) * n, s * n)), {
|
|
code: 0,
|
|
data: l.sort((r, u) => r.sort - u.sort) || [],
|
|
total: e.length
|
|
};
|
|
}
|
|
/**
|
|
* 读取单条数据
|
|
* @param {*} param0
|
|
*/
|
|
static async retrieveSingle({
|
|
db: t,
|
|
name: a,
|
|
params: i
|
|
}) {
|
|
await t.read();
|
|
const e = t.data[a], s = i;
|
|
let n = {};
|
|
return s.id && e && (n = e.find((l) => l.id == s.id)), {
|
|
code: 0,
|
|
data: n || {}
|
|
};
|
|
}
|
|
/**
|
|
* 修改数据
|
|
* @param {*} param0
|
|
*/
|
|
static async update({
|
|
db: t,
|
|
name: a,
|
|
data: i
|
|
}) {
|
|
await t.read(), t.data[a] || (t.data[a] = []);
|
|
const e = t.data[a], s = i.id;
|
|
if (s && e) {
|
|
const n = e.find((r) => r.id == s);
|
|
n && R.merge(n, i);
|
|
const l = new L({
|
|
name: a,
|
|
type: "upd"
|
|
});
|
|
await l.addDataLog({
|
|
data: n
|
|
}), await t.write(), await l.modifyDataLogStatus();
|
|
}
|
|
return {
|
|
code: 0
|
|
};
|
|
}
|
|
/**
|
|
* 创建数据
|
|
* @param {*} param0
|
|
*/
|
|
static async create({
|
|
db: t,
|
|
name: a,
|
|
data: i
|
|
}) {
|
|
await t.read(), t.data[a] || (t.data[a] = []);
|
|
const e = t.data[a];
|
|
i.id = H(), i.createTime = /* @__PURE__ */ new Date(), e.push(i);
|
|
const s = new L({
|
|
name: a,
|
|
type: "add"
|
|
});
|
|
return await s.addDataLog({
|
|
data: i
|
|
}), await t.write(), await s.modifyDataLogStatus(), {
|
|
code: 0,
|
|
data: i
|
|
};
|
|
}
|
|
/**
|
|
* 删除数据
|
|
* @param {*} param0
|
|
* @returns
|
|
*/
|
|
static async delete({
|
|
db: t,
|
|
name: a,
|
|
data: i
|
|
}) {
|
|
await t.read(), t.data[a] || (t.data[a] = []);
|
|
const e = t.data[a], s = i.ids;
|
|
if (s.length > 0 && e) {
|
|
let n = null;
|
|
const l = [];
|
|
for (let u = 0; u < s.length; u++) {
|
|
n = s[u];
|
|
const c = e.findIndex((S) => S.id == n);
|
|
c != -1 && (l.push(e[c]), e.splice(c, 1));
|
|
}
|
|
const r = new L({
|
|
name: a,
|
|
type: "del"
|
|
});
|
|
await r.addDataLog({
|
|
data: l
|
|
}), await t.write(), await r.modifyDataLogStatus();
|
|
}
|
|
return {
|
|
code: 0
|
|
};
|
|
}
|
|
}
|
|
const B = {};
|
|
class $ {
|
|
constructor() {
|
|
d(this, "addBackUPRecord", async (t) => p.create({
|
|
db: await g.getBackupDb(),
|
|
name: "backupHistory",
|
|
data: t
|
|
}));
|
|
d(this, "updateBackUPRecord", async (t) => p.update({
|
|
db: await g.getBackupDb(),
|
|
name: "backupHistory",
|
|
data: t
|
|
}));
|
|
}
|
|
/**
|
|
* 获取文件名称 fileName.ext
|
|
* @param {*} path
|
|
* @returns
|
|
*/
|
|
getFileName(t) {
|
|
const a = t.split("/");
|
|
return a.length >= 2 ? a.reverse()[0] : t;
|
|
}
|
|
/**
|
|
* 获取文件后缀和文件名称
|
|
* @param {*} path
|
|
* @returns
|
|
*/
|
|
getFileExt(t) {
|
|
let a = t.split(".");
|
|
return a.length >= 2 ? (a = a.reverse(), {
|
|
name: a[1],
|
|
ext: "." + a[0]
|
|
}) : {
|
|
name: t,
|
|
ext: ""
|
|
};
|
|
}
|
|
/**
|
|
* 创建文件夹(指定文件夹路径不存在时才创建)
|
|
* @param {*} filePath
|
|
*/
|
|
mkDir(t) {
|
|
f.existsSync(t) || f.mkdirSync(t);
|
|
}
|
|
// backupToBaiduYunPan = () => {
|
|
// let cmd = 'python';
|
|
// let args = ('F:\\sourceCode\\2021\\test-baiduPan\\python\\getInfo.py').split(' ');
|
|
// let task = child_process.spawnSync(cmd, args);
|
|
// console.log("用户信息:", task.stdout.toString());
|
|
// }
|
|
/**
|
|
* 根据配置备份文件
|
|
* @param {*} data:[{original:string,dest:string}]
|
|
*/
|
|
async bakFile(t) {
|
|
for (const a of t || [])
|
|
if (f.existsSync(a.original)) {
|
|
this.mkDir(a.dest);
|
|
const e = this.getFileName(a.original), s = this.getFileExt(e), n = v().format("YYYYMMDDHHmmss"), l = s.name + n + s.ext, {
|
|
data: r,
|
|
code: u
|
|
} = await this.addBackUPRecord({
|
|
name: l,
|
|
category: a.category
|
|
});
|
|
u == 0 && (f.writeFileSync(a.dest + "/" + l, f.readFileSync(a.original)), await this.updateBackUPRecord({
|
|
id: r.id,
|
|
status: "1"
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
const G = () => {
|
|
let o = "pm2", t = "start computer-cms-server".split(" "), a = B.spawnSync(o, t);
|
|
console.log("pm2 log:", a);
|
|
}, Y = () => {
|
|
let o = "pm2", t = "stop computer-cms-server".split(" "), a = B.spawnSync(o, t);
|
|
console.log("pm2 log:", a);
|
|
}, A = new $();
|
|
(async () => (y.initConfig(), await g.init(), Y(), await A.bakFile(y.config.dbBak), G()))();
|
|
class Q {
|
|
constructor({
|
|
router: t,
|
|
db: a,
|
|
name: i
|
|
}) {
|
|
d(this, "name", "");
|
|
d(this, "db", null);
|
|
d(this, "router", null);
|
|
this.router = t, this.db = a, this.name = i;
|
|
}
|
|
/**
|
|
* 创建列表数据路由
|
|
*/
|
|
createGetList() {
|
|
const {
|
|
name: t,
|
|
db: a,
|
|
router: i
|
|
} = this;
|
|
i.post(`/api/${t}/list`, async (e) => {
|
|
e.type = "application/json";
|
|
const s = e.request.body, n = await p.retrieveList({
|
|
db: a,
|
|
name: t,
|
|
params: s
|
|
});
|
|
e.body = n;
|
|
});
|
|
}
|
|
/**
|
|
* 创建详情路由
|
|
*/
|
|
createGetDetail() {
|
|
const {
|
|
name: t,
|
|
db: a,
|
|
router: i
|
|
} = this;
|
|
i.get(`/api/${t}`, async (e) => {
|
|
e.type = "application/json";
|
|
const s = e.request.query, n = await p.retrieveSingle({
|
|
db: a,
|
|
name: t,
|
|
params: s
|
|
});
|
|
e.body = n;
|
|
});
|
|
}
|
|
/**
|
|
* 修改数据
|
|
*/
|
|
createUpdate() {
|
|
const {
|
|
name: t,
|
|
db: a,
|
|
router: i
|
|
} = this;
|
|
i.post(`/api/${t}/upd`, async (e) => {
|
|
e.type = "application/json";
|
|
const s = e.request.body, n = await p.update({
|
|
db: a,
|
|
name: t,
|
|
data: s
|
|
});
|
|
e.body = n;
|
|
});
|
|
}
|
|
/**
|
|
* 新增数据
|
|
*/
|
|
createAdd() {
|
|
const {
|
|
name: t,
|
|
db: a,
|
|
router: i
|
|
} = this;
|
|
i.post(`/api/${t}/add`, async (e) => {
|
|
e.type = "application/json";
|
|
const s = e.request.body, n = await p.create({
|
|
db: a,
|
|
name: t,
|
|
data: s
|
|
});
|
|
e.body = n;
|
|
});
|
|
}
|
|
/**
|
|
* 删除数据
|
|
*/
|
|
createDelete() {
|
|
const {
|
|
name: t,
|
|
db: a,
|
|
router: i
|
|
} = this;
|
|
i.post(`/api/${t}/delete`, async (e) => {
|
|
e.type = "application/json";
|
|
const s = e.request.body, n = await p.delete({
|
|
db: a,
|
|
name: t,
|
|
data: s
|
|
});
|
|
e.body = n;
|
|
});
|
|
}
|
|
creaetBaseRouter() {
|
|
this.createGetList(), this.createGetDetail(), this.createAdd(), this.createUpdate(), this.createDelete();
|
|
}
|
|
}
|
|
export {
|
|
$ as BackupHelper,
|
|
p as CURDHelper,
|
|
y as ConfigHelper,
|
|
k as LogHelper,
|
|
Q as RouterFactory,
|
|
P as getBackupDb,
|
|
E as getDb,
|
|
_ as getLogDb,
|
|
N as getResourceDb
|
|
};
|