Market.php 6.6 KB

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