CpClassController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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], '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. CpClassClass::addClass($get, $this->shop);
  59. util::complete('添加成功');
  60. }
  61. //更新分类 ssh 20240104
  62. public function actionUpdate()
  63. {
  64. $masterShop = $this->shop;
  65. if (isset($masterShop->default) == false || $masterShop->default == 0) {
  66. util::fail('请在总店修改');
  67. }
  68. $get = Yii::$app->request->get();
  69. $id = $get['id'] ?? 0;
  70. $class = CpClassClass::getById($id, true);
  71. if (empty($class)) {
  72. util::fail('没有找到分类');
  73. }
  74. if ($class->mainId != $this->mainId) {
  75. util::fail('不是你的分类哦');
  76. }
  77. $name = $get['name'] ?? '';
  78. $inTurn = $get['inTurn'] ?? 100;
  79. $print = $get['print'] ?? 0;
  80. $hide = $get['hide'] ?? 0;
  81. $originName = $class->name ?? '';
  82. $class->name = $name;
  83. $class->inTurn = $inTurn;
  84. $class->print = $print;
  85. $class->hide = $hide;
  86. $class->save();
  87. if (isset($masterShop->default) && $masterShop->default == 1) {
  88. $sjId = $masterShop->sjId ?? 0;
  89. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  90. foreach ($shopList as $shop) {
  91. $shopId = $shop->id ?? 0;
  92. if ($shopId == $masterShop->id) {
  93. //前面已经修改过了
  94. continue;
  95. }
  96. $currentMainId = $shop->mainId ?? 0;
  97. $chainClassList = CpClassClass::getAllByCondition(['mainId' => $currentMainId], null, '*', null, true);
  98. if (!empty($chainClassList)) {
  99. foreach ($chainClassList as $chainClass) {
  100. if ($chainClass->name == $originName) {
  101. if ($chainClass->isDefault != 1) {
  102. $chainClass->name = $name;
  103. }
  104. $chainClass->inTurn = $inTurn;
  105. $chainClass->print = $print;
  106. $chainClass->hide = $hide;
  107. $chainClass->save();
  108. }
  109. }
  110. }
  111. }
  112. }
  113. util::complete('修改成功');
  114. }
  115. public function actionDel()
  116. {
  117. $masterShop = $this->shop;
  118. if (isset($masterShop->default) == false || $masterShop->default == 0) {
  119. util::fail('请在总店操作');
  120. }
  121. $get = Yii::$app->request->get();
  122. $id = $get['id'] ?? 0;
  123. $class = CpClassClass::getById($id, true);
  124. if ($class->mainId != $this->mainId) {
  125. util::fail('不是你的分类');
  126. }
  127. $connection = Yii::$app->db;
  128. $transaction = $connection->beginTransaction();
  129. try {
  130. CpClassClass::delClass($class, $this->shop);
  131. $transaction->commit();
  132. util::complete('操作成功');
  133. } catch (\Exception $e) {
  134. $transaction->rollBack();
  135. util::fail('删除失败');
  136. }
  137. }
  138. }