session.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_bridge/models/eta"
  5. "eta/eta_bridge/models/response"
  6. "eta/eta_bridge/utils"
  7. "time"
  8. )
  9. // CreateEtaSession
  10. // @Description: 创建ETA会话session
  11. // @author: Roc
  12. // @datetime 2024-01-23 17:42:54
  13. // @param sysUser *eta.Admin
  14. // @return login *response.LoginResp
  15. // @return err error
  16. // @return errMsg string
  17. func CreateEtaSession(sysUser *eta.Admin) (login response.LoginResp, session *eta.SysSession, err error, errMsg string) {
  18. errMsg = `登录失败`
  19. login = response.LoginResp{}
  20. var token string
  21. account := utils.MD5(sysUser.AdminName)
  22. //// 获取用户未过期的session, 避免过于频繁生成token
  23. //expired := time.Now().AddDate(0, 0, 1).Format(utils.FormatDateTime)
  24. //session, _ = eta.GetUserUnexpiredSysSession(sysUser.AdminName, expired)
  25. //if session != nil && session.AccessToken != "" {
  26. // token = session.AccessToken
  27. //} else {
  28. // token = utils.GenToken(account)
  29. // session = new(eta.SysSession)
  30. // session.UserName = sysUser.AdminName
  31. // session.SysUserId = sysUser.AdminId
  32. // session.ExpiredTime = time.Now().AddDate(0, 0, 90)
  33. // session.IsRemember = 1
  34. // session.CreatedTime = time.Now()
  35. // session.LastUpdatedTime = time.Now()
  36. // session.AccessToken = token
  37. // if e := eta.AddSysSession(session); e != nil {
  38. // errMsg = "新增session失败, err: "
  39. // err = errors.New("新增session失败, err: " + e.Error())
  40. // return
  41. // }
  42. //}
  43. token = utils.GenToken(account)
  44. session = new(eta.SysSession)
  45. session.UserName = sysUser.AdminName
  46. session.SysUserId = sysUser.AdminId
  47. session.ExpiredTime = time.Now().AddDate(0, 0, 90)
  48. session.IsRemember = 1
  49. session.CreatedTime = time.Now()
  50. session.LastUpdatedTime = time.Now()
  51. session.AccessToken = token
  52. if e := eta.AddSysSession(session); e != nil {
  53. errMsg = "新增session失败, err: "
  54. err = errors.New("新增session失败, err: " + e.Error())
  55. return
  56. }
  57. login.Authorization = token
  58. login.Authorization = "authorization=" + token
  59. login.RealName = sysUser.RealName
  60. login.AdminId = sysUser.AdminId
  61. login.AdminName = sysUser.AdminName
  62. login.RoleName = sysUser.RoleName
  63. login.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  64. login.RoleTypeCode = sysUser.RoleTypeCode
  65. login.Authority = sysUser.Authority
  66. // 判断实际的角色类型
  67. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  68. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  69. }
  70. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  71. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  72. }
  73. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  74. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  75. }
  76. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  77. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  78. }
  79. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  80. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  81. }
  82. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  83. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  84. }
  85. // 角色产品ID
  86. productId := GetProductId(sysUser.RoleTypeCode)
  87. productIdName := map[int]string{
  88. 0: "admin",
  89. utils.COMPANY_PRODUCT_FICC_ID: utils.COMPANY_PRODUCT_FICC_NAME,
  90. utils.COMPANY_PRODUCT_RAI_ID: utils.COMPANY_PRODUCT_RAI_NAME,
  91. }
  92. login.ProductName = productIdName[productId]
  93. return
  94. }