修改加价购商品详情的数据结构以适应js交互
Code Review By Rock Zhang
Showing
1 changed file
with
66 additions
and
42 deletions
@@ -647,47 +647,58 @@ class CartModel | @@ -647,47 +647,58 @@ class CartModel | ||
647 | if (isset($productData['goods_list'])) { | 647 | if (isset($productData['goods_list'])) { |
648 | $goodsList = $productData['goods_list']; | 648 | $goodsList = $productData['goods_list']; |
649 | 649 | ||
650 | + $sizeName = ''; | ||
650 | $colors = array(); | 651 | $colors = array(); |
652 | + $colorList = array(); | ||
651 | $oneColor = array(); | 653 | $oneColor = array(); |
652 | $sizes = array(); | 654 | $sizes = array(); |
653 | $oneSize = array(); | 655 | $oneSize = array(); |
654 | $sizeList = array(); | 656 | $sizeList = array(); |
657 | + $allSizeList = array(); // 所有尺码列表 | ||
655 | $thumbImageList = array(); | 658 | $thumbImageList = array(); |
656 | $colorNum = 0; | 659 | $colorNum = 0; |
657 | $totalStorageNum = 0; // 总库存数 | 660 | $totalStorageNum = 0; // 总库存数 |
658 | $colorStorageGroup = array(); // 颜色分组的库存总数集合, 多个之间用/分隔 | 661 | $colorStorageGroup = array(); // 颜色分组的库存总数集合, 多个之间用/分隔 |
659 | foreach ($goodsList as $val) { | 662 | foreach ($goodsList as $val) { |
660 | $colorNum = 0; | 663 | $colorNum = 0; |
661 | - $sizeStorageStr = ''; | ||
662 | - | ||
663 | - // 尺码 | ||
664 | - foreach ($val['size_list'] as $one) { | ||
665 | - $oneSize = array(); | ||
666 | - $oneSize['id'] = $one['size_id']; | ||
667 | - $oneSize['skuId'] = $one['product_sku']; | ||
668 | - $oneSize['goodsId'] = $val['goods_id']; | ||
669 | - $oneSize['colorId'] = $val['color_id']; | ||
670 | - $oneSize['name'] = $one['size_name']; | ||
671 | - $oneSize['sizeNum'] = $one['storage_number']; | ||
672 | - | ||
673 | - $colorNum += $one['storage_number']; | ||
674 | - $sizeStorageStr .= $one['storage_number'] . '/'; | ||
675 | - | ||
676 | - $sizeList[$val['product_skc']][] = $oneSize; | 664 | + $sizeName = ''; |
665 | + | ||
666 | + // 商品的尺码列表 | ||
667 | + $colorStorageGroup[ $val['product_skc'] ] = array(); | ||
668 | + if (isset($val['size_list'])) { | ||
669 | + // 尺码 | ||
670 | + foreach ($val['size_list'] as $one) { | ||
671 | + $sizeName = $one['size_name']; | ||
672 | + $oneSize = array(); | ||
673 | + | ||
674 | + $oneSize['id'] = $one['size_id']; | ||
675 | + $oneSize['skuId'] = $one['product_sku']; | ||
676 | + $oneSize['goodsId'] = $val['goods_id']; | ||
677 | + $oneSize['colorId'] = $val['color_id']; | ||
678 | + $oneSize['name'] = $sizeName; | ||
679 | + $oneSize['sizeNum'] = $one['storage_number']; | ||
680 | + $sizeList[$val['product_skc']][] = $oneSize; | ||
681 | + | ||
682 | + // 所有尺码列表,赋值用于前端展示默认尺码的时候 判断出没有库存则显示灰色 | ||
683 | + $allSizeList[$sizeName] = empty($allSizeList[$sizeName]['storage_number']) | ||
684 | + ? array('storage' => $one['storage_number'], 'id' => $one['size_id']) | ||
685 | + : $allSizeList[$sizeName]; | ||
686 | + | ||
687 | + $colorNum += intval($one['storage_number']); | ||
688 | + | ||
689 | + $colorStorageGroup[$val['product_skc']][$one['size_name']] = $one['storage_number']; | ||
690 | + } | ||
677 | 691 | ||
678 | - $colorStorageGroup[$val['product_skc']][$one['size_name']] = $one['storage_number']; | 692 | + // 颜色分组 |
693 | + $oneColor = array(); | ||
694 | + $oneColor['id'] = $val['color_id']; | ||
695 | + $oneColor['skcId'] = $val['product_skc']; | ||
696 | + $oneColor['name'] = $val['color_name']; | ||
697 | + $oneColor['goodsName'] = $productData['product_name']; | ||
698 | + $oneColor['colorNum'] = $colorNum; | ||
699 | + $colorList[] = $oneColor; | ||
679 | } | 700 | } |
680 | 701 | ||
681 | - // 颜色 | ||
682 | - $oneColor = array(); | ||
683 | - $oneColor['id'] = $val['color_id']; | ||
684 | - $oneColor['skcId'] = $val['product_skc']; | ||
685 | - $oneColor['name'] = $val['color_name']; | ||
686 | - $oneColor['goodsName'] = $productData['product_name']; | ||
687 | - $oneColor['colorNum'] = $colorNum; | ||
688 | - $oneColor['sizeNumStr'] = rtrim($sizeStorageStr, '/'); | ||
689 | - $colors[] = $oneColor; | ||
690 | - | ||
691 | // 缩略图 | 702 | // 缩略图 |
692 | foreach ($val['images_list'] as $image) { | 703 | foreach ($val['images_list'] as $image) { |
693 | $thumbImageList[] = array( | 704 | $thumbImageList[] = array( |
@@ -699,25 +710,38 @@ class CartModel | @@ -699,25 +710,38 @@ class CartModel | ||
699 | $totalStorageNum += $colorNum; | 710 | $totalStorageNum += $colorNum; |
700 | } | 711 | } |
701 | 712 | ||
702 | - // 遍历所有尺码,统计出该尺码的每个颜色的库存量,没有时添0,不能空着,因为JS中需要判断 | ||
703 | - foreach ($sizeList as $skc => $sizeArr) { | ||
704 | - foreach ($sizeArr as $key => $value) { | ||
705 | - $sizeStorageStr = ''; | ||
706 | - foreach ($colorStorageGroup as $colorArr) { | ||
707 | - if (isset($colorArr[$value['name']])) { | ||
708 | - $sizeStorageStr .= $colorArr[$value['name']] . '/'; | ||
709 | - } else { | ||
710 | - $sizeStorageStr .= '0/'; | ||
711 | - } | ||
712 | - } | ||
713 | - $sizeList[$skc][$key]['colorNumStr'] = rtrim($sizeStorageStr, '/'); | 713 | + // 遍历所有尺码,构建颜色显示数据 |
714 | + $i = 1; | ||
715 | + foreach ($allSizeList as $sizeName => $value) { | ||
716 | + // 默认尺码 | ||
717 | + $sizes[0]['size'][] = array( | ||
718 | + 'name' => $sizeName, // 尺码名称 | ||
719 | + 'sizeNum' => empty($value['storage']) ? false : true, // 是否有库存 (false:表示没有库存,true:表示有库存) | ||
720 | + 'id' => $value['id'], | ||
721 | + ); | ||
722 | + | ||
723 | + // 各个颜色的尺码, 每行显示一个尺码对应的颜色 | ||
724 | + foreach ($colorList as $colorArr) { | ||
725 | + $colorArr['colorNum'] = isset($colorStorageGroup[ $colorArr['skcId'] ][$sizeName]) ? $colorStorageGroup[ $colorArr['skcId'] ][$sizeName] : 0; | ||
726 | + $colors[$i]['color'][] = $colorArr; | ||
714 | } | 727 | } |
728 | + $colors[$i]['id'] = $value['id']; | ||
729 | + | ||
730 | + ++ $i; | ||
715 | } | 731 | } |
716 | 732 | ||
717 | - // 格式化尺码对应的各个颜色分组 | ||
718 | - foreach ($colors as $color) { | ||
719 | - $sizes[]['size'] = $sizeList[$color['skcId']]; | 733 | + // 遍历所有颜色, 构建尺码显示数据 |
734 | + $i = 1; | ||
735 | + foreach ($colorList as $value) { | ||
736 | + // 各个尺码的颜色,每行显示一个颜色的对应尺码 | ||
737 | + $sizes[$i]['size'] = $sizeList[ $value['skcId'] ]; | ||
738 | + $sizes[$i]['colorId'] = $value['skcId']; | ||
739 | + // 默认颜色 | ||
740 | + $colors[0]['color'][] = $value; | ||
741 | + | ||
742 | + ++ $i; | ||
720 | } | 743 | } |
744 | + ksort($colors, SORT_NUMERIC); | ||
721 | 745 | ||
722 | $data['thumbs'] = $thumbImageList; | 746 | $data['thumbs'] = $thumbImageList; |
723 | $data['colors'] = $colors; | 747 | $data['colors'] = $colors; |
-
Please register or login to post a comment