material.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/tasker/httptasker"
  7. "git.getensh.com/common/gopkgs/util"
  8. "github.com/gin-gonic/gin"
  9. "go.uber.org/zap"
  10. "net/http"
  11. "property-system-gateway/errors"
  12. param_v1 "property-system-gateway/param/v1"
  13. "property-system-gateway/pb"
  14. "property-system-gateway/pb/v1"
  15. "strconv"
  16. "strings"
  17. )
  18. //
  19. // @Summary 获取单个素材
  20. // @Description 获取单个素材
  21. // @Tags 素材
  22. // @Accept json
  23. // @Produce json
  24. // @Param token header string true " "
  25. // @Param mtype query int true "素材类型"
  26. // @Success 200 {object} v1.MaterialInfoResponse
  27. // @Failure 500 {object} base.HTTPError
  28. // @Router /api/v1/material/info [get]
  29. func (c *Controller) MaterialInfo(ctx *gin.Context) {
  30. // 解析参数
  31. req := &param_v1.MaterialInfoRequest{}
  32. parseParamTask := func() error {
  33. err := util.ShouldBind(ctx, &req.Header, nil, &req.MaterialInfoQuery, nil)
  34. if err != nil {
  35. logger.Error("func",
  36. zap.String("call", "util.ShouldBind"),
  37. zap.String("error", err.Error()))
  38. return errors.ParamsError
  39. }
  40. return nil
  41. }
  42. // 业务处理
  43. handleServiceTask := func() error {
  44. //tokenInfo, err := utils.GetJwtTokenFromCtx(ctx)
  45. //if err != nil {
  46. // return err
  47. //}
  48. // 响应数据
  49. resp := param_v1.MaterialInfoResponse{}
  50. rpcReq := &v1.MaterialInfoRequest{
  51. Mtype: req.Mtype,
  52. }
  53. rpcRsp, err := pb.Common.MaterialInfo(ctx, rpcReq)
  54. if err != nil {
  55. s, _ := json.MarshalToString(req)
  56. logger.Error("func",
  57. zap.String("call", "pb.Common.MaterialInfo"),
  58. zap.String("params", s),
  59. zap.String("error", err.Error()))
  60. return errors.ErrorTransForm(err)
  61. }
  62. resp.Data = *rpcRsp
  63. ctx.JSON(http.StatusOK, resp)
  64. return nil
  65. }
  66. // 执行任务
  67. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  68. }
  69. //
  70. // @Summary 根据类型获取素材列表
  71. // @Description 根据类型获取素材列表
  72. // @Tags 素材
  73. // @Accept json
  74. // @Produce json
  75. // @Param token header string true " "
  76. // @Param mtypes query string true "类型,多个类型以逗号分隔"
  77. // @Success 200 {object} v1.MaterialListResponse
  78. // @Failure 500 {object} base.HTTPError
  79. // @Router /api/v1/material/list [get]
  80. func (c *Controller) MaterialList(ctx *gin.Context) {
  81. // 解析参数
  82. req := &param_v1.MaterialListRequest{}
  83. parseParamTask := func() error {
  84. err := util.ShouldBind(ctx, &req.Header, nil, &req.MaterialListQuery, nil)
  85. if err != nil {
  86. logger.Error("func",
  87. zap.String("call", "util.ShouldBind"),
  88. zap.String("error", err.Error()))
  89. return errors.ParamsError
  90. }
  91. return nil
  92. }
  93. // 业务处理
  94. handleServiceTask := func() error {
  95. // 响应数据
  96. resp := param_v1.MaterialListResponse{}
  97. array := strings.Split(req.Mtypes, ",")
  98. if len(array) == 0 {
  99. return errors.ParamsError
  100. }
  101. mtypes := make([]int32, len(array))
  102. for i, v := range array {
  103. tmp, _ := strconv.Atoi(v)
  104. mtypes[i] = int32(tmp)
  105. }
  106. rpcReq := &v1.MaterialListRequest{
  107. Mtypes: mtypes,
  108. Page: -1,
  109. PageSize: -1,
  110. }
  111. rpcRsp, err := pb.Common.MaterialList(ctx, rpcReq)
  112. if err != nil {
  113. s, _ := json.MarshalToString(req)
  114. logger.Error("func",
  115. zap.String("call", "pb.Common.MaterialList"),
  116. zap.String("params", s),
  117. zap.String("error", err.Error()))
  118. return errors.ErrorTransForm(err)
  119. }
  120. if rpcRsp.List == nil {
  121. rpcRsp.List = make([]*v1.MaterialItem, 0)
  122. }
  123. resp.Data = *rpcRsp
  124. ctx.JSON(http.StatusOK, resp)
  125. return nil
  126. }
  127. // 执行任务
  128. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  129. }