user.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_clpt/models"
  5. "hongze/hongze_clpt/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type UserController struct {
  11. BaseAuthController
  12. }
  13. type UserCommonController struct {
  14. BaseCommonController
  15. }
  16. // @Title 登录
  17. // @Description 登录接口
  18. // @Param request body models.LoginReq true "type json string"
  19. // @Success 200 {object} models.LoginResp
  20. // @router /login [post]
  21. func (this *UserCommonController) Login() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var token string
  28. var req models.LoginReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. mobile := req.Mobile
  36. req.Mobile = strings.Trim(req.Mobile, " ")
  37. if req.Mobile == "" {
  38. br.Msg = "参数错误"
  39. br.ErrMsg = "参数错误,手机号为空"
  40. return
  41. }
  42. code := req.VCode
  43. if code == "" {
  44. br.Msg = "参数错误"
  45. br.ErrMsg = "Code 为空"
  46. return
  47. }
  48. item, err := models.GetMsgCode(req.Mobile, req.VCode)
  49. if err != nil {
  50. if err.Error() == utils.ErrNoRow() {
  51. br.Msg = "验证码错误,请重新输入"
  52. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  53. return
  54. } else {
  55. br.Msg = "验证码错误,请重新输入"
  56. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  57. return
  58. }
  59. }
  60. if item == nil {
  61. br.Msg = "验证码错误,请重新输入"
  62. return
  63. }
  64. user, err := models.GetWxUserItemByMobile(mobile)
  65. if err != nil {
  66. br.Msg = "登录失败"
  67. br.ErrMsg = "获取用户信息失败,GetUserDetailByMobile Err:" + err.Error()
  68. return
  69. }
  70. timeUnix := time.Now().Unix()
  71. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  72. token = utils.MD5(mobile) + utils.MD5(timeUnixStr)
  73. itemsSession := new(models.CygxClptSession)
  74. itemsSession.UserId = user.UserId
  75. itemsSession.Mobile = mobile
  76. itemsSession.AccessToken = token
  77. itemsSession.CreatedTime = time.Now()
  78. itemsSession.LastUpdatedTime = time.Now()
  79. itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
  80. err = models.AddCygxClptSession(itemsSession)
  81. if err != nil {
  82. br.Msg = "获取用户信息失败"
  83. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  84. return
  85. }
  86. resp := new(models.LoginResp)
  87. resp.UserId = user.UserId
  88. resp.Headimgurl = user.Headimgurl
  89. resp.Mobile = user.Mobile
  90. resp.Email = user.Email
  91. resp.CompanyName = user.CompanyName
  92. resp.Authorization = token
  93. br.Ret = 200
  94. br.Success = true
  95. br.Msg = "获取成功"
  96. br.Data = resp
  97. }