CpClassController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\cp\classes\CpClassClass;
  4. use bizGhs\shop\classes\ShopClass;
  5. use common\components\imgUtil;
  6. use common\components\util;
  7. use Yii;
  8. class CpClassController extends BaseController
  9. {
  10. //左侧菜单导航 ssh 20231228
  11. public function actionGetMenu()
  12. {
  13. $mainId = $this->mainId;
  14. $list = CpClassClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0, 'hide' => 0], 'inTurn DESC', 'id,name,inTurn', null, true);
  15. util::success(['list' => $list]);
  16. }
  17. //获取产品所有分类,不含特价 ssh 20240106
  18. public function actionGetAll()
  19. {
  20. $mainId = $this->mainId;
  21. $list = CpClassClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0], 'inTurn DESC', 'id,name,cover,inTurn');
  22. if (!empty($list)) {
  23. foreach ($list as $key => $val) {
  24. $shortCover = $val['cover'] ?? '';
  25. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  26. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  27. $list[$key]['shortCover'] = $shortCover;
  28. $list[$key]['bigCover'] = $bigCover;
  29. $list[$key]['cover'] = $cover;
  30. }
  31. }
  32. util::success(['list' => $list]);
  33. }
  34. //集合分类列表 ssh 20240104
  35. public function actionList()
  36. {
  37. //$get = Yii::$app->request->get();
  38. $mainId = $this->mainId ?? 0;
  39. $where = [];
  40. $where['mainId'] = $mainId;
  41. $where['delStatus'] = 0;
  42. $respond = CpClassClass::getClassList($where);
  43. util::success($respond);
  44. }
  45. //添加分类 ssh 20240104
  46. public function actionAdd()
  47. {
  48. $masterShop = $this->shop;
  49. if (isset($masterShop->default) == false || $masterShop->default == 0) {
  50. util::fail('请在总店添加');
  51. }
  52. $get = Yii::$app->request->get();
  53. $name = $get['name'] ?? '';
  54. if (empty($name)) {
  55. util::fail('名字不能为空');
  56. }
  57. $get['mainId'] = $this->mainId;
  58. $list = CpClassClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0], null, '*', null, true);
  59. if (!empty($list)) {
  60. foreach ($list as $info) {
  61. if ($info->name == $name) {
  62. util::fail('分类名已经存在');
  63. }
  64. }
  65. }
  66. CpClassClass::addClass($get, $this->shop);
  67. util::complete('添加成功');
  68. }
  69. //更新分类 ssh 20240104
  70. public function actionUpdate()
  71. {
  72. $masterShop = $this->shop;
  73. if (isset($masterShop->default) == false || $masterShop->default == 0) {
  74. util::fail('请在总店修改');
  75. }
  76. $get = Yii::$app->request->get();
  77. $id = $get['id'] ?? 0;
  78. $class = CpClassClass::getById($id, true);
  79. if (empty($class)) {
  80. util::fail('没有找到分类');
  81. }
  82. if ($class->mainId != $this->mainId) {
  83. util::fail('不是你的分类哦');
  84. }
  85. $name = $get['name'] ?? '';
  86. $list = CpClassClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0], null, '*', null, true);
  87. if (!empty($list)) {
  88. foreach ($list as $info) {
  89. if ($info->id != $id && $info->name == $name) {
  90. util::fail('分类名已经存在');
  91. }
  92. }
  93. }
  94. $inTurn = $get['inTurn'] ?? 100;
  95. $print = $get['print'] ?? 0;
  96. $hide = $get['hide'] ?? 0;
  97. $originName = $class->name ?? '';
  98. $class->name = $name;
  99. $class->inTurn = $inTurn;
  100. $class->print = $print;
  101. $class->hide = $hide;
  102. $class->save();
  103. if (isset($masterShop->default) && $masterShop->default == 1) {
  104. $sjId = $masterShop->sjId ?? 0;
  105. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  106. foreach ($shopList as $shop) {
  107. $shopId = $shop->id ?? 0;
  108. if ($shopId == $masterShop->id) {
  109. //前面已经修改过了
  110. continue;
  111. }
  112. $currentMainId = $shop->mainId ?? 0;
  113. $chainClassList = CpClassClass::getAllByCondition(['mainId' => $currentMainId], null, '*', null, true);
  114. if (!empty($chainClassList)) {
  115. foreach ($chainClassList as $chainClass) {
  116. if ($chainClass->name == $originName) {
  117. if ($chainClass->isDefault != 1) {
  118. $chainClass->name = $name;
  119. }
  120. $chainClass->inTurn = $inTurn;
  121. $chainClass->print = $print;
  122. $chainClass->hide = $hide;
  123. $chainClass->save();
  124. }
  125. }
  126. }
  127. }
  128. }
  129. util::complete('修改成功');
  130. }
  131. public function actionDel()
  132. {
  133. $masterShop = $this->shop;
  134. if (isset($masterShop->default) == false || $masterShop->default == 0) {
  135. util::fail('请在总店操作');
  136. }
  137. $get = Yii::$app->request->get();
  138. $id = $get['id'] ?? 0;
  139. $class = CpClassClass::getById($id, true);
  140. if ($class->mainId != $this->mainId) {
  141. util::fail('不是你的分类');
  142. }
  143. $connection = Yii::$app->db;
  144. $transaction = $connection->beginTransaction();
  145. try {
  146. CpClassClass::delClass($class, $this->shop);
  147. $transaction->commit();
  148. util::complete('操作成功');
  149. } catch (\Exception $e) {
  150. $transaction->rollBack();
  151. util::fail('删除失败');
  152. }
  153. }
  154. }