diff --git a/app/data/controller/api/Goods.php b/app/data/controller/api/Goods.php index 72760ff27..5cb95db87 100644 --- a/app/data/controller/api/Goods.php +++ b/app/data/controller/api/Goods.php @@ -39,10 +39,10 @@ class Goods extends Controller */ public function getGoods() { - $query = $this->_query('ShopGoods')->like('name')->equal('cate'); - $query->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc'); - $result = $query->page(true, false, false, 10); - // @todo 处理商品列表 + $map = ['deleted' => 0, 'status' => 1]; + $query = $this->_query('ShopGoods')->like('name,mark')->equal('code,cate'); + $result = $query->where($map)->order('sort desc,id desc')->page(true, false, false, 10); + GoodsService::instance()->buildItemData($result['list']); $this->success('获取商品数据成功', $result); } diff --git a/app/data/service/GoodsService.php b/app/data/service/GoodsService.php index 121eac854..bcf0285ac 100644 --- a/app/data/service/GoodsService.php +++ b/app/data/service/GoodsService.php @@ -81,6 +81,30 @@ class GoodsService extends Service return $query->where($map)->order('sort desc,id desc')->column('name'); } + /** + * 商品数据绑定 + * @param array $list + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function buildItemData(array &$list = []): array + { + $codes = array_unique(array_column($list, 'code')); + $map = [['goods_code', 'in', $codes], ['status', '=', 1]]; + $items = $this->app->db->name('ShopGoodsItem')->where($map)->select()->toArray(); + foreach ($list as &$vo) { + $vo['items'] = []; + foreach ($items as $item) { + if ($item['goods_code'] === $vo['code']) { + $vo['items'][] = $item; + } + } + } + return $list; + } + /** * 最大分类级别 * @return integer