CpController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\cp\classes\CpClass;
  4. use bizGhs\cp\classes\CpClassClass;
  5. use common\components\imgUtil;
  6. use common\components\stringUtil;
  7. use common\components\util;
  8. use Yii;
  9. class CpController extends BaseController
  10. {
  11. //根据分类id取集合,带分页 ssh 20231228
  12. public function actionList()
  13. {
  14. $get = Yii::$app->request->get();
  15. $classId = $get['classId'] ?? 0;
  16. $search = $get['search'] ?? '';
  17. $mainId = $this->mainId ?? 0;
  18. $where = [];
  19. $requestType = $get['requestType'] ?? 'cp';
  20. $where['mainId'] = $mainId;
  21. if ($requestType == 'cp') {
  22. $where['delStatus'] = 0;
  23. }
  24. if ($requestType == 'del') {
  25. $where['delStatus'] = 1;
  26. }
  27. if (!empty($search)) {
  28. if (stringUtil::isLetter($search)) {
  29. $where['py'] = ['like', $search];
  30. } else {
  31. $where['name'] = ['like', $search];
  32. }
  33. } else {
  34. if (!empty($classId)) {
  35. $where['classId'] = $classId;
  36. }
  37. }
  38. $respond = CpClass::getCpList($where);
  39. util::success($respond);
  40. }
  41. //添加产品 ssh 20240106
  42. public function actionAddCp()
  43. {
  44. $post = Yii::$app->request->post();
  45. $post['mainId'] = $this->mainId;
  46. $post['adminId'] = $this->adminId;
  47. if (isset($post['name']) == false || empty($post['name'])) {
  48. util::fail('请填写名称');
  49. }
  50. $post['py'] = stringUtil::py($post['name']);
  51. $staff = $this->shopAdmin;
  52. $staffName = $staff->name ?? '';
  53. $post['staffName'] = $staffName;
  54. $shop = $this->shop;
  55. unset($post['id']);
  56. $connection = Yii::$app->db;//事务处理
  57. $transaction = $connection->beginTransaction();
  58. try {
  59. CpClass::addCp($post, $shop);
  60. $transaction->commit();
  61. util::complete('添加成功');
  62. } catch (\Exception $e) {
  63. $transaction->rollBack();
  64. util::fail('添加没有成功');
  65. }
  66. }
  67. public function actionUpdateCp()
  68. {
  69. $post = Yii::$app->request->post();
  70. $id = $post['id'] ?? 0;
  71. $cp = CpClass::getById($id, true);
  72. if (empty($cp)) {
  73. util::fail('没有找到产品');
  74. }
  75. if ($cp->mainId != $this->mainId) {
  76. util::fail('不是你的产品');
  77. }
  78. $connection = Yii::$app->db;//事务处理
  79. $transaction = $connection->beginTransaction();
  80. try {
  81. unset($post['id']);
  82. CpClass::updateCp($post, $cp, $this->shop);
  83. $transaction->commit();
  84. util::complete();
  85. } catch (\Exception $e) {
  86. $transaction->rollBack();
  87. util::fail('添加没有成功');
  88. }
  89. }
  90. public function actionRecoverCp()
  91. {
  92. $masterShop = $this->shop;
  93. if (isset($masterShop->default) == false || $masterShop->default == 0) {
  94. util::fail('请在总店操作');
  95. }
  96. $get = Yii::$app->request->get();
  97. $id = $get['id'] ?? 0;
  98. $cp = CpClass::getById($id, true);
  99. if (isset($cp->mainId) == false || $cp->mainId != $this->mainId) {
  100. util::fail('无法查看');
  101. }
  102. $connection = Yii::$app->db;//事务处理
  103. $transaction = $connection->beginTransaction();
  104. try {
  105. CpClass::recoverCp($cp, $this->shop);
  106. $transaction->commit();
  107. util::complete();
  108. } catch (\Exception $e) {
  109. $transaction->rollBack();
  110. Yii::info("删除失败原因:" . $e->getMessage());
  111. util::fail('删除失败');
  112. }
  113. }
  114. public function actionDelCp()
  115. {
  116. $masterShop = $this->shop;
  117. if (isset($masterShop->default) == false || $masterShop->default == 0) {
  118. util::fail('请在总店操作');
  119. }
  120. $get = Yii::$app->request->get();
  121. $id = $get['id'] ?? 0;
  122. $cp = CpClass::getById($id, true);
  123. if (isset($cp->mainId) == false || $cp->mainId != $this->mainId) {
  124. util::fail('无法查看');
  125. }
  126. $connection = Yii::$app->db;//事务处理
  127. $transaction = $connection->beginTransaction();
  128. try {
  129. CpClass::delCp($cp, $this->shop);
  130. $transaction->commit();
  131. util::complete();
  132. } catch (\Exception $e) {
  133. $transaction->rollBack();
  134. Yii::info("删除失败原因:" . $e->getMessage());
  135. util::fail('删除失败');
  136. }
  137. }
  138. //查看详情 ssh 20240219
  139. public function actionDetail()
  140. {
  141. $get = Yii::$app->request->get();
  142. $id = $get['id'] ?? 0;
  143. $cp = CpClass::getById($id);
  144. if (isset($cp['mainId']) == false || $cp['mainId'] != $this->mainId) {
  145. util::fail('无法查看');
  146. }
  147. $classId = $cp['classId'] ?? 0;
  148. $class = CpClassClass::getById($classId, true);
  149. $className = $class->name ?? '';
  150. $shortCover = $cp['cover'] ?? '';
  151. $smallCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  152. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  153. $cp['shortCover'] = $shortCover;
  154. $cp['cover'] = $bigCover;
  155. $cp['smallCover'] = $smallCover;
  156. $cp['bigCover'] = $bigCover;
  157. $cp['className'] = $className;
  158. util::success($cp);
  159. }
  160. }