项目1登录功能优化(中间件添加操作信息,统一日志输出)

中间件添加操作信息

  1. context增加属性userid用于储存访问者id,便于后期使用中间件添加操作信息
type Context struct {
  //......,context其他属性
 Userid   int
}
  1. 对于添加操作信息的中间件函数
// 添加用户操作中间件
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+"成功")
 }
}
  1. 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
}

统一日志输出

  1. 设置日志输出格式
//设置统一日志输出(日期+时间+输出文件+行+信息)
  log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  1. 定义统一输出函数
// 统一输出日志
// 程序正常运行
func Lognormal(msg string) {
 log.Println("normal:", msg)
}

// 警告
func Logwarning(msg string) {
 log.Println("warning:", msg)
}

// 错误
func Logerror(msg string) {
 log.Panicln("error:", msg)
}

接口文档

接口执行示例

图片验证码获取

  1. 正确示例
alt
注册

端口: http://localhost:8888/login1 1.

  • 错误示例
  1. 图片验证码错误

alt 2. 邮箱已经注册

alt 3. 邮箱不正确

alt 端口: http://localhost:8888/login2

  • 成功示例:
alt
  • 失败示例:
  1. 验证码过期 alt

  2. 姓名已经使用

alt
登录

端口: http://localhost:8888/signuppsw 成功示例:

alt

失败示例:

  1. 账号密码错误
alt

端口: http://localhost:8888/signupemail1

  • 成功示例:
alt
  • 错误示例
  1. 邮箱错误

alt 端口: http://localhost:8888/signupemail2

  • 失败示例 验证码错误
alt
改密
密码改密
  • 正确示例
alt
  • 错误示例
  1. 密码错误 alt

  2. 身份码错误

alt
  1. 密码不规范
alt

验证码改密

端口: http://localhost:8888/home/resetpaswbyemail1

  • 成功示例
alt

接口: http://localhost:8888/home/resetpaswbyemail2

  • 成功示例
alt

失败示例与登录的失败示例相同


登出

接口: http://localhost:8888/home/signout

  • 成功示例
alt
  • 失败示例

身份码不正确

alt
获取操作记录

接口: 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
            }
        ]
    }
}
  • 成功示例

alt 失败示例与登出相同

本文由 mdnice 多平台发布

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值