PosterController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace backend\modules\mall\controllers;
  3. use backend\controllers\MallController;
  4. use backend\modules\mall\services\PosterService;
  5. use common\enums\StatusEnum;
  6. use common\models\common\MallSetting;
  7. use common\models\common\PosterCode;
  8. use common\models\mall\Poster;
  9. use Yii;
  10. use yii\helpers\Json;
  11. /**
  12. * 装修-海报列表
  13. *
  14. * Class PosterController
  15. * @package backend\modules\mall\PosterController
  16. */
  17. class PosterController extends MallController
  18. {
  19. /**
  20. * 海报列表
  21. * @return mixed|string|void
  22. */
  23. public function actionIndex()
  24. {
  25. if (Yii::$app->request->isAjax) {
  26. if (Yii::$app->request->isPost) {
  27. $params = Yii::$app->request->Post();
  28. $res = Yii::$app->services->validate->validate($params, [
  29. [['cycle', 'keyword'], 'string'],
  30. [['page', 'limit'], 'integer'],
  31. [['cycle', 'keyword', 'page', 'limit', 'sort_type'], 'safe'],
  32. ]);
  33. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  34. $where = [];
  35. $where[] = ['=', 'mall_id', $this->mall_id];
  36. $where[] = ['<>', 'status', StatusEnum::DELETE];
  37. // 统计周期
  38. if (!empty($params['cycle'])) {
  39. $begTime = strtotime(date("Y-m-d", strtotime("-" . $params['cycle'] ." day")));
  40. $endTime = time();
  41. $where[] = ['between', 'created_at', $begTime, $endTime];
  42. }
  43. // 关键字搜索
  44. !empty($params['keyword']) && $where[] = [
  45. 'or',
  46. ['like', 'name', $params['keyword']],
  47. ['like', 'key_word', $params['keyword']]
  48. ];
  49. $order = 'sort ' .$params["sort_type"]. ', id desc';
  50. $select = ['id', 'status', 'name', 'key_word', 'scan_code_num', 'poster_content', 'created_at','share_url','look_num', 'sort'];
  51. $params = ['select' => $select, 'where' => $where, 'order' => $order];
  52. $data = Poster::listPage($params, false, true);
  53. $this->success('获取成功', $data);
  54. }
  55. }
  56. return $this->render($this->action->id);
  57. }
  58. /**
  59. * 编辑海报
  60. */
  61. public function actionEdit()
  62. {
  63. $request = Yii::$app->request;
  64. if ($request->isAjax) {
  65. if ($request->isPost) {
  66. $params = $request->Post();
  67. $res = Yii::$app->services->validate->validate($params, [
  68. [['name', 'poster_content','share_url'], 'required'],
  69. [['name', 'key_word','share_url'], 'string'],
  70. [['id'], 'integer'],
  71. [['id', 'key_word','sort'], 'safe'],
  72. ]);
  73. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  74. //share_url 限制背景图格式:只能png与jpeg格式
  75. $share_url_info = pathinfo($params['share_url']);
  76. if(!in_array($share_url_info['extension'],['png'])){
  77. return $this->error('海报背景请选择png格式');
  78. }
  79. // 保存数据
  80. $res = Poster::setData($this->mall_id, $params);
  81. if ($res === false) return $this->error(Poster::getStaticError());
  82. $this->success('保存成功');
  83. } else {
  84. $params = $request->Get();
  85. $res = Yii::$app->services->validate->validate($params, [
  86. [['id'], 'required'],
  87. [['id'], 'integer'],
  88. ]);
  89. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  90. $detail = Poster::getOne([['mall_id' => $this->mall_id], ['id' => $params['id']]], true);
  91. if (!empty($detail['poster_content'])) {
  92. $poster_content = Json::decode($detail['poster_content'], true);
  93. $detail['poster_content'] = $poster_content['share'];
  94. }
  95. $this->success('获取成功', $detail);
  96. }
  97. }
  98. return $this->render($this->action->id);
  99. }
  100. /**
  101. * 数据操作
  102. */
  103. public function actionOperate()
  104. {
  105. if (Yii::$app->request->isAjax) {
  106. if (Yii::$app->request->isPost) {
  107. $params = Yii::$app->request->Post();
  108. $res = Yii::$app->services->validate->validate($params, [
  109. [['id'], 'required'],
  110. [['type'], 'string'],
  111. [['id', 'status'], 'integer'],
  112. [['status'], 'safe'],
  113. ]);
  114. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  115. // 获取海报信息
  116. $detail = Poster::findOne(['id' => $params['id'], 'mall_id' => $this->mall_id]);
  117. empty($detail) && $this->error('海报查询出错');
  118. // 根据类型进行对应的更改状态或删除操作
  119. switch ($params['type']) {
  120. case 'change_status':
  121. $detail->status = $params['status'];
  122. break;
  123. case 'destroy':
  124. $detail->status = StatusEnum::DELETE;
  125. break;
  126. default :
  127. $this->error('类型有误');
  128. break;
  129. }
  130. $res = $detail->save(false);
  131. if($res === false) $this->error('修改失败');
  132. $this->success('修改成功');
  133. }
  134. }
  135. }
  136. /**
  137. * 获取海报设置
  138. * @Author: lun
  139. * @DateTime: 2023/2/28 0028 14:12
  140. * @Copyright: copyright (c) 2021 广东七件事集团
  141. * @return mixed|string|void
  142. */
  143. public function actionSetting()
  144. {
  145. if (Yii::$app->request->isAjax) {
  146. if (Yii::$app->request->isPost) {
  147. $params = Yii::$app->request->Post();
  148. $res = Yii::$app->services->setting->mall->set($this->mall_id, $params);
  149. if ($res === false) return $this->error(Yii::$app->services->setting->mall->getError());
  150. return $this->success('保存成功');
  151. }
  152. if (Yii::$app->request->isGet) {
  153. $data = MallSetting::getConfByGroup($this->mall_id, 'poster_setting', true);
  154. return $this->success('获取成功', $data);
  155. }
  156. }
  157. return $this->render($this->action->id);
  158. }
  159. /**
  160. * 修改海报状态
  161. *
  162. * @param integer $id
  163. * @return string
  164. */
  165. public function actionChangeStatus(int $id)
  166. {
  167. $service = new PosterService(['mall_id' => $this->mall_id]);
  168. return $service->changeStatus($id)
  169. ? $this->success('ok')
  170. : $this->error('修改失败');
  171. }
  172. /**
  173. * 清除海报缓存
  174. *
  175. * @return mixed|null
  176. */
  177. public function actionClearCache()
  178. {
  179. PosterCode::updateAll(['applet_code' => ''], ['mall_id' => $this->mall_id]);
  180. return $this->success();
  181. }
  182. }