auth.go 9.2 KB

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