PicTextController.php 5.1 KB

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