product.go 3.7 KB

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