sys_user.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_ETA_mobile_api/models"
  6. "hongze/hongze_ETA_mobile_api/models/system"
  7. "hongze/hongze_ETA_mobile_api/services"
  8. "hongze/hongze_ETA_mobile_api/utils"
  9. "time"
  10. )
  11. // 系统相关
  12. type SysUserController struct {
  13. BaseCommonController
  14. }
  15. // @Title 系统用户登录接口
  16. // @Description 系统用户登录
  17. // @Param request body models.LoginReq true "type json string"
  18. // @Success 200 {object} models.LoginResp
  19. // @router /login [post]
  20. func (this *SysUserController) Login() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. var req system.LoginReq
  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 := system.CheckSysUser(req.Username, req.Password)
  42. if err != nil {
  43. if err.Error() == utils.ErrNoRow() {
  44. br.Msg = "登录失败,账号或密码错误"
  45. br.ErrMsg = "登录失败,账号或密码错误,Err:" + err.Error()
  46. br.IsSendEmail = false
  47. return
  48. }
  49. br.IsSendEmail = false
  50. br.Msg = "登录失败,账号或密码错误"
  51. br.ErrMsg = "登录失败,Err:" + err.Error()
  52. return
  53. }
  54. if sysUser == nil {
  55. br.IsSendEmail = false
  56. br.Msg = "登录失败,账号或密码错误"
  57. br.ErrMsg = "登录失败,sysUser is nil "
  58. return
  59. }
  60. if sysUser.Enabled == 0 {
  61. br.Msg = "您的账号已被禁用,如需登录,请联系管理员"
  62. br.ErrMsg = fmt.Sprintf("您的账号已被禁用,登录账号:%s,账户名称:%s,如需登录,请联系管理员", sysUser.AdminName, sysUser.RealName)
  63. return
  64. }
  65. // 是否记住密码
  66. var isRemember int
  67. if req.IsRemember {
  68. isRemember = 1
  69. }
  70. account := utils.MD5(req.Username)
  71. token := utils.GenToken(account)
  72. sysSession := new(system.SysSession)
  73. sysSession.UserName = req.Username
  74. sysSession.SysUserId = sysUser.AdminId
  75. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  76. sysSession.IsRemember = isRemember
  77. sysSession.CreatedTime = time.Now()
  78. sysSession.LastUpdatedTime = time.Now()
  79. sysSession.AccessToken = token
  80. err = system.AddSysSession(sysSession)
  81. fmt.Println("id:", sysSession.Id)
  82. if err != nil {
  83. br.ErrMsg = "新增session信息失败,Err:" + err.Error()
  84. br.Msg = "登录失败"
  85. return
  86. }
  87. // 修改最后登录时间
  88. {
  89. sysUser.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  90. sysUser.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  91. sysUser.Update([]string{"LastLoginTime", "LastUpdatedTime"})
  92. }
  93. resp := new(system.LoginResp)
  94. resp.Authorization = token
  95. resp.Authorization = "authorization=" + token + "$account=" + account
  96. resp.RealName = sysUser.RealName
  97. resp.AdminName = sysUser.AdminName
  98. resp.RoleName = sysUser.RoleName
  99. resp.DepartmentName = sysUser.DepartmentName
  100. resp.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  101. resp.RoleTypeCode = sysUser.RoleTypeCode
  102. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  103. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  104. }
  105. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  106. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  107. }
  108. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  109. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  110. }
  111. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  112. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  113. }
  114. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  115. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  116. }
  117. resp.AdminId = sysUser.AdminId
  118. var productName string
  119. productId := services.GetProductId(sysUser.RoleTypeCode)
  120. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  121. // productName = utils.COMPANY_PRODUCT_FICC_NAME
  122. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  123. // productName = utils.COMPANY_PRODUCT_RAI_NAME
  124. //} else {
  125. // productName = "admin"
  126. //}
  127. if productId == 1 {
  128. productName = utils.COMPANY_PRODUCT_FICC_NAME
  129. } else if productId == 2 {
  130. productName = utils.COMPANY_PRODUCT_RAI_NAME
  131. } else {
  132. productName = "admin"
  133. }
  134. resp.ProductName = productName
  135. resp.Authority = sysUser.Authority
  136. br.Ret = 200
  137. br.Success = true
  138. br.Msg = "登录成功"
  139. br.Data = resp
  140. //新增登录记录
  141. {
  142. record := new(system.SysUserLoginRecord)
  143. record.Uid = sysUser.AdminId
  144. record.UserName = req.Username
  145. record.Ip = this.Ctx.Input.IP()
  146. record.Stage = "login"
  147. record.CreateTime = time.Now()
  148. go system.AddSysUserLoginRecord(record)
  149. }
  150. // 设置redis缓存
  151. {
  152. // 获取不可信的登录态,并将该登录态重置掉,不允许多次登录
  153. noTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST, sysUser.AdminId)
  154. noTrustLoginId, _ := utils.Rc.RedisString(noTrustLoginKey)
  155. fmt.Println("noTrustLoginId:", noTrustLoginId)
  156. if noTrustLoginId != `` { // 如果存在不可信设备,那么将其下架
  157. oldNoTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, noTrustLoginId)
  158. utils.Rc.Put(oldNoTrustLoginKey, "0", 30*time.Minute)
  159. }
  160. if !req.IsRemember { // 如果当前是不可信设备,那么将其加入到不可信名单
  161. loginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, sysSession.Id)
  162. utils.Rc.Put(loginKey, "1", 30*time.Minute)
  163. noTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST, sysUser.AdminId)
  164. utils.Rc.Put(noTrustLoginKey, sysSession.Id, 30*time.Minute)
  165. }
  166. }
  167. }
  168. type SysUserAuthController struct {
  169. BaseAuthController
  170. }
  171. // CheckPwd
  172. // @Title 校验是否初始密码
  173. // @Description 校验是否初始密码
  174. // @Success 200 {bool} true "是否初始密码;false代表不是初始密码,直接进去;true代表是初始密码,需要强制修改密码"
  175. // @router /check_pwd [get]
  176. func (this *SysUserAuthController) CheckPwd() {
  177. br := new(models.BaseResponse).Init()
  178. defer func() {
  179. this.Data["json"] = br
  180. this.ServeJSON()
  181. }()
  182. sysUser := this.SysUser
  183. if sysUser == nil {
  184. br.Msg = "登录失败,账号或密码错误"
  185. br.ErrMsg = "登录失败,sysUser is nil "
  186. return
  187. }
  188. br.Data = false
  189. if sysUser.Password == utils.MD5("123456a") {
  190. br.Data = true
  191. }
  192. br.Ret = 200
  193. br.Success = true
  194. br.Msg = "校验成功"
  195. }