ProductService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\MicroApi\Services;
  3. use App\MicroApi\Exceptions\RpcException;
  4. use App\MicroApi\Facades\HttpClient;
  5. use App\MicroApi\Items\ProductItem;
  6. use Illuminate\Support\Facades\Log;
  7. class ProductService
  8. {
  9. use DataHandler;
  10. protected $servicePrefix = '/product/productService';
  11. /**
  12. * @param $product
  13. * @return ProductItem
  14. * @throws RpcException
  15. */
  16. public function create(ProductItem $product)
  17. {
  18. $path = $this->servicePrefix . '/create';
  19. $options = ['json' => $product];
  20. try {
  21. $response = HttpClient::post($path, $options);
  22. } catch (\Exception $exception) {
  23. Log::error("MicroApi.ProductService.Create Call Failed: " . $exception->getMessage());
  24. throw new RpcException("调用远程服务失败");
  25. }
  26. $result = $this->decode($response->getBody()->getContents());
  27. return isset($result->product) ? $product->fillAttributes($result->product) : null;
  28. }
  29. /**
  30. * 获取商品集合
  31. * @return \Illuminate\Support\Collection|null
  32. * @throws RpcException
  33. */
  34. public function getAll()
  35. {
  36. $path = $this->servicePrefix . '/getAll';
  37. try {
  38. $response = HttpClient::get($path);
  39. } catch (\Exception $exception) {
  40. Log::error("MicroApi.ProductService.GetAll Call Failed: " . $exception->getMessage());
  41. throw new RpcException("调用远程服务失败");
  42. }
  43. $result = $this->decode($response->getBody()->getContents());
  44. // 返回商品集合
  45. return isset($result->products) ? collect($result->products)->map(function ($item) {
  46. $product = new ProductItem();
  47. return $product->fillAttributes($item);
  48. })->reject(function ($item){
  49. return empty($item);
  50. }) : null;
  51. }
  52. /**
  53. * 根据商品 ID 获取商品信息
  54. * @param $id
  55. * @param $withRelations
  56. * @return ProductItem|null
  57. * @throws RpcException
  58. */
  59. public function getById($id, $withRelations = false)
  60. {
  61. if ($withRelations == false) {
  62. $path = $this->servicePrefix . '/get';
  63. } else {
  64. $path = $this->servicePrefix . '/getDetail';
  65. }
  66. $product = new ProductItem();
  67. $product->id = $id;
  68. $options = ['json' => $product];
  69. try {
  70. $response = HttpClient::post($path, $options);
  71. } catch (\Exception $exception) {
  72. Log::error("MicroApi.ProductService.Get Call Failed: " . $exception->getMessage());
  73. throw new RpcException("调用远程服务失败");
  74. }
  75. $result = $this->decode($response->getBody()->getContents());
  76. return isset($result->product) ? $product->fillAttributes($result->product) : null;
  77. }
  78. /**
  79. * 通过商品别名获取明细
  80. * @param $slug
  81. * @return ProductItem|null
  82. * @throws RpcException
  83. */
  84. public function getBySlug($slug)
  85. {
  86. $path = $this->servicePrefix . '/get';
  87. $product = new ProductItem();
  88. $product->slug = $slug;
  89. $options = ['json' => $product];
  90. try {
  91. $response = HttpClient::post($path, $options);
  92. } catch (\Exception $exception) {
  93. Log::error("MicroApi.ProductService.Get Call Failed: " . $exception->getMessage());
  94. throw new RpcException("调用远程服务失败");
  95. }
  96. $result = $this->decode($response->getBody()->getContents());
  97. return isset($result->user) ? $product->fillAttributes($product) : null;
  98. }
  99. /**
  100. * 更新商品信息
  101. * @param ProductItem $product
  102. * @return ProductItem
  103. * @throws RpcException
  104. */
  105. public function update(ProductItem $product)
  106. {
  107. $path = $this->servicePrefix . '/update';
  108. $options = ['json' => $product];
  109. try {
  110. $response = HttpClient::post($path, $options);
  111. } catch (\Exception $exception) {
  112. Log::error("MicroApi.ProductService.Update Call Failed: " . $exception->getMessage());
  113. throw new RpcException("调用远程服务失败");
  114. }
  115. $result = $this->decode($response->getBody()->getContents());
  116. return $product->fillAttributes($result->product);
  117. }
  118. /**
  119. * 删除商品信息
  120. * @param $productId
  121. * @return bool
  122. * @throws RpcException
  123. */
  124. public function delete($productId)
  125. {
  126. $path = $this->servicePrefix . '/delete';
  127. $options = ['json' => ['id' => $productId]];
  128. try {
  129. $response = HttpClient::post($path, $options);
  130. } catch (\Exception $exception) {
  131. Log::error("MicroApi.ProductService.Delete Call Failed: " . $exception->getMessage());
  132. throw new RpcException("调用远程服务失败");
  133. }
  134. $result = $this->decode($response->getBody()->getContents());
  135. return empty($result->product) ? true : false;
  136. }
  137. }