product.go 3.4 KB

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