error('账号未绑定工厂'); } } /** * 在售商品 */ public function index() { $limit = $this->request->request('limit/d', 12); $keyword = $this->request->request('keyword', '', 'trim'); $query = (new MarketGoods())->where('status', 'on_sale')->order('id desc'); if ($keyword !== '') { $query->where('title|category|intro', 'like', '%' . $keyword . '%'); } $list = $query->paginate($limit); $factoryId = FactoryContext::id(); $items = $list->items(); $sellerIds = []; foreach ($items as $item) { $sellerIds[] = (int)(is_array($item) ? $item['factory_id'] : $item['factory_id']); } $sellerIds = array_values(array_unique(array_filter($sellerIds))); $sellerMap = []; if ($sellerIds) { $factories = Db::name('ps_factory')->where('id', 'in', $sellerIds)->column('name', 'id'); $sellerMap = $factories ?: []; } $rows = []; foreach ($items as $item) { $row = $this->formatGoods($item, $factoryId, $sellerMap); $rows[] = $row; } $this->success('', ['total' => $list->total(), 'rows' => $rows]); } /** * 商品详情 */ public function info() { $id = $this->request->request('id/d'); $row = MarketGoods::get($id); if (!$row || $row['status'] !== 'on_sale') { $this->error(__('No Results were found')); } $info = $this->formatGoods($row, FactoryContext::id()); $this->success('', $info); } /** * 上架或更新本厂模板在商城的价格 */ public function publish() { $templateId = $this->request->post('template_id/d'); $priceSingle = $this->request->post('price_single/d', 0); $priceBuyout = $this->request->post('price_buyout/d', 0); $title = $this->request->post('title', '', 'trim'); $intro = $this->request->post('intro', '', 'trim'); if (!$templateId) { $this->error(__('Invalid parameters')); } try { $goods = MarketPublisher::publish($templateId, $priceSingle, $priceBuyout, $title, $intro); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->success('已上架,其他工厂可在商城看到', $this->formatGoods($goods, FactoryContext::id())); } /** * 下架本厂商品 */ public function unpublish() { $goodsId = $this->request->post('goods_id/d'); if (!$goodsId) { $this->error(__('Invalid parameters')); } try { $goods = MarketPublisher::unpublish($goodsId); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->success('已下架', $this->formatGoods($goods, FactoryContext::id())); } /** * 本厂上架记录 */ public function mine() { $limit = $this->request->request('limit/d', 20); $list = (new MarketGoods()) ->where('factory_id', FactoryContext::id()) ->order('id desc') ->paginate($limit); $factoryId = FactoryContext::id(); $rows = []; foreach ($list->items() as $item) { $rows[] = $this->formatGoods($item, $factoryId); } $this->success('', ['total' => $list->total(), 'rows' => $rows]); } /** * 购买并导入本厂模板库。本期用工厂积分,不走支付渠道。 */ public function buy() { $goodsId = $this->request->post('goods_id/d'); $license = $this->request->post('license_type', 'single'); if (!$goodsId) { $this->error(__('Invalid parameters')); } try { $data = MarketBuyer::buy($goodsId, $license, $this->auth->id); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->success('已导入本厂模板库', $data); } /** * 本厂购买记录 */ public function orders() { $limit = $this->request->request('limit/d', 10); $list = (new MarketOrder()) ->where('buyer_factory_id', FactoryContext::id()) ->order('id desc') ->paginate($limit); $this->success('', ['total' => $list->total(), 'rows' => $list->items()]); } /** * @param mixed $item * @param int $factoryId * @param array $sellerMap * @return array */ protected function formatGoods($item, $factoryId, array $sellerMap = []) { $row = is_array($item) ? $item : $item->toArray(); $sellerId = (int)$row['factory_id']; $row['is_own'] = $sellerId === (int)$factoryId; $row['is_official'] = $sellerId === 0; if ($row['is_official']) { $row['seller_name'] = '官方'; } elseif (isset($sellerMap[$sellerId])) { $row['seller_name'] = $sellerMap[$sellerId]; } elseif ($row['is_own']) { $row['seller_name'] = '本厂'; } else { $name = Db::name('ps_factory')->where('id', $sellerId)->value('name'); $row['seller_name'] = $name ?: ('工厂#' . $sellerId); } $cover = isset($row['cover']) ? trim((string)$row['cover']) : ''; if ($cover === '') { $row['cover_url'] = ''; } elseif (preg_match('#^(https?:)?//#i', $cover) || strpos($cover, 'data:') === 0) { $row['cover_url'] = $cover; } else { $row['cover_url'] = '/' . ltrim(str_replace('\\', '/', $cover), '/'); } return $row; } }