produdct.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package controllers
  2. import (
  3. "sparrow/pkg/models"
  4. "sparrow/pkg/rpcs"
  5. "sparrow/pkg/server"
  6. "sparrow/services/knowoapi/services"
  7. "strings"
  8. "github.com/kataras/iris"
  9. opentracing "github.com/opentracing/opentracing-go"
  10. "github.com/opentracing/opentracing-go/ext"
  11. )
  12. // ProductController 产品API
  13. type ProductController struct {
  14. Ctx iris.Context
  15. Service services.ProductService
  16. Token Token
  17. }
  18. // Post /product 添加产品
  19. func (a *ProductController) Post() {
  20. product := new(models.Product)
  21. if err := parseBody(a.Ctx, product); err != nil {
  22. badRequest(a.Ctx, err)
  23. return
  24. }
  25. product.VendorID = int32(a.Token.getVendorID(a.Ctx))
  26. // if upload a image file
  27. if product.ProductImage != "" {
  28. //move image
  29. args := &rpcs.ArgsMoveFile{
  30. Source: product.ProductImage,
  31. Target: "product",
  32. }
  33. args.SpanCarrier = make(map[string]string)
  34. reply := &rpcs.ReplyMoveFile{}
  35. span := opentracing.StartSpan(a.Ctx.Path())
  36. defer span.Finish()
  37. ext.SpanKindRPCClient.Set(span)
  38. ext.HTTPMethod.Set(span, a.Ctx.Method())
  39. span.Tracer().Inject(
  40. span.Context(),
  41. opentracing.TextMap,
  42. opentracing.TextMapCarrier(args.SpanCarrier),
  43. )
  44. err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
  45. if err != nil {
  46. server.Log.Error(err)
  47. product.ProductImage = ""
  48. } else {
  49. product.ProductImage = reply.FilePath
  50. }
  51. }
  52. err := a.Service.Create(product)
  53. if err != nil {
  54. responseError(a.Ctx, ErrDatabase, err.Error())
  55. return
  56. }
  57. done(a.Ctx, product)
  58. }
  59. // Delete /product 删除
  60. func (a *ProductController) Delete() {
  61. product := new(models.Product)
  62. if err := parseBody(a.Ctx, product); err != nil {
  63. badRequest(a.Ctx, err)
  64. return
  65. }
  66. if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
  67. responseError(a.Ctx, ErrNormal, "非法操作")
  68. return
  69. }
  70. err := a.Service.Delete(product)
  71. if err != nil {
  72. responseError(a.Ctx, ErrDatabase, err.Error())
  73. return
  74. }
  75. done(a.Ctx, "删除成功")
  76. }
  77. // Put /produdct 更新产品信息
  78. func (a *ProductController) Put() {
  79. product := new(models.Product)
  80. if err := parseBody(a.Ctx, product); err != nil {
  81. badRequest(a.Ctx, err)
  82. return
  83. }
  84. if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
  85. responseError(a.Ctx, ErrNormal, "非法操作")
  86. return
  87. }
  88. // check update image
  89. if strings.Contains(product.ProductImage, "tmp") {
  90. //move image
  91. args := &rpcs.ArgsMoveFile{
  92. Source: product.ProductImage,
  93. Target: "product",
  94. }
  95. reply := &rpcs.ReplyMoveFile{}
  96. err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
  97. if err != nil {
  98. server.Log.Error(err)
  99. product.ProductImage = ""
  100. } else {
  101. product.ProductImage = reply.FilePath
  102. }
  103. }
  104. if product.ProductImage == "" {
  105. old, _ := a.Service.QueryOne(a.Token.getVendorID(a.Ctx), product.ProductKey)
  106. arg := &rpcs.ArgsDeleteFile{
  107. FileName: old.ProductImage,
  108. }
  109. reply := &rpcs.ReplyEmptyResult{}
  110. server.RPCCallByName("fileaccess", "FileAccess.DeleteFile", arg, reply)
  111. }
  112. pro, err := a.Service.Update(product)
  113. if err != nil {
  114. responseError(a.Ctx, ErrDatabase, err.Error())
  115. return
  116. }
  117. done(a.Ctx, pro)
  118. }
  119. // Get /product 查询我的产品
  120. func (a *ProductController) Get() {
  121. pi, err := a.Ctx.URLParamInt("pi")
  122. if err != nil {
  123. badRequest(a.Ctx, err)
  124. return
  125. }
  126. ps, err := a.Ctx.URLParamInt("ps")
  127. if err != nil {
  128. badRequest(a.Ctx, err)
  129. return
  130. }
  131. name := a.Ctx.URLParam("name")
  132. ds, total, err := a.Service.GetVendorProducts(a.Token.getVendorID(a.Ctx),
  133. pi,
  134. ps,
  135. name)
  136. if err != nil {
  137. responseError(a.Ctx, ErrDatabase, err.Error())
  138. return
  139. }
  140. done(a.Ctx, map[string]interface{}{
  141. "list": ds,
  142. "total": total,
  143. })
  144. }
  145. // GetBy 获取产品信息
  146. // GET /product/{key}
  147. func (a *ProductController) GetBy(key string) {
  148. pro, err := a.Service.QueryOne(a.Token.getVendorID(a.Ctx), key)
  149. if err != nil {
  150. responseError(a.Ctx, ErrDatabase, err.Error())
  151. return
  152. }
  153. done(a.Ctx, pro)
  154. }