CategoryController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\goods\classes\GoodsUseCaseClass;
  5. use bizHd\goods\classes\UseCaseClass;
  6. use bizMall\goods\services\CategoryService;
  7. use Yii;
  8. use common\components\util;
  9. class CategoryController extends BaseController
  10. {
  11. /**
  12. * 游客可浏览分类与商品列表(商城分类页未登录浏览);下单/建客仍走登录后流程
  13. * buildRelation 对空 user 直接 return,游客不会建客
  14. */
  15. public $guestAccess = ['list', 'use-case-list', 'goods-list'];
  16. //获取分类 ssh 2019.12.2(游客可浏览,不依赖 user;无店铺上下文时返回空)
  17. public function actionList()
  18. {
  19. // 未登录场景必须有 account→mainId,否则无法定位门店分类
  20. if (empty($this->shop) || empty($this->mainId)) {
  21. util::success([]);
  22. }
  23. $get = Yii::$app->request->get();
  24. $flower = $get['flower'] ?? -1;
  25. //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
  26. $mainId = $this->mainId;
  27. if (getenv('YII_ENV') == 'production') {
  28. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  29. util::fail('暂无商品哦,编号25395');
  30. }
  31. } else {
  32. if (in_array($mainId, [0, 1])) {
  33. util::fail('暂无商品哦!编号639942');
  34. }
  35. }
  36. //建立客户关系(未登录时 buildRelation 直接返回,不建客)
  37. CustomClass::buildRelation($this->shop, $this->user);
  38. $mainId = $this->mainId ?? 0;
  39. $where = ['mainId' => $mainId, 'delStatus' => 0, 'status' => 1];
  40. if ($flower > -1) {
  41. $where['flower'] = $flower;
  42. }
  43. //默认列出正常分类,美团等第三方分类不列表
  44. $where['style'] = 0;
  45. $return = CategoryService::getAllCategory($where);
  46. util::success($return);
  47. }
  48. //取分类下商品 ssh 2019.12.2(游客可浏览,custom 为空时按散客价组装)
  49. public function actionGoodsList()
  50. {
  51. $get = Yii::$app->request->get();
  52. $shop = $this->shop;
  53. if (empty($shop)) {
  54. util::success(['list' => []]);
  55. }
  56. $custom = !empty($this->custom) ? $this->custom : [];
  57. $params = [];
  58. //调用花店端的方法,后面再分离 (为了代码维护可以不分离,免得一处改了,另一处没修改到。--- 所以分离好后,又还原了)
  59. $data = \bizHd\goods\classes\GoodsCategoryClass::getGoodsList($get, $shop, $custom, $params);
  60. $data['list'] = $this->appendUseCaseInfo($data['list'] ?? []);
  61. util::success($data);
  62. }
  63. /**
  64. * 场景列表(商城筛选弹窗用)
  65. * 游客可访问,未登录返回空数组
  66. */
  67. public function actionUseCaseList()
  68. {
  69. $user = $this->user;
  70. if (empty($user)) {
  71. util::success([]);
  72. }
  73. $mainId = intval($this->mainId ?? 0);
  74. if ($mainId <= 0) {
  75. util::success([]);
  76. }
  77. $list = UseCaseClass::getAllByCondition(
  78. ['mainId' => $mainId, 'status' => 1, 'delStatus' => 0],
  79. 'inTurn DESC',
  80. 'id,useCaseName'
  81. );
  82. util::success($list ?: []);
  83. }
  84. /**
  85. * 批量为商品列表补充场景 ID 与名称,供列表页标签展示
  86. *
  87. * @param array $list
  88. * @return array
  89. */
  90. private function appendUseCaseInfo($list)
  91. {
  92. if (empty($list)) {
  93. return [];
  94. }
  95. $goodsIds = array_column($list, 'id');
  96. if (empty($goodsIds)) {
  97. return $list;
  98. }
  99. $relations = GoodsUseCaseClass::getAllByCondition(
  100. ['goodsId' => ['in', $goodsIds]],
  101. null,
  102. 'goodsId,useCaseId'
  103. );
  104. $useCaseIds = array_unique(array_column($relations ?: [], 'useCaseId'));
  105. $nameMap = [];
  106. if (!empty($useCaseIds)) {
  107. $useCases = UseCaseClass::getAllByCondition(
  108. [
  109. 'id' => ['in', $useCaseIds],
  110. 'mainId' => intval($this->mainId ?? 0),
  111. 'delStatus' => 0,
  112. ],
  113. null,
  114. 'id,useCaseName'
  115. );
  116. foreach ($useCases ?: [] as $uc) {
  117. $nameMap[intval($uc['id'])] = $uc['useCaseName'] ?? '';
  118. }
  119. }
  120. $goodsUseCaseMap = [];
  121. foreach ($relations ?: [] as $rel) {
  122. $gid = intval($rel['goodsId']);
  123. $uid = intval($rel['useCaseId']);
  124. if (!isset($goodsUseCaseMap[$gid])) {
  125. $goodsUseCaseMap[$gid] = [];
  126. }
  127. if (!isset($nameMap[$uid])) {
  128. continue;
  129. }
  130. $goodsUseCaseMap[$gid][] = [
  131. 'id' => $uid,
  132. 'name' => $nameMap[$uid],
  133. ];
  134. }
  135. foreach ($list as &$item) {
  136. $gid = intval($item['id'] ?? 0);
  137. $cases = $goodsUseCaseMap[$gid] ?? [];
  138. $item['useCaseIdList'] = array_column($cases, 'id');
  139. $item['useCaseNames'] = array_values(array_filter(array_column($cases, 'name')));
  140. }
  141. unset($item);
  142. return $list;
  143. }
  144. }