CpController.php 3.8 KB

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