Index.php 15.8 KB
Newer Older
_
__FresHmaN 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */

namespace fecshop\app\apphtml5\modules\Catalog\block\product;

use fecshop\app\apphtml5\modules\Catalog\helpers\Review as ReviewHelper;
use Yii;

/**
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class Index
{
    protected $_product;
    protected $_title;
    protected $_primaryVal;
T
Terry 已提交
24 25
    protected $_productSpuAttrArr;
    protected $_spuAttrShowAsImg;
_
__FresHmaN 已提交
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

    public function getLastData()
    {
        $productImgSize = Yii::$app->controller->module->params['productImgSize'];
        $productImgMagnifier = Yii::$app->controller->module->params['productImgMagnifier'];
        $this->initProduct();
        ReviewHelper::initReviewConfig();
        $ReviewAndStarCount = ReviewHelper::getReviewAndStarCount($this->_product);
        list($review_count, $reviw_rate_star_average) = $ReviewAndStarCount;

        return [
            'name'                    => Yii::$service->store->getStoreAttrVal($this->_product['name'], 'name'),
            'image'                    => $this->_product['image'],
            'sku'                    => $this->_product['sku'],
            'spu'                    => $this->_product['spu'],
            'attr_group'            => $this->_product['attr_group'],
            'review_count'            => $review_count,
            'reviw_rate_star_average'=> $reviw_rate_star_average,
            'price_info'            => $this->getProductPriceInfo(),
            'tier_price'            => $this->_product['tier_price'],
            'media_size' => [
                'small_img_width'    => $productImgSize['small_img_width'],
                'small_img_height'    => $productImgSize['small_img_height'],
                'middle_img_width'    => $productImgSize['middle_img_width'],
            ],
            'productImgMagnifier'    => $productImgMagnifier,
            'options'                => $this->getSameSpuInfo(),
            'custom_option'            => $this->_product['custom_option'],
            'description'            => Yii::$service->store->getStoreAttrVal($this->_product['description'], 'description'),
            '_id'                    => $this->_product['_id'],
            'buy_also_buy'            => $this->getProductBySkus($skus),
        ];
    }

60
    /**废弃
_
__FresHmaN 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
     * @property $data | Array 和当前产品的spu相同,但sku不同的产品  数组。
     * @property $current_size | String 当前产品的size值
     * @property $current_color | String 当前产品的颜色值
     * @return array 分别为
     *               $all_attr1 所有的颜色数组
     *               $all_attr2  所有的尺码数组
     *               $attr1_2_attr2 当前尺码下的所有颜色数组。
     *               $attr2_2_attr1 当前颜色下的所有尺码数组。
     */
    protected function getAttr1AndAttr2Info($data, $current_attr1, $current_attr2, $attr1, $attr2)
    {
        $all_attr1 = [];
        $all_attr2 = [];
        $attr1_2_attr2 = [];
        $attr2_2_attr1 = [];
        //var_dump($data);
        foreach ($data as $one) {
            $attr1_val = isset($one[$attr1]) ? $one[$attr1] : '';
            $attr2_val = isset($one[$attr2]) ? $one[$attr2] : '';
            //echo $attr1_val;
            //echo $size;
            //echo '##';
            $image = $one['image'];
            $name = $one['name'];
            $url_key = $one['url_key'];
            if ($attr1_val || $attr2_val) {
                if ($attr1_val) {
                    $all_attr1[$attr1_val] = [
                        'name'        => $name,
                        'image'    => $image,
                        'url_key'    => $url_key,
                    ];
                }
                if ($attr2_val) {
                    $all_attr2[$attr2_val] = [
                        'name'        => $name,
                        'image'    => $image,
                        'url_key'    => $url_key,
                    ];
                }
                //echo $attr2_val.'#'.$current_attr2;
                //echo '<br/>';
                if ($attr2_val && $current_attr2 == $attr2_val) {
                    $attr1_2_attr2[$attr1_val] = [
                        'name'        => $name,
                        'image'    => $image,
                        'url_key'    => $url_key,
                    ];
                }
                if ($attr1_val && $current_attr1 == $attr1_val) {
                    $attr2_2_attr1[$attr2_val] = [
                        'name'        => $name,
                        'image'    => $image,
                        'url_key'    => $url_key,
                    ];
                }
            }
        }

        return [$all_attr1, $all_attr2, $attr1_2_attr2, $attr2_2_attr1];
    }
T
Terry 已提交
122

T
Terry 已提交
123
    /**
124
     * @property $select | Array , 需要查询的字段。
T
Terry 已提交
125
     * 得到当前spu下面的所有的sku的数组、
126
     * 这个是为了产品详细页面的spu下面的产品切换,譬如同一spu下的不同的颜色尺码切换。
T
Terry 已提交
127 128 129 130
     */
    protected function getSpuData($select)
    {
        $spu = $this->_product['spu'];
T
Terry 已提交
131 132
        $select = array_merge($select, $this->_productSpuAttrArr);

T
Terry 已提交
133 134 135 136 137 138 139 140
        $filter = [
            'select'    => $select,
            'where'            => [
                ['spu' => $spu],
            ],
            'asArray' => true,
        ];
        $coll = Yii::$service->product->coll($filter);
T
Terry 已提交
141

T
Terry 已提交
142 143
        return $coll['coll'];
    }
T
Terry 已提交
144

_
__FresHmaN 已提交
145
    /**
146 147 148 149
     * @return Array得到spu下面的sku的spu属性的数据。用于在产品
     * 得到spu下面的sku的spu属性的数据。用于在产品详细页面显示其他的sku
     * 譬如页面:https://fecshop.appfront.fancyecommerce.com/raglan-sleeves-letter-printed-crew-neck-sweatshirt-53386451-77774122
     * 该spu的其他sku的颜色尺码也在这里显示出来。
_
__FresHmaN 已提交
150 151 152
     */
    protected function getSameSpuInfo()
    {
T
Terry 已提交
153
        // 当前的产品对应的spu属性组的属性,譬如 ['color','size','myyy']
T
Terry 已提交
154
        $this->_productSpuAttrArr = Yii::$service->product->getSpuAttr($this->_product['attr_group']);
T
Terry 已提交
155

T
Terry 已提交
156
        $this->_spuAttrShowAsImg = Yii::$service->product->getSpuImgAttr($this->_product['attr_group']);
T
Terry 已提交
157
        if (!is_array($this->_productSpuAttrArr) || empty($this->_productSpuAttrArr)) {
_
__FresHmaN 已提交
158 159
            return;
        }
T
Terry 已提交
160 161
        // 当前的spu属性对应值数组 $['color'] = 'red'

T
Terry 已提交
162
        $this->_currentSpuAttrValArr = [];
T
Terry 已提交
163
        foreach ($this->_productSpuAttrArr as $spuAttr) {
T
Terry 已提交
164
            $spuAttrVal = $this->_product[$spuAttr];
T
Terry 已提交
165
            if ($spuAttrVal) {
T
Terry 已提交
166
                $this->_currentSpuAttrValArr[$spuAttr] = $spuAttrVal;
T
Terry 已提交
167
            } else {
T
Terry 已提交
168
                // 如果某个spuAttr的值为空,则退出,这个说明产品数据有问题。
T
Terry 已提交
169
                return;
T
Terry 已提交
170
            }
_
__FresHmaN 已提交
171
        }
T
Terry 已提交
172
        // 得到当前的spu下面的所有的值
T
Terry 已提交
173 174 175
        $select = ['name', 'image', 'url_key'];
        $data = $this->getSpuData($select);
        $spuValColl = [];
T
Terry 已提交
176
        // 通过值,找到spu。
T
Terry 已提交
177
        $reverse_val_spu = [];
T
Terry 已提交
178 179
        if (is_array($data) && !empty($data)) {
            foreach ($data as $one) {
T
Terry 已提交
180
                $reverse_key = '';
T
Terry 已提交
181
                foreach ($this->_productSpuAttrArr as $spuAttr) {
T
Terry 已提交
182 183 184
                    $spuValColl[$spuAttr][$one[$spuAttr]] = $one[$spuAttr];
                    $reverse_key .= $one[$spuAttr];
                }
T
Terry 已提交
185

T
Terry 已提交
186 187 188 189 190
                //$active = 'class="active"';
                $one['main_img'] = isset($one['image']['main']['image']) ? $one['image']['main']['image'] : '';
                $one['url'] = Yii::$service->url->getUrl($one['url_key']);
                $reverse_val_spu[$reverse_key] = $one;
                $showAsImgVal = $one[$this->_spuAttrShowAsImg];
T
Terry 已提交
191 192
                if ($showAsImgVal) {
                    if (!isset($this->_spuAttrShowAsImgArr[$this->_spuAttrShowAsImg])) {
T
Terry 已提交
193 194 195 196
                        $this->_spuAttrShowAsImgArr[$showAsImgVal] = $one;
                    }
                }
            }
_
__FresHmaN 已提交
197
        }
T
Terry 已提交
198 199
        // 得到各个spu属性对应的值的集合。
        foreach ($spuValColl as $spuAttr => $attrValArr) {
T
Terry 已提交
200
            $spuValColl[$spuAttr] = array_unique($attrValArr);
T
Terry 已提交
201
            $spuValColl[$spuAttr] = $this->sortSpuAttr($spuAttr, $spuValColl[$spuAttr]);
T
Terry 已提交
202
        }
T
Terry 已提交
203

T
Terry 已提交
204
        $spuShowArr = [];
T
Terry 已提交
205
        foreach ($spuValColl as $spuAttr => $attrValArr) {
T
Terry 已提交
206
            $attr_coll = [];
T
Terry 已提交
207 208
            foreach ($attrValArr as $attrVal) {
                $attr_info = $this->getSpuAttrInfo($spuAttr, $attrVal, $reverse_val_spu);
T
Terry 已提交
209
                $attr_coll[] = $attr_info;
T
Terry 已提交
210

T
Terry 已提交
211 212 213 214 215 216 217 218 219
                //[
                //    'attr_val' => $attr,
                //];
            }
            $spuShowArr[] = [
                'label' => $spuAttr,
                'value' => $attr_coll,
            ];
        }
T
Terry 已提交
220

T
Terry 已提交
221 222
        return $spuShowArr;
    }
223
    // spu属性部分
T
Terry 已提交
224 225
    protected function getSpuAttrInfo($spuAttr, $attrVal, $reverse_val_spu)
    {
T
Terry 已提交
226 227
        $current = $this->_currentSpuAttrValArr;
        $active = false;
T
Terry 已提交
228 229
        if (isset($this->_currentSpuAttrValArr[$spuAttr])) {
            if ($attrVal != $this->_currentSpuAttrValArr[$spuAttr]) {
T
Terry 已提交
230
                $current[$spuAttr] = $attrVal;
T
Terry 已提交
231
            } else {
T
Terry 已提交
232 233 234 235 236 237 238
                $active = true;
            }
        }
        $reverse_key = $this->generateSpuReverseKey($current);
        $return = [];
        $return['attr_val'] = $attrVal;
        $return['active'] = 'noactive';
T
Terry 已提交
239

T
Terry 已提交
240
        //echo $reverse_key."<br/>";
T
Terry 已提交
241
        if (isset($reverse_val_spu[$reverse_key]) && is_array($reverse_val_spu[$reverse_key])) {
T
Terry 已提交
242 243
            $return['active'] = 'active';
            $arr = $reverse_val_spu[$reverse_key];
T
Terry 已提交
244 245
            foreach ($arr as $k=>$v) {
                $return[$k] = $v;
T
Terry 已提交
246
            }
T
Terry 已提交
247
            if ($spuAttr == $this->_spuAttrShowAsImg) {
T
Terry 已提交
248 249
                $return['show_as_img'] = $arr['main_img'];
            }
T
Terry 已提交
250 251 252
        } else {
            // 如果是图片,不存在,则使用备用的。
            if ($spuAttr == $this->_spuAttrShowAsImg) {
T
Terry 已提交
253 254
                $return['active'] = 'active';
                $arr = $this->_spuAttrShowAsImgArr[$attrVal];
T
Terry 已提交
255 256 257
                if (is_array($arr) && !empty($arr)) {
                    foreach ($arr as $k=>$v) {
                        $return[$k] = $v;
T
Terry 已提交
258 259 260 261 262
                    }
                }
                $return['show_as_img'] = $arr['main_img'];
            }
        }
T
Terry 已提交
263
        if ($active) {
T
Terry 已提交
264 265
            $return['active'] = 'current';
        }
T
Terry 已提交
266

T
Terry 已提交
267 268
        return $return;
    }
T
Terry 已提交
269 270 271

    protected function generateSpuReverseKey($one)
    {
T
Terry 已提交
272
        $reverse_key = '';
T
Terry 已提交
273
        foreach ($this->_productSpuAttrArr as $spuAttr) {
T
Terry 已提交
274 275
            $spuValColl[$spuAttr][] = $one[$spuAttr];
            $reverse_key .= $one[$spuAttr];
_
__FresHmaN 已提交
276
        }
T
Terry 已提交
277

T
Terry 已提交
278
        return  $reverse_key;
_
__FresHmaN 已提交
279 280 281 282 283 284 285 286
    }

    /**
     *	@property $data | Array  各个尺码对应的产品数组
     *  @return array 排序后的数组
     *		该函数,按照在配置中的size的顺序,将$data中的数据进行排序,让其按照尺码的由小到大的顺序
     * 		排列,譬如 :s,m,l,xl,xxl,xxxl等
     */
T
Terry 已提交
287
    protected function sortSpuAttr($spuAttr, $data)
_
__FresHmaN 已提交
288 289 290 291 292
    {
        // 对size排序一下
        $size = [];
        $attr_group = $this->_product['attr_group'];
        $attrInfo = Yii::$service->product->getGroupAttrInfo($attr_group);
T
Terry 已提交
293
        $size_arr = isset($attrInfo[$spuAttr]['display']['data']) ? $attrInfo[$spuAttr]['display']['data'] : '';
_
__FresHmaN 已提交
294 295 296 297 298 299 300
        $d_arr = [];
        if (is_array($size_arr) && !empty($size_arr)) {
            foreach ($size_arr as $size) {
                if (isset($data[$size])) {
                    $d_arr[$size] = $data[$size];
                }
            }
T
Terry 已提交
301
            if (!empty($d_arr)) {
_
__FresHmaN 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
                return $d_arr;
            }
        }

        return $data;
    }

    /**
     * @return array
     *               得到当前货币状态下的产品的价格和特价信息。
     */
    protected function getProductPriceInfo()
    {
        $price = $this->_product['price'];
        $special_price = $this->_product['special_price'];
        $special_from = $this->_product['special_from'];
        $special_to = $this->_product['special_to'];

        return Yii::$service->product->price->getCurrentCurrencyProductPriceInfo($price, $special_price, $special_from, $special_to);
    }

    /**
     * 初始化数据包括
     * 主键值:$this->_primaryVal
     * 当前产品对象:$this->_product
     * Meta keywords , Meta Description等信息的设置。
     * Title的设置。
     * 根据当前产品的attr_group(属性组信息)重新给Product Model增加相应的属性组信息
     * 然后,重新获取当前产品对象:$this->_product,此时加入了配置中属性组对应的属性。
     */
    protected function initProduct()
    {
        $primaryKey = Yii::$service->product->getPrimaryKey();
        $primaryVal = Yii::$app->request->get($primaryKey);
        $this->_primaryVal = $primaryVal;
        $product = Yii::$service->product->getByPrimaryKey($primaryVal);
        $this->_product = $product;
        Yii::$app->view->registerMetaTag([
            'name' => 'keywords',
            'content' => Yii::$service->store->getStoreAttrVal($product['meta_keywords'], 'meta_keywords'),
        ]);
        Yii::$app->view->registerMetaTag([
            'name' => 'description',
            'content' => Yii::$service->store->getStoreAttrVal($product['meta_description'], 'meta_description'),
        ]);
        $this->_title = Yii::$service->store->getStoreAttrVal($product['meta_title'], 'meta_title');
        $name = Yii::$service->store->getStoreAttrVal($product['name'], 'name');
        //$this->breadcrumbs($name);
        $this->_title = $this->_title ? $this->_title : $name;
        Yii::$app->view->title = $this->_title;
        //$this->_where = $this->initWhere();

        // 通过上面查询的属性组,得到属性组对应的属性列表
        // 然后重新查询产品
        $attr_group = $this->_product['attr_group'];

        Yii::$service->product->addGroupAttrs($attr_group);

        // 重新查询产品信息。
        $product = Yii::$service->product->getByPrimaryKey($primaryVal);
        $this->_product = $product;
    }

    // 面包屑导航
    protected function breadcrumbs($name)
    {
        if (Yii::$app->controller->module->params['category_breadcrumbs']) {
            $parent_info = Yii::$service->category->getAllParentInfo($this->_category['parent_id']);
            if (is_array($parent_info) && !empty($parent_info)) {
                foreach ($parent_info as $info) {
                    $parent_name = Yii::$service->store->getStoreAttrVal($info['name'], 'name');
                    $parent_url = Yii::$service->url->getUrl($info['url_key']);
                    Yii::$service->page->breadcrumbs->addItems(['name' => $parent_name, 'url' => $parent_url]);
                }
            }
            Yii::$service->page->breadcrumbs->addItems(['name' => $name]);
        } else {
            Yii::$service->page->breadcrumbs->active = false;
        }
    }
382
    // 买了的人还买了什么,通过产品字段取出来sku,然后查询得到。
_
__FresHmaN 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    protected function getProductBySkus($skus)
    {
        $buy_also_buy_sku = $this->_product['buy_also_buy_sku'];
        if ($buy_also_buy_sku) {
            $skus = explode(',', $buy_also_buy_sku);
            if (is_array($skus) && !empty($skus)) {
                $filter['select'] = [
                    'sku', 'spu', 'name', 'image',
                    'price', 'special_price',
                    'special_from', 'special_to',
                    'url_key', 'score',
                ];
                $filter['where'] = ['in', 'sku', $skus];
                $products = Yii::$service->product->getProducts($filter);
                //var_dump($products);
                $products = Yii::$service->category->product->convertToCategoryInfo($products);

                return $products;
            }
        }
    }
}