auth.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package controller
  2. import (
  3. "encoding/base64"
  4. "eta_gn/eta_bridge/controller/resp"
  5. "eta_gn/eta_bridge/global"
  6. "eta_gn/eta_bridge/models/crm"
  7. "eta_gn/eta_bridge/models/eta"
  8. "eta_gn/eta_bridge/models/rddp"
  9. "eta_gn/eta_bridge/models/request"
  10. "eta_gn/eta_bridge/models/response"
  11. "eta_gn/eta_bridge/services"
  12. "eta_gn/eta_bridge/utils"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "github.com/go-playground/validator/v10"
  16. "time"
  17. )
  18. type AuthController struct{}
  19. func (a *AuthController) CreateAuthCode(c *gin.Context) {
  20. var req request.CreateAuthCodeReq
  21. err := c.Bind(&req)
  22. if err != nil {
  23. errs, ok := err.(validator.ValidationErrors)
  24. if !ok {
  25. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  26. return
  27. }
  28. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  29. return
  30. }
  31. prefix := map[int]string{
  32. utils.SOURCE_CRM_FLAG: utils.CACHE_ETA_AUTH_CODE_PREFIX,
  33. utils.SOURCE_ETA_FLAG: utils.CACHE_CRM_AUTH_CODE_PREFIX,
  34. }
  35. str := base64.URLEncoding.EncodeToString([]byte(req.AdminName))
  36. key := fmt.Sprint(prefix[req.Source], str)
  37. exist, _ := global.Rc.RedisString(key)
  38. if exist != "" {
  39. resp.OkData("获取成功", str, c)
  40. return
  41. }
  42. if global.Rc.SetNX(key, req.AdminName, utils.GetTodayLastSecond()) {
  43. resp.OkData("获取成功", str, c)
  44. return
  45. }
  46. return
  47. }
  48. func (a *AuthController) GetEtaToken(c *gin.Context) {
  49. var req request.TokenLoginReq
  50. err := c.Bind(&req)
  51. if err != nil {
  52. errs, ok := err.(validator.ValidationErrors)
  53. if !ok {
  54. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  55. return
  56. }
  57. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  58. return
  59. }
  60. authCode := req.AuthCode
  61. if authCode == `` {
  62. authCode = req.Code
  63. }
  64. if authCode == `` {
  65. resp.FailData("参数解析失败", "code or auth_code为空", c)
  66. return
  67. }
  68. key := fmt.Sprint(utils.CACHE_ETA_AUTH_CODE_PREFIX, authCode)
  69. adminName, e := global.Rc.RedisString(key)
  70. if e != nil {
  71. resp.FailMsg("获取失败", "获取失败, Redis Err: "+e.Error(), c)
  72. return
  73. }
  74. if adminName == "" {
  75. resp.Fail("获取失败, 无效编码", c)
  76. return
  77. }
  78. defer func() {
  79. _ = global.Rc.Delete(key)
  80. }()
  81. sysUser, e := eta.GetSysUserByAdminName(adminName)
  82. if e != nil {
  83. if e == utils.ErrNoRow {
  84. resp.Fail("用户不存在", c)
  85. return
  86. }
  87. resp.FailMsg("获取失败", "获取用户信息失败, err: "+e.Error(), c)
  88. return
  89. }
  90. var token string
  91. account := utils.MD5(adminName)
  92. expired := time.Now().AddDate(0, 0, 1).Format(utils.FormatDateTime)
  93. session, _ := eta.GetUserUnexpiredSysSession(adminName, expired)
  94. if session != nil && session.AccessToken != "" {
  95. token = session.AccessToken
  96. } else {
  97. token = utils.GenToken(account)
  98. sysSession := new(eta.SysSession)
  99. sysSession.UserName = adminName
  100. sysSession.SysUserId = sysUser.AdminId
  101. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  102. sysSession.IsRemember = 1
  103. sysSession.CreatedTime = time.Now()
  104. sysSession.LastUpdatedTime = time.Now()
  105. sysSession.AccessToken = token
  106. if e := eta.AddSysSession(sysSession); e != nil {
  107. resp.FailMsg("获取失败", "新增session失败, err: "+e.Error(), c)
  108. return
  109. }
  110. }
  111. login := new(response.LoginResp)
  112. login.Authorization = token
  113. login.Authorization = "authorization=" + token + "$account=" + account
  114. login.RealName = sysUser.RealName
  115. login.AdminId = sysUser.AdminId
  116. login.AdminName = sysUser.AdminName
  117. login.RoleName = sysUser.RoleName
  118. login.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  119. login.RoleTypeCode = sysUser.RoleTypeCode
  120. login.Authority = sysUser.Authority
  121. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  122. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  123. }
  124. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  125. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  126. }
  127. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  128. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  129. }
  130. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  131. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  132. }
  133. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  134. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  135. }
  136. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  137. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  138. }
  139. productId := services.GetProductId(sysUser.RoleTypeCode)
  140. productIdName := map[int]string{
  141. 0: "admin",
  142. utils.COMPANY_PRODUCT_FICC_ID: utils.COMPANY_PRODUCT_FICC_NAME,
  143. utils.COMPANY_PRODUCT_RAI_ID: utils.COMPANY_PRODUCT_RAI_NAME,
  144. }
  145. login.ProductName = productIdName[productId]
  146. {
  147. record := new(eta.SysUserLoginRecord)
  148. record.Uid = sysUser.AdminId
  149. record.UserName = adminName
  150. record.Ip = c.RemoteIP()
  151. record.Stage = "login"
  152. record.CreateTime = time.Now()
  153. go eta.AddSysUserLoginRecord(record)
  154. }
  155. resp.OkData("获取成功", login, c)
  156. }
  157. func (a *AuthController) GetCrmToken(c *gin.Context) {
  158. var req request.TokenLoginReq
  159. err := c.Bind(&req)
  160. if err != nil {
  161. errs, ok := err.(validator.ValidationErrors)
  162. if !ok {
  163. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  164. return
  165. }
  166. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  167. return
  168. }
  169. if req.AuthCode == `` {
  170. resp.FailData("参数解析失败", "auth_code为空", c)
  171. return
  172. }
  173. key := fmt.Sprint(utils.CACHE_CRM_AUTH_CODE_PREFIX, req.AuthCode)
  174. adminName, e := global.Rc.RedisString(key)
  175. if e != nil {
  176. resp.FailMsg("获取失败", "获取失败, Redis Err: "+e.Error(), c)
  177. return
  178. }
  179. if adminName == "" {
  180. resp.Fail("获取失败, 无效编码", c)
  181. return
  182. }
  183. defer func() {
  184. _ = global.Rc.Delete(key)
  185. }()
  186. sysUser, e := crm.GetSysUserByAdminName(adminName)
  187. if e != nil {
  188. if e == utils.ErrNoRow {
  189. resp.Fail("用户不存在", c)
  190. return
  191. }
  192. resp.FailMsg("获取失败", "获取用户信息失败, err: "+e.Error(), c)
  193. return
  194. }
  195. var token string
  196. account := utils.MD5(adminName)
  197. expired := time.Now().AddDate(0, 0, 1).Format(utils.FormatDateTime)
  198. session, _ := rddp.GetUserUnexpiredSysSession(adminName, expired)
  199. if session != nil && session.AccessToken != "" {
  200. token = session.AccessToken
  201. } else {
  202. token = utils.GenToken(account)
  203. sysSession := new(rddp.SysSession)
  204. sysSession.UserName = adminName
  205. sysSession.SysUserId = sysUser.AdminId
  206. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  207. sysSession.IsRemember = 1
  208. sysSession.CreatedTime = time.Now()
  209. sysSession.LastUpdatedTime = time.Now()
  210. sysSession.AccessToken = token
  211. if e := rddp.AddSysSession(sysSession); e != nil {
  212. resp.FailMsg("获取失败", "新增session失败, err: "+e.Error(), c)
  213. return
  214. }
  215. }
  216. login := new(response.LoginResp)
  217. login.Authorization = token
  218. login.Authorization = "authorization=" + token + "$account=" + account
  219. login.RealName = sysUser.RealName
  220. login.AdminId = sysUser.AdminId
  221. login.AdminName = sysUser.AdminName
  222. login.RoleName = sysUser.RoleName
  223. login.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  224. login.RoleTypeCode = sysUser.RoleTypeCode
  225. login.Authority = sysUser.Authority
  226. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  227. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  228. }
  229. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  230. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  231. }
  232. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  233. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  234. }
  235. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  236. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  237. }
  238. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  239. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  240. }
  241. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  242. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  243. }
  244. productId := services.GetProductId(sysUser.RoleTypeCode)
  245. productIdName := map[int]string{
  246. 0: "admin",
  247. utils.COMPANY_PRODUCT_FICC_ID: utils.COMPANY_PRODUCT_FICC_NAME,
  248. utils.COMPANY_PRODUCT_RAI_ID: utils.COMPANY_PRODUCT_RAI_NAME,
  249. }
  250. login.ProductName = productIdName[productId]
  251. {
  252. record := new(rddp.SysUserLoginRecord)
  253. record.Uid = sysUser.AdminId
  254. record.UserName = adminName
  255. record.Ip = c.RemoteIP()
  256. record.Stage = "login"
  257. record.CreateTime = time.Now()
  258. go rddp.AddSysUserLoginRecord(record)
  259. }
  260. resp.OkData("获取成功", login, c)
  261. }