PicTextController.php 4.7 KB

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