This commit is contained in:
robin
2022-04-12 00:51:40 +08:00
parent c2f8473fb5
commit 344562baf4
18 changed files with 940 additions and 39 deletions
+44 -2
View File
@@ -1,14 +1,56 @@
'use strict';
module.exports = {
doc : "",
doc : "默认页面",
path : "/",
method : ["get", "post"],
middleware: [],
params : {},
return : {},
controller: async (ctx, app) => {
// app.err.ParameterException()
app.event.emit('test.event2')
const where = {
// id: 4
}
const data = {
// id: 1,
uid: "77",
}
ctx.body = 'index,xe:' + app.utils.VERSION
// const User = await app.table("User").where(where).findAll()
// const User = await app.table("User").time(["2022-04-9", "2022-04-10"]).findAll()
// const User = await app.table("User").where(where).findOrCreate(data)
// const {count, rows} = await app.table("User").where(where).findAndCountAll()
// const User = await app.table("User").save(data)
// const res = {
// "创建单个数据" : await app.table("User").data({uid: "6666"}).save(),
// "查询单个数据" : await app.table("User").where({id: 2}).find(),
// "查询单个数据,不存在就创建" : await app.table("User").where({id: 999}).data({uid: "6666"}).findOrCreate(),
// "查询所有数据" : await app.table("User").where({uid: "6666"}).findAll(),
// "查询所有数据(时间区间)" : await app.table("User").time(['2022-04-09', '2022-04-10']).findAll(),
// "查询所有数据(当月数据)" : await app.table("User").time('month').findAll(),
// "查询所有数据(30分钟内的数据)": await app.table("User").time(30).findAll(),
// "查询所有数据(分页)" : await app.table("User").where({uid: "6666"}).page(1).limit(1).findAll(),
// "查询所有数据+总行数(分页)" : await app.table("User").where({uid: "6666"}).page(1).limit(1).findAndCountAll(),
// }
/*
await app.setTransaction() // 设置事务
await app.table("User").data({uid: "6666"}).save()
await app.table("User").where({id: 2}).find()
await app.table("User").data({uid: "6666"}).save()
await app.table("User").where({id: 2}).find()
await app.commitTransaction() //提交事务,如果设置了事务不提交,任务不会执行
*/
// await app.table("User").where({id: 1032}).data({age: 1}).setInc() // 字段值+1
// await app.table("User").where({id: 1032}).data({age: 2}).setInc() // 字段值+2
// const {res, value} = await app.table("User").where({id: 1032}).data({age: 2}).setDec(0) // 字段值-2,不能低于0,如果低于0返回false
// const {res, value} = await app.table("User").where({id: 1032}).data({age: 2}).setDec(0,10) // 字段值-2,不能低于0,如果低于0设置为10
const {res, value} = await app.table("User").where({id: 1032}).data({age: 2}).setInc(20, 10) // 字段值+2,不能大于20,如果大于20设置为10
ctx.body = {res, value}
}
}
+53
View File
@@ -0,0 +1,53 @@
class HttpException extends Error {
// message为异常信息,code 为错误码(开发人员内部约定),status 为HTTP状态码
constructor(message, code, status) {
super()
this.status = status || 500
this.code = code || 500
this.message = message || '服务器异常'
}
}
class ParameterException extends HttpException {
constructor(message, code, status) {
super()
this.status = status || 402
this.code = code
this.message = message || '参数错误'
}
}
class NotFound extends HttpException {
constructor(message, code, status) {
super()
this.status = status || 404
this.code = 404
this.message = message || '资源未找到'
}
}
class AuthFailed extends HttpException {
constructor(message, code, status) {
super()
this.status = status || 401
this.message = message || '授权失败'
this.code = 401
}
}
class Forbidden extends HttpException {
constructor(message, code, status) {
super()
this.status = status || 403
this.message = message || '禁止访问'
this.code = 403
}
}
module.exports = {
HttpException,
ParameterException,
NotFound,
AuthFailed,
Forbidden,
}
+10
View File
@@ -0,0 +1,10 @@
'use strict';
// 监听事件
module.exports = {
"event1": (args) => {
console.log(args || 'event1')
},
"event2": (args) => {
console.log(args || 'event2')
},
}
+4
View File
@@ -0,0 +1,4 @@
/*系统启动后要做初始化*/
module.exports = (app) => {
console.log("启动完成");
}
+12
View File
@@ -0,0 +1,12 @@
'use strict';
const error = require('koa-json-error')
// 错误处理
module.exports = error((err) => {
return {
status : err.status,
message : err.message,
code : err.code,
res : false,
// postFormat: (e, obj) => process.env.NODE_ENV === 'production' ? _.omit(obj, 'stack') : obj
}
})
-1
View File
@@ -5,7 +5,6 @@ module.exports = async (ctx, next, app) => {
// const timetaken = `${ctx.request.method}${ctx.request.url} 响应时间`
// console.time(timetaken)
// app.logger.debug("123")
throw new app.err.HttpException
await next()
// console.timeEnd(timetaken)
}
+8
View File
@@ -0,0 +1,8 @@
module.exports = {
doc : "用户",
model: {
uid: {type: "STRING", comment: '用户id'},
age: {type: "INTEGER", comment: '年龄', defaultValue: 0,},
}
}
+9
View File
@@ -0,0 +1,9 @@
'use strict';
// 监听事件
module.exports = {
time : "*/3 * * * * *",
run : true,//系统启动时立即执行一次
schedule: (app) => {
// console.log('每3秒我执行一次' + app.utils.VERSION)
}
}
View File
Binary file not shown.
+8
View File
@@ -0,0 +1,8 @@
module.exports = {
doc : "mysql模型文件的m5记录",
model: {
fileName: {type: "STRING", comment: '文件名称'},
md5 : {type: "TEXT", comment: 'md5记录'},
md5json : {type: "JSON", comment: 'md5记录'},
}
}