广告平台(站长下代理使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hdl_login.go 1.5 KiB

3 months ago
2 months ago
3 months ago
2 months ago
2 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package hdl
  2. import (
  3. "applet/app/e"
  4. "applet/app/lib/validate"
  5. "applet/app/md"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. db "code.fnuoos.com/zhimeng/model.git/src"
  9. "code.fnuoos.com/zhimeng/model.git/src/implement"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. )
  13. // Login 登陆
  14. // @Summary 登陆
  15. // @Tags ADMIN
  16. // @Description 登入
  17. // @Accept json
  18. // @Produce json
  19. // @Param req body md.LoginReq true "用户名密码"
  20. // @Success 200 {object} md.LoginResponse "token"
  21. // @Failure 400 {object} md.Response "具体错误"
  22. // @Router /api/login [post]
  23. func Login(c *gin.Context) {
  24. var req md.LoginReq
  25. err := c.ShouldBindJSON(&req)
  26. if err != nil {
  27. err = validate.HandleValidateErr(err)
  28. err1 := err.(e.E)
  29. e.OutErr(c, err1.Code, err1.Error())
  30. return
  31. }
  32. engine := db.DBs[svc.GetMasterId(c)]
  33. adminDb := implement.NewAgentDb(engine)
  34. admin, err := adminDb.GetAgentByUsername(req.UserName)
  35. if err != nil {
  36. e.OutErr(c, e.ERR_DB_ORM, err)
  37. return
  38. }
  39. if admin == nil {
  40. e.OutErr(c, e.ERR_NO_DATA, "账号不存在!")
  41. return
  42. }
  43. if utils.Md5(req.PassWord) != admin.Password {
  44. e.OutErr(c, e.ERR_INVALID_ARGS, "密码错误")
  45. return
  46. }
  47. ip := utils.GetIP(c.Request)
  48. key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(admin.Id))
  49. token, err := svc.HandleLoginToken(key, admin)
  50. if err != nil {
  51. e.OutErr(c, e.ERR, err.Error())
  52. return
  53. }
  54. e.OutSuc(c, md.LoginResponse{
  55. Token: token,
  56. }, nil)
  57. return
  58. }