CpController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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['del'] ?? '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 actionDelCp()
  91. {
  92. $get = Yii::$app->request->get();
  93. $id = $get['id'] ?? 0;
  94. $cp = CpClass::getById($id, true);
  95. if (isset($cp->mainId) == false || $cp->mainId != $this->mainId) {
  96. util::fail('无法查看');
  97. }
  98. $connection = Yii::$app->db;//事务处理
  99. $transaction = $connection->beginTransaction();
  100. try {
  101. CpClass::delCp($cp, $this->shop);
  102. $transaction->commit();
  103. util::complete();
  104. } catch (\Exception $e) {
  105. $transaction->rollBack();
  106. Yii::info("删除失败原因:" . $e->getMessage());
  107. util::fail('删除失败');
  108. }
  109. }
  110. //查看详情 ssh 20240219
  111. public function actionDetail()
  112. {
  113. $get = Yii::$app->request->get();
  114. $id = $get['id'] ?? 0;
  115. $cp = CpClass::getById($id);
  116. if (isset($cp['mainId']) == false || $cp['mainId'] != $this->mainId) {
  117. util::fail('无法查看');
  118. }
  119. $classId = $cp['classId'] ?? 0;
  120. $class = CpClassClass::getById($classId, true);
  121. $className = $class->name ?? '';
  122. $shortCover = $cp['cover'] ?? '';
  123. $smallCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  124. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  125. $cp['shortCover'] = $shortCover;
  126. $cp['cover'] = $bigCover;
  127. $cp['smallCover'] = $smallCover;
  128. $cp['bigCover'] = $bigCover;
  129. $cp['className'] = $className;
  130. util::success($cp);
  131. }
  132. }