1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
 
namespace app\branch\controller\data;
 
use app\branch\controller\Controller;
use app\shop\model\product\Product as ProductModel;
use app\shop\model\product\Category as CategoryModel;
use app\shop\service\ProductService;
 
/**
 * 商品数据控制器
 */
class Product extends Controller
{
    /**
     * 商品列表
     */
    public function lists()
    {
        $postData = $this->postData();
        // 商品分类
        $category = CategoryModel::getCacheTree();
        $model = new ProductModel;
        $list = $model->getList($postData);
        foreach ($list as &$item) {
            $item['specList'] = $this->transSpecData(ProductService::getSpecData($item));
        }
        return $this->renderSuccess('', compact('list', 'category'));
    }
 
    /**
     * 商品规格列表,组装成列表
     */
    public function spec($product_id)
    {
        $model = ProductModel::detail($product_id);
        $specData = ProductService::getSpecData($model);
        $specList = $this->transSpecData($specData);
        return $this->renderSuccess('', compact('specList'));
    }
 
    /**
     * 组装前端用的数据
     */
    private function transSpecData($specData){
        if (!$specData) {
            return [];
        }
        $specList = [];
        foreach($specData['spec_list'] as $spec){
            $specIds = explode('_',$spec['spec_sku_id']);
            $spec['spec_name'] = '';
            foreach ($specIds as $specId){
                $spec['spec_name'] .= $this->searchSpecItem($specData['spec_attr'], $specId) . ';';
            }
            array_push($specList, $spec);
        }
        return $specList;
    }
 
    /**
     * 规格值
     */
    private function searchSpecItem($spec_attr, $item_id){
        $specValue = '';
        foreach ($spec_attr as $attr){
            foreach ($attr['spec_items'] as $item){
                if($item['item_id'] == $item_id){
                    $specValue = $attr['group_name'] . ',' . $item['spec_value'];
                    break 2;
                }
            }
        }
        return $specValue;
    }
}