productDetailLogic.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package product
  2. import (
  3. "context"
  4. "perms-system-server/internal/middleware"
  5. "perms-system-server/internal/response"
  6. "perms-system-server/internal/svc"
  7. "perms-system-server/internal/types"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type ProductDetailLogic struct {
  11. logx.Logger
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. }
  15. func NewProductDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductDetailLogic {
  16. return &ProductDetailLogic{
  17. Logger: logx.WithContext(ctx),
  18. ctx: ctx,
  19. svcCtx: svcCtx,
  20. }
  21. }
  22. // ProductDetail 产品详情。超管可查任何产品的完整信息(含 appKey);非超管只能查自己所属的产品,
  23. // 对其他产品一律返回 404,避免将"存在但无权"和"不存在"区分开后被用作枚举 oracle(见审计 M-2)。
  24. func (l *ProductDetailLogic) ProductDetail(req *types.ProductDetailReq) (resp *types.ProductItem, err error) {
  25. caller := middleware.GetUserDetails(l.ctx)
  26. if caller == nil {
  27. return nil, response.ErrUnauthorized("未登录")
  28. }
  29. product, err := l.svcCtx.SysProductModel.FindOne(l.ctx, req.Id)
  30. if err != nil {
  31. return nil, response.ErrNotFound("产品不存在")
  32. }
  33. if !caller.IsSuperAdmin && product.Code != caller.ProductCode {
  34. return nil, response.ErrNotFound("产品不存在")
  35. }
  36. item := &types.ProductItem{
  37. Id: product.Id,
  38. Code: product.Code,
  39. Name: product.Name,
  40. Remark: product.Remark,
  41. Status: product.Status,
  42. CreateTime: product.CreateTime,
  43. }
  44. if caller.IsSuperAdmin {
  45. item.AppKey = product.AppKey
  46. }
  47. return item, nil
  48. }