diff --git a/app/data/controller/api/Goods.php b/app/data/controller/api/Goods.php index 5cb95db87..26f27dcf6 100644 --- a/app/data/controller/api/Goods.php +++ b/app/data/controller/api/Goods.php @@ -39,6 +39,11 @@ class Goods extends Controller */ public function getGoods() { + if ($code = input('code', null)) { + $this->app->db->name('ShopGoods')->where(['code' => $code])->update([ + 'num_read' => $this->app->db->raw('num_read+1'), + ]); + } $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); diff --git a/app/data/service/GoodsService.php b/app/data/service/GoodsService.php index bcf0285ac..44c25db1d 100644 --- a/app/data/service/GoodsService.php +++ b/app/data/service/GoodsService.php @@ -81,9 +81,25 @@ class GoodsService extends Service return $query->where($map)->order('sort desc,id desc')->column('name'); } + /** + * 一维数组生成数据树 + * @param array $list 待处理数据 + * @param string $cid 自己的主键 + * @param string $pid 上级的主键 + * @param string $sub 子数组名称 + * @return array + */ + public function arr2tree(array $list, string $cid = 'id', string $pid = 'pid', string $sub = 'sub'): array + { + [$tree, $tmp] = [[], array_combine(array_column($list, $cid), array_values($list))]; + foreach ($list as $vo) isset($vo[$pid]) && isset($tmp[$vo[$pid]]) ? $tmp[$vo[$pid]][$sub][] = &$tmp[$vo[$cid]] : $tree[] = &$tmp[$vo[$cid]]; + unset($tmp, $list); + return $tree; + } + /** * 商品数据绑定 - * @param array $list + * @param array $list 商品主数据 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException @@ -91,20 +107,28 @@ class GoodsService extends Service */ 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; - } - } + $cates = $this->app->db->name('ShopGoodsCate')->column('id,pid,name', 'id'); + foreach ($cates as $cate) if (isset($cates[$cate['pid']])) { + $cates[$cate['id']]['parent'] =& $cates[$cate['pid']]; } + $codes = array_unique(array_column($list, 'code')); + $where = [['goods_code', 'in', $codes], ['status', '=', 1]]; + $items = $this->app->db->name('ShopGoodsItem')->withoutField('id,status,create_at')->where($where)->select()->toArray(); + foreach ($list as &$vo) { + $vo['marks'] = think_string_to_array($vo['mark']); + $vo['cates'] = $cates[$vo['cate']] ?? []; + $vo['slider'] = explode('|', $vo['slider']); + $vo['specs'] = json_decode($vo['data_specs'], true); + $vo['items'] = []; + foreach ($items as $item) if ($item['goods_code'] === $vo['code']) $vo['items'][] = $item; + unset($vo['mark'], $vo['sort'], $vo['status'], $vo['deleted'], $vo['data_items'], $vo['data_specs']); + } + dump($list); + exit; return $list; } + /** * 最大分类级别 * @return integer diff --git a/app/data/sys.php b/app/data/sys.php index 4f6265d52..587bc4b74 100644 --- a/app/data/sys.php +++ b/app/data/sys.php @@ -1,11 +1,26 @@