PicTextController.php 4.5 KB

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