PicTextController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $shopId = $this->shopId;
  66. $picTextId = intval($post['picTextId']);
  67. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  68. if (empty($ext)) {
  69. util::fail('门店信息缺失呢');
  70. }
  71. if ($switch == 1) {
  72. if (empty($picTextId) || !is_numeric($picTextId)) {
  73. util::fail('请选择图文');
  74. }
  75. $ext->purchaseGuide = $picTextId;
  76. } else {
  77. $ext->purchaseGuide = 0;
  78. }
  79. $ext->save();
  80. util::complete('修改成功');
  81. }
  82. public function actionDel()
  83. {
  84. $id = intval(Yii::$app->request->post('id'));
  85. $picText = PicTextClass::getById($id, true, 'id,mainId,content');
  86. if (empty($picText)) {
  87. util::fail('图文id不对');
  88. }
  89. if ($picText->mainId != $this->mainId) {
  90. util::fail('没有权限');
  91. }
  92. $re = PicTextClass::deleteById($id);
  93. if ($re) {
  94. // 删除 oss 图片
  95. $content = $picText->content;
  96. if (!empty($content)) {
  97. $contentArr = json_decode($content, true);
  98. $filePaths = [];
  99. if (is_array($contentArr)) {
  100. foreach ($contentArr as $item) {
  101. if (isset($item['type']) && in_array($item['type'], [1, 2]) && !empty($item['content'])) {
  102. if ($item['type'] == 1) {
  103. $filePaths[] = $item['content'];
  104. } elseif ($item['type'] == 2 && is_array($item['content'])) {
  105. $filePaths = array_merge($filePaths, $item['content']);
  106. }
  107. }
  108. }
  109. }
  110. if (!empty($filePaths)) {
  111. try {
  112. \common\components\oss::deleteObjects($filePaths);
  113. } catch (\Exception $e) {
  114. util::fail('删除OSS文件失败:' . $e->getMessage());
  115. }
  116. }
  117. }
  118. util::complete('成功删除');
  119. }
  120. util::fail('删除失败');
  121. }
  122. public function actionDetail()
  123. {
  124. $id = intval(Yii::$app->request->get('id'));
  125. $picText = PicTextClass::getById($id, true);
  126. if (empty($picText)) {
  127. util::fail('没有找到图片');
  128. }
  129. if ($picText->mainId != $this->mainId) {
  130. util::fail('没有权限');
  131. }
  132. util::success($picText);
  133. }
  134. }