auth.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package controller
  2. import (
  3. "encoding/base64"
  4. "eta/eta_bridge/controller/resp"
  5. "eta/eta_bridge/global"
  6. "eta/eta_bridge/models/crm"
  7. "eta/eta_bridge/models/eta"
  8. "eta/eta_bridge/models/rddp"
  9. "eta/eta_bridge/models/request"
  10. "eta/eta_bridge/models/response"
  11. "eta/eta_bridge/services"
  12. "eta/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. // 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. authCode := req.AuthCode
  70. if authCode == `` {
  71. authCode = req.Code
  72. }
  73. if authCode == `` {
  74. resp.FailData("参数解析失败", "code or auth_code为空", c)
  75. return
  76. }
  77. key := fmt.Sprint(utils.CACHE_ETA_AUTH_CODE_PREFIX, authCode)
  78. adminName, e := global.Rc.RedisString(key)
  79. if e != nil {
  80. resp.FailMsg("获取失败", "获取失败, Redis Err: "+e.Error(), c)
  81. return
  82. }
  83. if adminName == "" {
  84. resp.Fail("获取失败, 无效编码", c)
  85. return
  86. }
  87. // 清除AuthCode
  88. defer func() {
  89. _ = global.Rc.Delete(key)
  90. }()
  91. // 获取用户信息
  92. sysUser, e := eta.GetSysUserByAdminName(adminName)
  93. if e != nil {
  94. if e == utils.ErrNoRow {
  95. resp.Fail("用户不存在", c)
  96. return
  97. }
  98. resp.FailMsg("获取失败", "获取用户信息失败, err: "+e.Error(), c)
  99. return
  100. }
  101. var token string
  102. account := utils.MD5(adminName)
  103. // 获取用户未过期的session, 避免过于频繁生成token
  104. expired := time.Now().AddDate(0, 0, 1).Format(utils.FormatDateTime)
  105. session, _ := eta.GetUserUnexpiredSysSession(adminName, expired)
  106. if session != nil && session.AccessToken != "" {
  107. token = session.AccessToken
  108. } else {
  109. token = utils.GenToken(account)
  110. sysSession := new(eta.SysSession)
  111. sysSession.UserName = adminName
  112. sysSession.SysUserId = sysUser.AdminId
  113. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  114. sysSession.IsRemember = 1
  115. sysSession.CreatedTime = time.Now()
  116. sysSession.LastUpdatedTime = time.Now()
  117. sysSession.AccessToken = token
  118. if e := eta.AddSysSession(sysSession); e != nil {
  119. resp.FailMsg("获取失败", "新增session失败, err: "+e.Error(), c)
  120. return
  121. }
  122. }
  123. login := new(response.LoginResp)
  124. login.Authorization = token
  125. login.Authorization = "authorization=" + token + "$account=" + account
  126. login.RealName = sysUser.RealName
  127. login.AdminId = sysUser.AdminId
  128. login.AdminName = sysUser.AdminName
  129. login.RoleName = sysUser.RoleName
  130. login.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  131. login.RoleTypeCode = sysUser.RoleTypeCode
  132. login.Authority = sysUser.Authority
  133. // 判断实际的角色类型
  134. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  135. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  136. }
  137. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  138. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  139. }
  140. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  141. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  142. }
  143. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  144. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  145. }
  146. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  147. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  148. }
  149. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  150. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  151. }
  152. // 角色产品ID
  153. productId := services.GetProductId(sysUser.RoleTypeCode)
  154. productIdName := map[int]string{
  155. 0: "admin",
  156. utils.COMPANY_PRODUCT_FICC_ID: utils.COMPANY_PRODUCT_FICC_NAME,
  157. utils.COMPANY_PRODUCT_RAI_ID: utils.COMPANY_PRODUCT_RAI_NAME,
  158. }
  159. login.ProductName = productIdName[productId]
  160. // 新增登录记录
  161. {
  162. record := new(eta.SysUserLoginRecord)
  163. record.Uid = sysUser.AdminId
  164. record.UserName = adminName
  165. record.Ip = c.RemoteIP()
  166. record.Stage = "login"
  167. record.CreateTime = time.Now()
  168. go eta.AddSysUserLoginRecord(record)
  169. }
  170. resp.OkData("获取成功", login, c)
  171. }
  172. // GetCrmToken
  173. // @Description 换取CRM系统Token
  174. // @Success 200 {string} string "操作成功"
  175. // @Router /auth/crm_token [post]
  176. func (a *AuthController) GetCrmToken(c *gin.Context) {
  177. var req request.TokenLoginReq
  178. err := c.Bind(&req)
  179. if err != nil {
  180. errs, ok := err.(validator.ValidationErrors)
  181. if !ok {
  182. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  183. return
  184. }
  185. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  186. return
  187. }
  188. if req.AuthCode == `` {
  189. resp.FailData("参数解析失败", "auth_code为空", c)
  190. return
  191. }
  192. key := fmt.Sprint(utils.CACHE_CRM_AUTH_CODE_PREFIX, req.AuthCode)
  193. adminName, e := global.Rc.RedisString(key)
  194. if e != nil {
  195. resp.FailMsg("获取失败", "获取失败, Redis Err: "+e.Error(), c)
  196. return
  197. }
  198. if adminName == "" {
  199. resp.Fail("获取失败, 无效编码", c)
  200. return
  201. }
  202. // 清除AuthCode
  203. defer func() {
  204. _ = global.Rc.Delete(key)
  205. }()
  206. // 获取用户信息
  207. sysUser, e := crm.GetSysUserByAdminName(adminName)
  208. if e != nil {
  209. if e == utils.ErrNoRow {
  210. resp.Fail("用户不存在", c)
  211. return
  212. }
  213. resp.FailMsg("获取失败", "获取用户信息失败, err: "+e.Error(), c)
  214. return
  215. }
  216. var token string
  217. account := utils.MD5(adminName)
  218. // 获取用户未过期的session, 避免过于频繁生成token
  219. expired := time.Now().AddDate(0, 0, 1).Format(utils.FormatDateTime)
  220. session, _ := rddp.GetUserUnexpiredSysSession(adminName, expired)
  221. if session != nil && session.AccessToken != "" {
  222. token = session.AccessToken
  223. } else {
  224. token = utils.GenToken(account)
  225. sysSession := new(rddp.SysSession)
  226. sysSession.UserName = adminName
  227. sysSession.SysUserId = sysUser.AdminId
  228. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  229. sysSession.IsRemember = 1
  230. sysSession.CreatedTime = time.Now()
  231. sysSession.LastUpdatedTime = time.Now()
  232. sysSession.AccessToken = token
  233. if e := rddp.AddSysSession(sysSession); e != nil {
  234. resp.FailMsg("获取失败", "新增session失败, err: "+e.Error(), c)
  235. return
  236. }
  237. }
  238. login := new(response.LoginResp)
  239. login.Authorization = token
  240. login.Authorization = "authorization=" + token + "$account=" + account
  241. login.RealName = sysUser.RealName
  242. login.AdminId = sysUser.AdminId
  243. login.AdminName = sysUser.AdminName
  244. login.RoleName = sysUser.RoleName
  245. login.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  246. login.RoleTypeCode = sysUser.RoleTypeCode
  247. login.Authority = sysUser.Authority
  248. // 判断实际的角色类型
  249. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  250. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  251. }
  252. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  253. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  254. }
  255. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  256. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  257. }
  258. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  259. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  260. }
  261. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  262. login.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  263. }
  264. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  265. login.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  266. }
  267. // 角色产品ID
  268. productId := services.GetProductId(sysUser.RoleTypeCode)
  269. productIdName := map[int]string{
  270. 0: "admin",
  271. utils.COMPANY_PRODUCT_FICC_ID: utils.COMPANY_PRODUCT_FICC_NAME,
  272. utils.COMPANY_PRODUCT_RAI_ID: utils.COMPANY_PRODUCT_RAI_NAME,
  273. }
  274. login.ProductName = productIdName[productId]
  275. // 新增登录记录
  276. {
  277. record := new(rddp.SysUserLoginRecord)
  278. record.Uid = sysUser.AdminId
  279. record.UserName = adminName
  280. record.Ip = c.RemoteIP()
  281. record.Stage = "login"
  282. record.CreateTime = time.Now()
  283. go rddp.AddSysUserLoginRecord(record)
  284. }
  285. resp.OkData("获取成功", login, c)
  286. }