Product.php 2.0 KB

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