PicTextController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace ghs\controllers;
  3. use Yii;
  4. use biz\shop\classes\ShopExtClass;
  5. use bizGhs\pictext\classes\PicTextClass;
  6. use common\components\util;
  7. class PicTextController extends BaseController
  8. {
  9. /**
  10. * 购买须知、售前与售后说明等列表
  11. */
  12. public function actionList()
  13. {
  14. // 当要兼容不同类型时,请从前端传过来
  15. $typeArr = [PicTextClass::TYPE_CATEGORY, PicTextClass::TYPE_BEFORE_AFTER_SAIL];
  16. $type = intval(Yii::$app->request->get('type'));
  17. if (!in_array($type, $typeArr)) {
  18. util::fail('类型不对');
  19. }
  20. $where = [];
  21. $where['mainId'] = $this->mainId;
  22. $where['delStatus'] = 0;
  23. // $where['type'] = $type; // 需求需要所有都展示,不分类别
  24. $list = PicTextClass::getList('id, title, addTime', $where, 'addTime desc');
  25. util::success($list);
  26. }
  27. /**
  28. * 创建购买须知、售前与售后说明
  29. */
  30. public function actionAdd()
  31. {
  32. $post = Yii::$app->request->post();
  33. $content = $post['content'];
  34. $post['content'] = $content;
  35. $post['mainId'] = $this->mainId;
  36. // $post['staffId'] = intval($this->shopAdmin->id);
  37. // 类别 1. 花束分类说明(运用于分类的商品) 2. 商品特有说明(非商品简介,优先级可高于分类说明) 3. 购买须知、售前与售后说明
  38. $post['type'] = intval($post['type']);
  39. if (!in_array($post['type'], [1, 2, 3])) {
  40. util::fail('类型不对');
  41. }
  42. $id = PicTextClass::add($post);
  43. util::success($id);
  44. }
  45. public function actionUpdate()
  46. {
  47. $post = Yii::$app->request->post();
  48. $id = $post['id'];
  49. $picText = PicTextClass::getById($id, true);
  50. if (empty($picText)) {
  51. util::fail('没有找到图文');
  52. }
  53. if ($picText->mainId != $this->mainId) {
  54. util::fail('没有权限修改');
  55. }
  56. $post['staffId'] = intval($this->shopAdmin->id);
  57. $id = PicTextClass::updateById($id, $post);
  58. util::success('修改成功');
  59. }
  60. // 门店购买须知开启/关闭
  61. public function actionSwitch()
  62. {
  63. $post = Yii::$app->request->post();
  64. $switch = $post['switch'] ?? 0;
  65. $purchaseGuideText = $post['purchaseGuideText'] ?? '';
  66. $shopId = $this->shopId;
  67. $picTextId = intval($post['picTextId']);
  68. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  69. if (empty($ext)) {
  70. util::fail('门店信息缺失呢');
  71. }
  72. //允许打开公告,但不设置公告详情的图文
  73. $ext->purchaseGuide = $picTextId;
  74. $shop = $this->shop;
  75. if ($switch == 1) {
  76. $shop->buyNotice = 1;
  77. } else {
  78. $shop->buyNotice = 0;
  79. }
  80. $shop->save();
  81. $ext->purchaseGuideText = $purchaseGuideText;
  82. $ext->save();
  83. util::complete('修改成功');
  84. }
  85. public function actionDel()
  86. {
  87. $id = intval(Yii::$app->request->post('id'));
  88. $picText = PicTextClass::getById($id, true, 'id,mainId,content');
  89. if (empty($picText)) {
  90. util::fail('图文id不对');
  91. }
  92. if ($picText->mainId != $this->mainId) {
  93. util::fail('没有权限');
  94. }
  95. $re = PicTextClass::deleteById($id);
  96. if ($re) {
  97. // 删除 oss 图片
  98. $content = $picText->content;
  99. if (!empty($content)) {
  100. $contentArr = json_decode($content, true);
  101. $filePaths = [];
  102. if (is_array($contentArr)) {
  103. foreach ($contentArr as $item) {
  104. if (isset($item['type']) && in_array($item['type'], [1, 2]) && !empty($item['content'])) {
  105. if ($item['type'] == 1) {
  106. $filePaths[] = $item['content'];
  107. } elseif ($item['type'] == 2 && is_array($item['content'])) {
  108. $filePaths = array_merge($filePaths, $item['content']);
  109. }
  110. }
  111. }
  112. }
  113. if (!empty($filePaths)) {
  114. try {
  115. \common\components\oss::deleteObjects($filePaths);
  116. } catch (\Exception $e) {
  117. util::fail('删除OSS文件失败:' . $e->getMessage());
  118. }
  119. }
  120. }
  121. util::complete('成功删除');
  122. }
  123. util::fail('删除失败');
  124. }
  125. public function actionDetail()
  126. {
  127. $id = intval(Yii::$app->request->get('id'));
  128. $picText = PicTextClass::getById($id, true);
  129. if (empty($picText)) {
  130. util::fail('没有找到图片');
  131. }
  132. if ($picText->mainId != $this->mainId) {
  133. util::fail('没有权限');
  134. }
  135. util::success($picText);
  136. }
  137. }