Market.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\FactoryContext;
  5. use app\common\library\ps\MarketBuyer;
  6. use app\common\library\ps\MarketPublisher;
  7. use app\common\model\ps\MarketGoods;
  8. use app\common\model\ps\MarketOrder;
  9. use think\Db;
  10. /**
  11. * 模板商城
  12. */
  13. class Market extends Api
  14. {
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. if (!FactoryContext::id()) {
  20. $this->error('账号未绑定工厂');
  21. }
  22. }
  23. /**
  24. * 在售商品
  25. */
  26. public function index()
  27. {
  28. $limit = $this->request->request('limit/d', 12);
  29. $keyword = $this->request->request('keyword', '', 'trim');
  30. $query = (new MarketGoods())->where('status', 'on_sale')->order('id desc');
  31. if ($keyword !== '') {
  32. $query->where('title|category|intro', 'like', '%' . $keyword . '%');
  33. }
  34. $list = $query->paginate($limit);
  35. $factoryId = FactoryContext::id();
  36. $items = $list->items();
  37. $sellerIds = [];
  38. foreach ($items as $item) {
  39. $sellerIds[] = (int)(is_array($item) ? $item['factory_id'] : $item['factory_id']);
  40. }
  41. $sellerIds = array_values(array_unique(array_filter($sellerIds)));
  42. $sellerMap = [];
  43. if ($sellerIds) {
  44. $factories = Db::name('ps_factory')->where('id', 'in', $sellerIds)->column('name', 'id');
  45. $sellerMap = $factories ?: [];
  46. }
  47. $rows = [];
  48. foreach ($items as $item) {
  49. $row = $this->formatGoods($item, $factoryId, $sellerMap);
  50. $rows[] = $row;
  51. }
  52. $this->success('', ['total' => $list->total(), 'rows' => $rows]);
  53. }
  54. /**
  55. * 商品详情
  56. */
  57. public function info()
  58. {
  59. $id = $this->request->request('id/d');
  60. $row = MarketGoods::get($id);
  61. if (!$row || $row['status'] !== 'on_sale') {
  62. $this->error(__('No Results were found'));
  63. }
  64. $info = $this->formatGoods($row, FactoryContext::id());
  65. $this->success('', $info);
  66. }
  67. /**
  68. * 上架或更新本厂模板在商城的价格
  69. */
  70. public function publish()
  71. {
  72. $templateId = $this->request->post('template_id/d');
  73. $priceSingle = $this->request->post('price_single/d', 0);
  74. $priceBuyout = $this->request->post('price_buyout/d', 0);
  75. $title = $this->request->post('title', '', 'trim');
  76. $intro = $this->request->post('intro', '', 'trim');
  77. if (!$templateId) {
  78. $this->error(__('Invalid parameters'));
  79. }
  80. try {
  81. $goods = MarketPublisher::publish($templateId, $priceSingle, $priceBuyout, $title, $intro);
  82. } catch (\Exception $e) {
  83. $this->error($e->getMessage());
  84. }
  85. $this->success('已上架,其他工厂可在商城看到', $this->formatGoods($goods, FactoryContext::id()));
  86. }
  87. /**
  88. * 下架本厂商品
  89. */
  90. public function unpublish()
  91. {
  92. $goodsId = $this->request->post('goods_id/d');
  93. if (!$goodsId) {
  94. $this->error(__('Invalid parameters'));
  95. }
  96. try {
  97. $goods = MarketPublisher::unpublish($goodsId);
  98. } catch (\Exception $e) {
  99. $this->error($e->getMessage());
  100. }
  101. $this->success('已下架', $this->formatGoods($goods, FactoryContext::id()));
  102. }
  103. /**
  104. * 本厂上架记录
  105. */
  106. public function mine()
  107. {
  108. $limit = $this->request->request('limit/d', 20);
  109. $list = (new MarketGoods())
  110. ->where('factory_id', FactoryContext::id())
  111. ->order('id desc')
  112. ->paginate($limit);
  113. $factoryId = FactoryContext::id();
  114. $rows = [];
  115. foreach ($list->items() as $item) {
  116. $rows[] = $this->formatGoods($item, $factoryId);
  117. }
  118. $this->success('', ['total' => $list->total(), 'rows' => $rows]);
  119. }
  120. /**
  121. * 购买并导入本厂模板库。本期用工厂积分,不走支付渠道。
  122. */
  123. public function buy()
  124. {
  125. $goodsId = $this->request->post('goods_id/d');
  126. $license = $this->request->post('license_type', 'single');
  127. if (!$goodsId) {
  128. $this->error(__('Invalid parameters'));
  129. }
  130. try {
  131. $data = MarketBuyer::buy($goodsId, $license, $this->auth->id);
  132. } catch (\Exception $e) {
  133. $this->error($e->getMessage());
  134. }
  135. $this->success('已导入本厂模板库', $data);
  136. }
  137. /**
  138. * 本厂购买记录
  139. */
  140. public function orders()
  141. {
  142. $limit = $this->request->request('limit/d', 10);
  143. $list = (new MarketOrder())
  144. ->where('buyer_factory_id', FactoryContext::id())
  145. ->order('id desc')
  146. ->paginate($limit);
  147. $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
  148. }
  149. /**
  150. * @param mixed $item
  151. * @param int $factoryId
  152. * @param array $sellerMap
  153. * @return array
  154. */
  155. protected function formatGoods($item, $factoryId, array $sellerMap = [])
  156. {
  157. $row = is_array($item) ? $item : $item->toArray();
  158. $sellerId = (int)$row['factory_id'];
  159. $row['is_own'] = $sellerId === (int)$factoryId;
  160. $row['is_official'] = $sellerId === 0;
  161. if ($row['is_official']) {
  162. $row['seller_name'] = '官方';
  163. } elseif (isset($sellerMap[$sellerId])) {
  164. $row['seller_name'] = $sellerMap[$sellerId];
  165. } elseif ($row['is_own']) {
  166. $row['seller_name'] = '本厂';
  167. } else {
  168. $name = Db::name('ps_factory')->where('id', $sellerId)->value('name');
  169. $row['seller_name'] = $name ?: ('工厂#' . $sellerId);
  170. }
  171. $cover = isset($row['cover']) ? trim((string)$row['cover']) : '';
  172. if ($cover === '') {
  173. $row['cover_url'] = '';
  174. } elseif (preg_match('#^(https?:)?//#i', $cover) || strpos($cover, 'data:') === 0) {
  175. $row['cover_url'] = $cover;
  176. } else {
  177. $row['cover_url'] = '/' . ltrim(str_replace('\\', '/', $cover), '/');
  178. }
  179. return $row;
  180. }
  181. }