PicTextController.php 4.3 KB

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