checkpowerlogic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package logic
  2. import (
  3. "context"
  4. "time"
  5. . "app.yhyue.com/moapp/jybase/date"
  6. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/entity"
  7. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/svc"
  8. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type CheckPowerLogic struct {
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. logx.Logger
  15. }
  16. func NewCheckPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckPowerLogic {
  17. return &CheckPowerLogic{
  18. ctx: ctx,
  19. svcCtx: svcCtx,
  20. Logger: logx.WithContext(ctx),
  21. }
  22. }
  23. func (l *CheckPowerLogic) CheckPower(in *pb.CheckPowerReq) (*pb.Resp, error) {
  24. resp := &pb.Resp{}
  25. function := Base_function.FindByCode(in.Appid, in.FunctionCode)
  26. if function == nil {
  27. l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "功能原始表中没有找到该功能")
  28. return resp, nil
  29. }
  30. myPowers := Base_power.FindMyPower(in.Appid, in.FunctionCode, in.EntId, in.UserId)
  31. if myPowers == nil || len(*myPowers) == 0 {
  32. l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "功能权益表中没有找到权益记录")
  33. return resp, nil
  34. }
  35. isPass := false
  36. for _, v := range *myPowers {
  37. start, err := time.ParseInLocation(Date_Full_Layout, v.Start_time, time.Local)
  38. if err != nil {
  39. l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "开始时间转化错误", err)
  40. return resp, nil
  41. }
  42. end, err := time.ParseInLocation(Date_Full_Layout, v.End_time, time.Local)
  43. if err != nil {
  44. l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "结束时间转化错误", err)
  45. return resp, nil
  46. }
  47. if time.Now().After(start) && time.Now().Before(end) {
  48. isPass = true
  49. }
  50. }
  51. if !isPass {
  52. resp.Status = -1
  53. l.Info(in.Appid, in.FunctionCode, in.EntId, in.UserId, "不在有效期内")
  54. return resp, nil
  55. }
  56. if function.Power_rule == 1 {
  57. resp.Status = 1
  58. return resp, nil
  59. } else {
  60. if function.Power_rule == 3 {
  61. hasEmpower := false
  62. if (*myPowers)[0].Power_type == 2 {
  63. empower := Base_ent_empower.FindMyEntId(in.Appid, in.FunctionCode, in.EntId)
  64. if empower == nil || len(*empower) == 0 {
  65. resp.Status = -3
  66. l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "没有授权记录")
  67. return resp, nil
  68. }
  69. if len(*empower) == 1 {
  70. if (*empower)[0].Ent_user_id == 0 {
  71. hasEmpower = true
  72. }
  73. } else {
  74. for _, v := range *empower {
  75. if v.Ent_user_id == in.UserId {
  76. hasEmpower = true
  77. break
  78. }
  79. }
  80. }
  81. } else {
  82. hasEmpower = true
  83. }
  84. if !hasEmpower {
  85. resp.Status = -3
  86. l.Info(in.Appid, in.FunctionCode, in.EntId, in.UserId, "没有对该用户进行授权")
  87. return resp, nil
  88. }
  89. use, err := Base_resource_use.FindLastOneByEntId(in.Appid, in.FunctionCode, in.EntId)
  90. if err != nil {
  91. l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "查询资源使用记录失败")
  92. return resp, nil
  93. } else if use == nil {
  94. resp.Status = 1
  95. return resp, nil
  96. }
  97. //limit_strategy := (*myPowers)[0].Limit_strategy
  98. }
  99. return resp, nil
  100. }
  101. return resp, nil
  102. }