user_login.go 4.0 KB

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