后端搭建起来

This commit is contained in:
aixianling
2022-12-30 16:03:32 +08:00
parent cf5f60a049
commit b8ac23dcfd
8 changed files with 85 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
module.exports = app => {
app.beforeStart(async () => {
const mysqlConfig = await app.configCenter.fetch("mysql")
app.database = app.mysql.createInstance(mysqlConfig)
})
module.exports = () => {
// app.beforeStart(async () => {
// console.log(app.config)
// const mysqlConfig = await app.configCenter.fetch("mysql")
// app.database = app.mysql.createInstance(mysqlConfig)
// })
}

View 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

View File

@@ -1,9 +0,0 @@
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
this.ctx.body = 'Hello world';
}
}
module.exports = HomeController;

View 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;
}
}

View File

@@ -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("接口初始化完毕")
};

View File

@@ -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}}
}
}

View File

@@ -1,19 +1,28 @@
exports.keys = "kubbo and flora"
exports.mysql = {
client: {
// host
host: '192.168.1.87',
// 端口号
port: '3306',
// 用户名
user: 'root',
// 密码
password: 'Cwy@2019',
// 数据库名
database: 'dvcp_oms_dev',
module.exports = {
keys: "kubbo&flora",
middleware: ['errorHandler'],
errorHandler: {match: '/api'},
mysql: {
client: {
// host
host: '192.168.1.87',
// 端口号
port: '3306',
// 用户名
user: 'root',
// 密码
password: 'Cwy@2019',
// 数据库名
database: 'dvcp_oms_dev',
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
security: {
csrf: {
enable: false
}
}
}

View File

@@ -13,7 +13,8 @@
"license": "ISC",
"dependencies": {
"egg": "^3.9.2",
"egg-mysql": "^3.3.0"
"egg-mysql": "^3.3.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"egg-bin": "^5.9.0"