后端搭建起来
This commit is contained in:
12
server/app/controller/custom.js
Normal file
12
server/app/controller/custom.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const Controller = require("egg").Controller
|
||||
|
||||
class Custom extends Controller {
|
||||
async list() {
|
||||
const {ctx: {query}} = this
|
||||
const result = await this.service.db.list("node_custom_config", query)
|
||||
this.ctx.status = 200
|
||||
this.ctx.body = result
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Custom
|
||||
@@ -1,9 +0,0 @@
|
||||
const Controller = require('egg').Controller;
|
||||
|
||||
class HomeController extends Controller {
|
||||
async index() {
|
||||
this.ctx.body = 'Hello world';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HomeController;
|
||||
19
server/app/middleware/errorHandler.js
Normal file
19
server/app/middleware/errorHandler.js
Normal file
@@ -0,0 +1,19 @@
|
||||
module.exports = () => async function errorHandler(ctx, next) {
|
||||
try {
|
||||
await next();
|
||||
} catch (err) {
|
||||
ctx.app.emit('error', err, ctx);
|
||||
const status = err.status || 500;
|
||||
// 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
|
||||
const error =
|
||||
status === 500 && ctx.app.config.env === 'prod'
|
||||
? 'Internal Server Error'
|
||||
: err.message;
|
||||
// 从 error 对象上读出各个属性,设置到响应中
|
||||
ctx.body = {error};
|
||||
if (status === 422) {
|
||||
ctx.body.detail = err.errors;
|
||||
}
|
||||
ctx.status = status;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,11 @@
|
||||
module.exports = (app) => {
|
||||
const { router, controller } = app;
|
||||
router.get('/', controller.home.index);
|
||||
const {router, controller} = app;
|
||||
console.log('启动接口...')
|
||||
for (const file in controller) {
|
||||
for (const item in controller[file]) {
|
||||
console.log(`初始化接口:/${file}/${item}`)
|
||||
router.post(`/api/${file}/${item}`, controller[file][item])
|
||||
}
|
||||
}
|
||||
console.log("接口初始化完毕")
|
||||
};
|
||||
|
||||
@@ -1,30 +1,37 @@
|
||||
const Service = require("egg").Service
|
||||
const {v4: uuid} = require("uuid");
|
||||
|
||||
class DbService extends Service {
|
||||
async addOrUpdate(table, form) {
|
||||
let result
|
||||
if (!form.id) {//创建
|
||||
form.id = uuid()
|
||||
result = await this.app.mysql.insert(table, form)
|
||||
} else {//更新
|
||||
result = await this.app.mysql.update(table, form)
|
||||
}
|
||||
return result === 1
|
||||
return result === 1 ? {code: 0, data: form.id} : {code: 1, data: null}
|
||||
}
|
||||
|
||||
async delete(table, id) {
|
||||
await this.app.mysql.delete(table, {id})
|
||||
return {code: 0, data: null}
|
||||
}
|
||||
|
||||
async detail(table, id) {
|
||||
return await this.app.mysql.get(table, {id})
|
||||
const data = await this.app.mysql.get(table, {id})
|
||||
return {code: 0, data}
|
||||
}
|
||||
|
||||
async list(table, params = {}) {
|
||||
return await this.app.mysql.select(table, {
|
||||
let {current = 0} = params
|
||||
const records = await this.app.mysql.select(table, {
|
||||
where: params,
|
||||
limit: params?.size || 10, // 返回数据量
|
||||
offset: Math.max(params?.current - 1, 0), // 数据偏移量
|
||||
offset: Math.max(--current, 0), // 数据偏移量
|
||||
})
|
||||
const total = (await this.app.mysql.select(table, {where: params}))?.length || 0
|
||||
return {code: 0, data: {records, total}}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user