manage_auth.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package middleware
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/constant"
  5. "Cold_Logistic/internal/pkg/common/global"
  6. "Cold_Logistic/internal/pkg/utils/headutil"
  7. "Cold_Logistic/internal/server/infra/dao"
  8. "Cold_Logistic/internal/server/infra/models"
  9. "Cold_Logistic/internal/server/infra/thirdparty/internalservice/clod"
  10. "context"
  11. "github.com/gin-gonic/gin"
  12. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  13. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  14. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/log"
  15. )
  16. // ManageAuth 平台验证
  17. func ManageAuth() gin.HandlerFunc {
  18. return func(c *gin.Context) {
  19. _, token, err := headutil.ParseAuthHead(c)
  20. if err != nil {
  21. core.WriteResponse(c, errors.WrapC(err, codex.ErrSignatureInvalid, "Parse Authorization token fail."), nil)
  22. c.Abort()
  23. return
  24. }
  25. if token == "" {
  26. core.WriteResponse(c, errors.WithCodeOnce(err, codex.ErrSignatureInvalid, ""), nil)
  27. c.Abort()
  28. return
  29. }
  30. tokenInfo, err := Verification(c, token)
  31. if err != nil {
  32. core.WriteResponse(c, errors.WithCodeOnce(err, codex.ErrLoginFailed, "验证失败,请重新登陆"), nil)
  33. c.Abort()
  34. return
  35. }
  36. c.Set(constant.TokenInfoKey, tokenInfo)
  37. AccountIdLogger := c.Value(log.GetContextKey()).(log.Logger)
  38. AccountIdLogger = AccountIdLogger.WithValues("accountId", tokenInfo.AccountId)
  39. c.Set(log.GetContextKey(), AccountIdLogger)
  40. c.Next()
  41. }
  42. }
  43. func ManageAuthV2() gin.HandlerFunc {
  44. return func(c *gin.Context) {
  45. userToken, err := c.Cookie("User_tokey")
  46. if err != nil {
  47. core.WriteResponse(c, errors.WithCodeOnce(err, codex.ErrLoginFailed, "验证失败,请重新登陆"), nil)
  48. c.Abort()
  49. return
  50. }
  51. if userToken == "" {
  52. userToken = c.PostForm("User_tokey")
  53. }
  54. if userToken == "" {
  55. core.WriteResponse(c, errors.WithCodeOnce(err, codex.ErrSignatureInvalid, ""), nil)
  56. c.Abort()
  57. return
  58. }
  59. tokenInfo, err := Verification(c, userToken)
  60. if err != nil {
  61. core.WriteResponse(c, errors.WithCodeOnce(err, codex.ErrLoginFailed, "验证失败,请重新登陆"), nil)
  62. c.Abort()
  63. return
  64. }
  65. c.Set(constant.TokenInfoKey, tokenInfo)
  66. AccountIdLogger := c.Value(log.GetContextKey()).(log.Logger)
  67. AccountIdLogger = AccountIdLogger.WithValues("accountId", tokenInfo.AccountId)
  68. c.Set(log.GetContextKey(), AccountIdLogger)
  69. c.Next()
  70. }
  71. }
  72. func ManagePermis() gin.HandlerFunc {
  73. return func(c *gin.Context) {
  74. tokenInfo := global.GetTokenInfoFromContext(c)
  75. hasPermis, err := clod.NewBzdClodService().CheckUserPermissions(c, clod.PermisParam{
  76. Power_Id: tokenInfo.PowerId,
  77. Req_Url: c.FullPath(),
  78. })
  79. if err != nil {
  80. core.WriteResponse(c, errors.WithCodeOnce(err, codex.ErrOpPermissionFailed, ""), nil)
  81. c.Abort()
  82. return
  83. }
  84. if !hasPermis {
  85. core.WriteResponse(c, errors.WithCode(codex.ErrOperationForbideen, ""), nil)
  86. c.Abort()
  87. return
  88. }
  89. c.Next()
  90. }
  91. }
  92. func Verification(c *gin.Context, token string) (info global.TokenInfo, err error) {
  93. ok, pid, user, err := clod.NewBzdClodService().LoginVerification(c, token)
  94. if err != nil {
  95. return info, err
  96. }
  97. if !ok {
  98. return info, errors.New("验证失败,请重新登陆")
  99. }
  100. accInfo := models.Account{}
  101. store := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB)
  102. err = store.InTx(c, func(ctx context.Context) error {
  103. accInfo, err = store.Account().FindByUuid(c, user.T_uuid)
  104. if err != nil {
  105. return err
  106. }
  107. accInfo.UsePid = user.T_pid
  108. accInfo.PowerId = user.T_power
  109. accInfo.Phone = user.T_phone
  110. accInfo.Name = user.T_name
  111. if pid == 0 {
  112. accInfo.UsePid = pid
  113. }
  114. if accInfo.Id == 0 {
  115. accInfo.Uuid = user.T_uuid
  116. accInfo.Pid = user.T_pid
  117. accInfo.AccountType = constant.AccountPlatform
  118. accInfo.FirstLogin = constant.NO
  119. if err = store.Account().Create(c, &accInfo); err != nil {
  120. return err
  121. }
  122. } else {
  123. if err = store.Account().UpdateById(c, &accInfo); err != nil {
  124. return err
  125. }
  126. }
  127. return nil
  128. })
  129. if err != nil {
  130. return info, err
  131. }
  132. info = global.TokenInfo{
  133. AccountId: accInfo.Id,
  134. AccountUuid: accInfo.Uuid,
  135. AccountType: constant.AccountPlatform,
  136. Pid: accInfo.Pid,
  137. Name: accInfo.Name,
  138. Phone: accInfo.Phone,
  139. PowerId: accInfo.PowerId,
  140. UsePid: accInfo.UsePid,
  141. }
  142. return info, nil
  143. }