CpController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $where['mainId'] = $mainId;
  20. if (!empty($search)) {
  21. if (stringUtil::isLetter($search)) {
  22. $where['py'] = ['like', $search];
  23. } else {
  24. $where['name'] = ['like', $search];
  25. }
  26. } else {
  27. if (!empty($classId)) {
  28. $where['classId'] = $classId;
  29. }
  30. }
  31. $respond = CpClass::getCpList($where);
  32. util::success($respond);
  33. }
  34. //添加产品 ssh 20240106
  35. public function actionAddCp()
  36. {
  37. $post = Yii::$app->request->post();
  38. $post['mainId'] = $this->mainId;
  39. $post['adminId'] = $this->adminId;
  40. if (isset($post['name']) == false || empty($post['name'])) {
  41. util::fail('请填写名称');
  42. }
  43. $post['py'] = stringUtil::py($post['name']);
  44. $staff = $this->shopAdmin;
  45. $staffName = $staff->name ?? '';
  46. $post['staffName'] = $staffName;
  47. $shop = $this->shop;
  48. unset($post['id']);
  49. $connection = Yii::$app->db;//事务处理
  50. $transaction = $connection->beginTransaction();
  51. try {
  52. CpClass::addCp($post, $shop);
  53. $transaction->commit();
  54. util::complete('添加成功');
  55. } catch (\Exception $e) {
  56. $transaction->rollBack();
  57. util::fail('添加没有成功');
  58. }
  59. }
  60. public function actionUpdateCp()
  61. {
  62. $post = Yii::$app->request->post();
  63. $id = $post['id'] ?? 0;
  64. $cp = CpClass::getById($id, true);
  65. if (empty($cp)) {
  66. util::fail('没有找到产品');
  67. }
  68. if ($cp->mainId != $this->mainId) {
  69. util::fail('不是你的产品');
  70. }
  71. $connection = Yii::$app->db;//事务处理
  72. $transaction = $connection->beginTransaction();
  73. try {
  74. unset($post['id']);
  75. CpClass::updateCp($post, $cp, $this->shop);
  76. $transaction->commit();
  77. util::complete();
  78. } catch (\Exception $e) {
  79. $transaction->rollBack();
  80. util::fail('添加没有成功');
  81. }
  82. }
  83. public function actionDelCp()
  84. {
  85. $get = Yii::$app->request->get();
  86. $id = $get['id'] ?? 0;
  87. $cp = CpClass::getById($id,true);
  88. if (isset($cp->mainId) == false || $cp->mainId != $this->mainId) {
  89. util::fail('无法查看');
  90. }
  91. $connection = Yii::$app->db;//事务处理
  92. $transaction = $connection->beginTransaction();
  93. try {
  94. CpClass::delCp($cp, $this->shop);
  95. $transaction->commit();
  96. util::complete();
  97. } catch (\Exception $e) {
  98. $transaction->rollBack();
  99. Yii::info("删除失败原因:" . $e->getMessage());
  100. util::fail('删除失败');
  101. }
  102. }
  103. //查看详情 ssh 20240219
  104. public function actionDetail()
  105. {
  106. $get = Yii::$app->request->get();
  107. $id = $get['id'] ?? 0;
  108. $cp = CpClass::getById($id);
  109. if (isset($cp['mainId']) == false || $cp['mainId'] != $this->mainId) {
  110. util::fail('无法查看');
  111. }
  112. $classId = $cp['classId'] ?? 0;
  113. $class = CpClassClass::getById($classId, true);
  114. $className = $class->name ?? '';
  115. $shortCover = $cp['cover'] ?? '';
  116. $smallCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  117. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  118. $cp['shortCover'] = $shortCover;
  119. $cp['cover'] = $bigCover;
  120. $cp['smallCover'] = $smallCover;
  121. $cp['bigCover'] = $bigCover;
  122. $cp['className'] = $className;
  123. util::success($cp);
  124. }
  125. }