user_login.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm/models"
  5. "eta/eta_mini_crm/models/request"
  6. "eta/eta_mini_crm/models/response"
  7. "eta/eta_mini_crm/utils"
  8. "fmt"
  9. "time"
  10. )
  11. type UserLoginController struct {
  12. BaseCommonController
  13. }
  14. // Login
  15. // @Title 用户登录
  16. // @Description 用户登录
  17. // @Param request body UserLoginReq true "type json string"
  18. // @Success 200 {object} models.LoginResp
  19. // @router /login [post]
  20. func (this *UserLoginController) Login() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. var req request.UserLoginReq
  27. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  28. if err != nil {
  29. br.Msg = "参数解析异常!"
  30. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  31. return
  32. }
  33. if req.UserName == "" {
  34. br.Msg = "请输入账号"
  35. return
  36. }
  37. if req.Password == "" {
  38. br.Msg = "请输入密码"
  39. return
  40. }
  41. sysUser, err := models.GetSysUserBySysUserName(req.UserName)
  42. if err != nil {
  43. if err.Error() == utils.ErrNoRow() {
  44. br.Msg = "登录失败, 账号或密码错误"
  45. return
  46. } else {
  47. br.Msg = "系统错误"
  48. br.ErrMsg = "系统错误" + err.Error()
  49. return
  50. }
  51. }
  52. sysRole, err := models.GetSysRoleById(sysUser.SysRoleId)
  53. if err != nil {
  54. br.Msg = "登录失败"
  55. br.ErrMsg = "查询角色失败, Err:" + err.Error()
  56. return
  57. }
  58. dbPass := utils.MD5(fmt.Sprintf("%s%s%s", sysUser.Password, utils.UserLoginSalt, req.ReqTime))
  59. if req.Password != dbPass {
  60. br.Msg = "登录失败, 账号或密码错误"
  61. return
  62. }
  63. account := utils.MD5(sysUser.SysUserName)
  64. token := utils.GenToken(account)
  65. sysSession := new(models.SysSession)
  66. sysSession.UserName = sysUser.SysUserName
  67. sysSession.SysUserId = sysUser.SysUserId
  68. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 60)
  69. sysSession.CreatedTime = time.Now()
  70. sysSession.LastUpdatedTime = time.Now()
  71. sysSession.AccessToken = token
  72. err = sysSession.AddSysSession()
  73. if err != nil {
  74. br.Msg = "登录失败"
  75. br.ErrMsg = "新增session信息失败, Err:" + err.Error()
  76. return
  77. }
  78. resp := new(response.LoginResp)
  79. resp.Authorization = "authorization=" + token
  80. resp.SysUserName = sysUser.SysUserName
  81. resp.SysRealName = sysUser.SysRealName
  82. resp.SysUserId = sysUser.SysUserId
  83. resp.RoleName = sysRole.SysRoleName
  84. resp.RoleId = sysUser.SysRoleId
  85. // 获取不可信的登录态,并将该登录态重置掉,不允许多次登录
  86. noTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST, sysUser.SysUserId)
  87. noTrustLoginId, _ := utils.Rc.RedisString(noTrustLoginKey)
  88. fmt.Println("noTrustLoginId:", noTrustLoginId)
  89. if noTrustLoginId != `` { // 如果存在不可信设备,那么将其下架
  90. oldNoTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, noTrustLoginId)
  91. utils.Rc.Put(oldNoTrustLoginKey, "0", time.Hour*24)
  92. }
  93. // 设置redis缓存,记录用户登录态
  94. loginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, sysSession.SysSessionId)
  95. utils.Rc.Put(loginKey, "1", time.Hour*24)
  96. utils.Rc.Put(noTrustLoginKey, sysSession.SysSessionId, time.Hour*24*60)
  97. br.Data = resp
  98. br.Ret = 200
  99. br.Success = true
  100. br.Msg = "登录成功"
  101. }
  102. // AreaCodeList
  103. // @Title 手机号区号列表
  104. // @Description 手机号区号列表
  105. // @Success 200 Ret=200 获取成功
  106. // @router /area_code/list [get]
  107. func (this *UserLoginController) AreaCodeList() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. if br.ErrMsg == "" {
  111. br.IsSendEmail = false
  112. }
  113. this.Data["json"] = br
  114. this.ServeJSON()
  115. }()
  116. resp := make([]response.AreaCodeListResp, 0)
  117. confAuth, e := models.GetConfigDetailByCode(models.ConfAreaCodeListKey)
  118. if e != nil {
  119. br.Msg = "获取失败"
  120. br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
  121. return
  122. }
  123. if confAuth.ConfigValue == "" {
  124. br.Msg = "获取失败"
  125. br.ErrMsg = "手机号区号配置为空"
  126. return
  127. }
  128. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &resp); e != nil {
  129. br.Msg = "获取失败"
  130. br.ErrMsg = "手机号区号配置有误"
  131. return
  132. }
  133. br.Data = resp
  134. br.Ret = 200
  135. br.Success = true
  136. br.Msg = "获取成功"
  137. }