init
This commit is contained in:
49
api/controller_admin/sys_file.js
Normal file
49
api/controller_admin/sys_file.js
Normal file
@@ -0,0 +1,49 @@
|
||||
var UUID = require("uuid");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
const ossTool = require("../service/ossTool");
|
||||
const funTool = require("../../tool/funTool");
|
||||
|
||||
module.exports = {
|
||||
"POST /sys_file/upload_img": async (ctx, next) => {
|
||||
const files = ctx.request.files; // 获取上传文件
|
||||
let fileArray = [];
|
||||
let rootPath = path.join(__dirname, "../../upload/imgs");
|
||||
for (var key in files) {
|
||||
fileArray.push(files[key]);
|
||||
}
|
||||
|
||||
//创建文件夹
|
||||
await funTool.mkdirsSync(rootPath);
|
||||
|
||||
let resArray = [];
|
||||
fileArray.forEach((file) => {
|
||||
// 创建可读流
|
||||
const reader = fs.createReadStream(file.path);
|
||||
|
||||
let filePath = `/${UUID.v1() + "_" + file.name}`;
|
||||
// 创建可写流
|
||||
const upStream = fs.createWriteStream(path.join(rootPath, filePath));
|
||||
// 可读流通过管道写入可写流
|
||||
|
||||
reader.pipe(upStream);
|
||||
|
||||
resArray.push({ name: file.name, path: path.join("/imgs", filePath) });
|
||||
});
|
||||
|
||||
ctx.success(resArray);
|
||||
},
|
||||
"POST /sys_file/upload_oos_img": async (ctx, next) => {
|
||||
let fileArray = [];
|
||||
const files = ctx.request.files; // 获取上传文件
|
||||
for (var key in files) {
|
||||
fileArray.push(files[key]);
|
||||
}
|
||||
let data = await ossTool.putImg(fileArray[0]);
|
||||
if (data.path) {
|
||||
return ctx.success(data);
|
||||
} else {
|
||||
return ctx.fail();
|
||||
}
|
||||
},
|
||||
};
|
||||
75
api/controller_admin/sys_parameter.js
Normal file
75
api/controller_admin/sys_parameter.js
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* 后台字典/系统参数管理
|
||||
* Banner 配置存于 sys_parameter,key:bannerListImage、bannerDetailImage、bannerListIndex
|
||||
*/
|
||||
const { sys_parameter, op } = require('../../middleware/baseModel');
|
||||
|
||||
const BANNER_KEYS = ['bannerListImage', 'bannerDetailImage', 'bannerListIndex'];
|
||||
|
||||
module.exports = {
|
||||
|
||||
// all 接口
|
||||
'GET /sys_parameter/all': async (ctx, next) => {
|
||||
const rows = await sys_parameter.findAll({
|
||||
where: {},
|
||||
attributes: ['key', 'value']
|
||||
});
|
||||
return ctx.success(rows);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 获取 Banner 配置(三个 key 的 value)
|
||||
*/
|
||||
'GET /sys_parameter/banner_config': async (ctx, next) => {
|
||||
const rows = await sys_parameter.findAll({
|
||||
where: {
|
||||
key: { [op.in]: ['bannerListImage', 'bannerDetailImage', 'bannerListIndex'] }
|
||||
},
|
||||
attributes: ['key', 'value']
|
||||
});
|
||||
const data = {
|
||||
bannerListImage: '',
|
||||
bannerDetailImage: '',
|
||||
bannerListIndex: '0'
|
||||
};
|
||||
if (rows && rows.length) {
|
||||
rows.forEach((row) => {
|
||||
const key = row.key;
|
||||
if (BANNER_KEYS.includes(key)) {
|
||||
data[key] = row.value != null ? String(row.value).replace(/\n/g, '') : '';
|
||||
}
|
||||
});
|
||||
}
|
||||
return ctx.success(data);
|
||||
},
|
||||
|
||||
/**
|
||||
* 保存 Banner 配置
|
||||
* body: { bannerListImage?, bannerDetailImage?, bannerListIndex? }
|
||||
*/
|
||||
'POST /sys_parameter/banner_config': async (ctx, next) => {
|
||||
const body = ctx.getBody() || {};
|
||||
const updates = [
|
||||
{ key: 'bannerListImage', value: body.bannerListImage != null ? String(body.bannerListImage).trim() : '' },
|
||||
{ key: 'bannerDetailImage', value: body.bannerDetailImage != null ? String(body.bannerDetailImage).trim() : '' },
|
||||
{ key: 'bannerListIndex', value: body.bannerListIndex != null ? String(body.bannerListIndex).trim() : '0' }
|
||||
];
|
||||
|
||||
for (const item of updates) {
|
||||
let row = await sys_parameter.findOne({ where: { key: item.key } });
|
||||
if (row) {
|
||||
await row.update({ value: item.value });
|
||||
} else {
|
||||
await sys_parameter.create({
|
||||
key: item.key,
|
||||
value: item.value,
|
||||
remark: item.key === 'bannerListIndex' ? '列表下标或关闭(0关闭)' : 'Banner图片URL',
|
||||
is_modified: 0
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.success({}, '保存成功');
|
||||
}
|
||||
};
|
||||
61
api/controller_admin/sys_tenant.js
Normal file
61
api/controller_admin/sys_tenant.js
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 系统租户 sys_tenant:表单下拉(平台租户可看全部启用租户,否则仅本租户)
|
||||
*/
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { getAdminTenantContext } = require("../service/tenant_scope");
|
||||
|
||||
module.exports = {
|
||||
"GET /sys_tenant/form_options": async (ctx) => {
|
||||
const sys_tenant = baseModel.sys_tenant;
|
||||
if (!sys_tenant) {
|
||||
return ctx.fail("sys_tenant 模型未加载");
|
||||
}
|
||||
const { tenantId: user_tenant_id, userId } = await getAdminTenantContext(ctx);
|
||||
const uid = userId != null && userId !== "" ? Number(userId) : null;
|
||||
const has_user = Number.isFinite(uid) && uid > 0;
|
||||
if (!has_user) {
|
||||
return ctx.fail("未登录或登录已失效");
|
||||
}
|
||||
const tid = user_tenant_id != null && user_tenant_id !== "" ? Number(user_tenant_id) : null;
|
||||
const user_tenant_ok = Number.isFinite(tid) && tid > 0;
|
||||
|
||||
let is_platform = false;
|
||||
if (user_tenant_ok) {
|
||||
const row = await sys_tenant.findByPk(tid, {
|
||||
attributes: ["id", "is_platform"],
|
||||
raw: true,
|
||||
});
|
||||
if (row && Number(row.is_platform) === 1) {
|
||||
is_platform = true;
|
||||
}
|
||||
}
|
||||
|
||||
let rows;
|
||||
if (is_platform) {
|
||||
rows = await sys_tenant.findAll({
|
||||
where: { status: 1 },
|
||||
order: [["id", "ASC"]],
|
||||
limit: 500,
|
||||
raw: true,
|
||||
});
|
||||
} else if (user_tenant_ok) {
|
||||
const one = await sys_tenant.findByPk(tid, { raw: true });
|
||||
rows = one ? [one] : [];
|
||||
} else {
|
||||
rows = [];
|
||||
}
|
||||
|
||||
const list = (rows || []).map((r) => {
|
||||
const id = Number(r.id);
|
||||
const name = r.name != null ? String(r.name).trim() : "";
|
||||
const plat = Number(r.is_platform) === 1;
|
||||
return {
|
||||
key: id,
|
||||
value: plat && name ? `${name}(平台)` : name || String(id),
|
||||
};
|
||||
});
|
||||
const locked = !is_platform;
|
||||
const defaultTenantId = user_tenant_ok ? tid : null;
|
||||
return ctx.success({ list, locked, defaultTenantId });
|
||||
},
|
||||
};
|
||||
114
api/controller_admin/tpl_demo.js
Normal file
114
api/controller_admin/tpl_demo.js
Normal file
@@ -0,0 +1,114 @@
|
||||
const { tpl_demo, op } = require("../../middleware/baseModel");
|
||||
|
||||
function build_search_where(seach_option) {
|
||||
const key = seach_option && seach_option.key;
|
||||
const raw = seach_option && seach_option.value;
|
||||
if (!key || raw === undefined || raw === null) {
|
||||
return {};
|
||||
}
|
||||
const str = String(raw).trim();
|
||||
if (str === "") {
|
||||
return {};
|
||||
}
|
||||
const attr = tpl_demo.rawAttributes[key];
|
||||
if (!attr) {
|
||||
return { [key]: { [op.like]: `%${str}%` } };
|
||||
}
|
||||
const type_key = attr.type && attr.type.key;
|
||||
if (type_key === "INTEGER" || type_key === "BIGINT") {
|
||||
const n = Number(str);
|
||||
if (!Number.isNaN(n)) {
|
||||
return { [key]: n };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
return { [key]: { [op.like]: `%${str}%` } };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
"POST /tpl_demo/page": async (ctx) => {
|
||||
const body = ctx.getBody() || {};
|
||||
const param = body.param || body;
|
||||
const { limit, offset } = ctx.getPageSize();
|
||||
const base_where = { is_delete: 0 };
|
||||
const search_where = build_search_where(param.seachOption || {});
|
||||
const where = { ...base_where, ...search_where };
|
||||
const { count, rows } = await tpl_demo.findAndCountAll({
|
||||
where,
|
||||
offset,
|
||||
limit,
|
||||
order: [["id", "DESC"]],
|
||||
});
|
||||
return ctx.success({ rows, count });
|
||||
},
|
||||
|
||||
"POST /tpl_demo/add": async (ctx) => {
|
||||
const body = ctx.getBody() || {};
|
||||
const title = body.title != null ? String(body.title).trim() : "";
|
||||
if (!title) {
|
||||
return ctx.fail("请填写标题");
|
||||
}
|
||||
const row = await tpl_demo.create({
|
||||
title,
|
||||
remark: body.remark != null ? String(body.remark).trim() : "",
|
||||
});
|
||||
return ctx.success(row);
|
||||
},
|
||||
|
||||
"POST /tpl_demo/edit": async (ctx) => {
|
||||
const body = ctx.getBody() || {};
|
||||
const id = body.id;
|
||||
if (!id) {
|
||||
return ctx.fail("缺少 id");
|
||||
}
|
||||
const title = body.title != null ? String(body.title).trim() : "";
|
||||
if (!title) {
|
||||
return ctx.fail("请填写标题");
|
||||
}
|
||||
await tpl_demo.update(
|
||||
{
|
||||
title,
|
||||
remark: body.remark != null ? String(body.remark).trim() : "",
|
||||
last_modify_time: new Date(),
|
||||
},
|
||||
{ where: { id, is_delete: 0 } }
|
||||
);
|
||||
return ctx.success({});
|
||||
},
|
||||
|
||||
"POST /tpl_demo/del": async (ctx) => {
|
||||
const body = ctx.getBody() || {};
|
||||
const id = body.id;
|
||||
if (!id) {
|
||||
return ctx.fail("缺少 id");
|
||||
}
|
||||
await tpl_demo.update(
|
||||
{ is_delete: 1, last_modify_time: new Date() },
|
||||
{ where: { id, is_delete: 0 } }
|
||||
);
|
||||
return ctx.success({});
|
||||
},
|
||||
|
||||
"GET /tpl_demo/detail": async (ctx) => {
|
||||
const id = ctx.get("id");
|
||||
if (!id) {
|
||||
return ctx.fail("缺少 id");
|
||||
}
|
||||
const row = await tpl_demo.findOne({ where: { id, is_delete: 0 } });
|
||||
return ctx.success(row);
|
||||
},
|
||||
|
||||
"GET /tpl_demo/all": async (ctx) => {
|
||||
const rows = await tpl_demo.findAll({ limit: 500, order: [["id", "DESC"]] });
|
||||
return ctx.success(rows);
|
||||
},
|
||||
|
||||
"POST /tpl_demo/export": async (ctx) => {
|
||||
const body = ctx.getBody() || {};
|
||||
const param = body.param || body;
|
||||
const search_where = build_search_where(param.seachOption || {});
|
||||
const where = { is_delete: 0, ...search_where };
|
||||
const rows = await tpl_demo.findAll({ where, limit: 10000, order: [["id", "DESC"]] });
|
||||
return ctx.success({ rows });
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user