中间件添加操作信息
-
context增加属性userid用于储存访问者id,便于后期使用中间件添加操作信息
type Context struct {
//......,context其他属性
Userid int
}
-
对于添加操作信息的中间件函数
// 添加用户操作中间件
func Addoperationmid(db *sql.DB) gee7.HandlerFunc {
return func(ctx *gee7.Context) {
ctx.Next()
//没有id信息,直接退出无需插入操作信息
if ctx.Userid == 0 {
return
}
if ctx.StatusCode >= 400 {
Addoperation(db, ctx.Userid, "操作"+ctx.Path+"失败")
}
Addoperation(db, ctx.Userid, "操作"+ctx.Path+"成功")
}
}
-
Addoperation函数内容
// 添加操作信息,向表OperationRecord(操作列表)添加
func Addoperation(db *sql.DB, id int, operation string) error {
_, err := db.Exec("insert into OperationRecord (id,behavior,time) VALUES (?, ?, ?)", id, operation, time.Now().Unix())
if err != nil {
return err
}
return nil
}
统一日志输出
-
设置日志输出格式
//设置统一日志输出(日期+时间+输出文件+行+信息)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
-
定义统一输出函数
// 统一输出日志
// 程序正常运行
func Lognormal(msg string) {
log.Println("normal:", msg)
}
// 警告
func Logwarning(msg string) {
log.Println("warning:", msg)
}
// 错误
func Logerror(msg string) {
log.Panicln("error:", msg)
}
接口文档
接口执行示例
图片验证码获取
-
正确示例
注册
端口: http://localhost:8888/login1 1.
-
错误示例
-
图片验证码错误
2. 邮箱已经注册
3. 邮箱不正确
端口: http://localhost:8888/login2
-
成功示例:
-
失败示例:
-
验证码过期
-
姓名已经使用
登录
端口: http://localhost:8888/signuppsw 成功示例:
失败示例:
-
账号密码错误
端口: http://localhost:8888/signupemail1
-
成功示例:
-
错误示例
-
邮箱错误
端口: http://localhost:8888/signupemail2
-
失败示例 验证码错误
改密
密码改密
-
正确示例
-
错误示例
-
密码错误
-
身份码错误
-
密码不规范
验证码改密
端口: http://localhost:8888/home/resetpaswbyemail1
-
成功示例
接口: http://localhost:8888/home/resetpaswbyemail2
-
成功示例
失败示例与登录的失败示例相同
登出
接口: http://localhost:8888/home/signout
-
成功示例
-
失败示例
身份码不正确
获取操作记录
接口: http://localhost:8888/getoperations
{
"code": 200,
"msg": "ok",
"data": {
"operations": [
{
"id": 10007,
"behavior": "注册成功",
"time": 1682254498
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682254592
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682254639
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682254653
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682254655
},
{
"id": 10007,
"behavior": "登出成功",
"time": 1682254752
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682254936
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682255058
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682333497
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682334103
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682334247
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682334252
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682334272
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682334274
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682334306
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682334321
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682334322
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682336453
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682517978
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682518235
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682518342
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682518407
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682601500
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682601562
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682601897
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682601923
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682601946
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbypasw失败",
"time": 1682607778
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbypasw成功",
"time": 1682607778
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682607789
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbypasw成功",
"time": 1682607789
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbypasw成功",
"time": 1682609855
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbypasw失败",
"time": 1682609862
},
{
"id": 10007,
"behavior": "signin成功",
"time": 1682612012
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbypasw成功",
"time": 1682613019
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbypasw失败",
"time": 1682613108
},
{
"id": 10007,
"behavior": "操作/home/resetpaswbyemail1成功",
"time": 1682613282
},
{
"id": 10007,
"behavior": "改密成功",
"time": 1682613472
}
]
}
}
-
成功示例
失败示例与登出相同
本文由 mdnice 多平台发布