PicTextController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\classes\CategoryClass;
  4. use bizHd\goods\models\Pic;
  5. use bizHd\shop\classes\ShopExtClass;
  6. use Yii;
  7. use bizHd\pictext\classes\PicTextClass;
  8. use common\components\util;
  9. class PicTextController extends BaseController
  10. {
  11. public function actionList()
  12. {
  13. $where = [];
  14. $where['mainId'] = $this->mainId;
  15. $where['delStatus'] = 0;
  16. $where['type'] = PicTextClass::TYPE_CATEGORY;
  17. $list = PicTextClass::getList('id, title, addTime', $where, 'addTime desc');
  18. util::success($list);
  19. }
  20. public function actionAdd()
  21. {
  22. $post = Yii::$app->request->post();
  23. $content = $post['content'];
  24. $post['content'] = $content;
  25. $post['mainId'] = $this->mainId;
  26. // $post['staffId'] = intval($this->shopAdmin->id);
  27. // 类别 1. 花束分类说明(运用于分类的商品) 2. 商品特有说明(非商品简介,优先级可高于分类说明) 3. 售前与售后说明
  28. $post['type'] = PicTextClass::TYPE_CATEGORY; // type 默认为 1
  29. $id = PicTextClass::add($post);
  30. util::success($id);
  31. }
  32. public function actionUpdate()
  33. {
  34. $post = Yii::$app->request->post();
  35. $id = $post['id'];
  36. $picText = PicTextClass::getById($id, true);
  37. if (empty($picText)) {
  38. util::fail('没有找到图文');
  39. }
  40. if ($picText->mainId != $this->mainId) {
  41. util::fail('没有权限修改');
  42. }
  43. $post['staffId'] = intval($this->shopAdmin->id);
  44. $id = PicTextClass::updateById($id, $post);
  45. util::success('修改成功');
  46. }
  47. public function actionUpdateCategoryId()
  48. {
  49. $post = Yii::$app->request->post();
  50. $id = $post['id'];
  51. $categoryId = intval($post['categoryId']);
  52. if ($categoryId <= 0) {
  53. util::fail('分类id出错');
  54. }
  55. $picText = PicTextClass::getById($id, true, 'id, mainId, type');
  56. if (empty($picText)) {
  57. util::fail('没有找到图文');
  58. }
  59. if ($picText->type != PicTextClass::TYPE_CATEGORY) {
  60. util::fail('图文类型不支持设置分类');
  61. }
  62. if ($picText->mainId != $this->mainId) {
  63. util::fail('没有权限修改');
  64. }
  65. $updateCount = CategoryClass::updateByCondition(['id' => $categoryId, 'mainId' => $this->mainId], ['picTextId' => $id]);
  66. //CategoryClass::updateById($id, ['picTextId' => $id]);
  67. if ($updateCount <= 0) {
  68. util::fail('变更失败');
  69. }
  70. util::success('变更成功');
  71. }
  72. public function actionDel()
  73. {
  74. $id = intval(Yii::$app->request->post('id'));
  75. $picText = PicTextClass::getById($id, true, 'id,mainId,content');
  76. if (empty($picText)) {
  77. util::fail('图文id不对');
  78. }
  79. if ($picText->mainId != $this->mainId) {
  80. util::fail('没有权限');
  81. }
  82. $re = PicTextClass::deleteById($id);
  83. if ($re) {
  84. // 删除 oss 图片
  85. $content = $picText->content;
  86. if (!empty($content)) {
  87. $contentArr = json_decode($content, true);
  88. $filePaths = [];
  89. if (is_array($contentArr)) {
  90. foreach ($contentArr as $item) {
  91. if (isset($item['type']) && in_array($item['type'], [1, 2]) && !empty($item['content'])) {
  92. if ($item['type'] == 1) {
  93. $filePaths[] = $item['content'];
  94. } elseif ($item['type'] == 2 && is_array($item['content'])) {
  95. $filePaths = array_merge($filePaths, $item['content']);
  96. }
  97. }
  98. }
  99. }
  100. if (!empty($filePaths)) {
  101. try {
  102. \common\components\oss::deleteObjects($filePaths);
  103. } catch (\Exception $e) {
  104. util::fail('删除OSS文件失败:' . $e->getMessage());
  105. }
  106. }
  107. }
  108. util::complete('成功删除');
  109. }
  110. util::fail('删除失败');
  111. }
  112. public function actionDetail()
  113. {
  114. $id = intval(Yii::$app->request->get('id'));
  115. $picText = PicTextClass::getById($id, true);
  116. if (empty($picText)) {
  117. util::fail('没有找到图片');
  118. }
  119. if ($picText->mainId != $this->mainId) {
  120. util::fail('没有权限');
  121. }
  122. util::success($picText);
  123. }
  124. public function actionGetPurchaseGuide()
  125. {
  126. $ghsShopId = intval(Yii::$app->request->get('shopId'));
  127. $shopExt = ShopExtClass::getByCondition(['shopId'=>$ghsShopId], false, false, 'purchaseGuide');
  128. if (empty($shopExt)) {
  129. util::fail('数据出错');
  130. }
  131. if ($shopExt['purchaseGuide'] == 0) {
  132. util::success(['picText' => []]);
  133. }
  134. $pt = PicTextClass::getById($shopExt['purchaseGuide'], true);
  135. if (empty($pt)) {
  136. util::fail('数据出错');
  137. }
  138. util::success(['picText'=>$pt]);
  139. }
  140. }