| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace backend\modules\mall\controllers;
- use backend\controllers\MallController;
- use backend\modules\mall\services\PosterService;
- use common\enums\StatusEnum;
- use common\models\common\MallSetting;
- use common\models\common\PosterCode;
- use common\models\mall\Poster;
- use Yii;
- use yii\helpers\Json;
- /**
- * 装修-海报列表
- *
- * Class PosterController
- * @package backend\modules\mall\PosterController
- */
- class PosterController extends MallController
- {
- /**
- * 海报列表
- * @return mixed|string|void
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->Post();
- $res = Yii::$app->services->validate->validate($params, [
- [['cycle', 'keyword'], 'string'],
- [['page', 'limit'], 'integer'],
- [['cycle', 'keyword', 'page', 'limit', 'sort_type'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['<>', 'status', StatusEnum::DELETE];
- // 统计周期
- if (!empty($params['cycle'])) {
- $begTime = strtotime(date("Y-m-d", strtotime("-" . $params['cycle'] ." day")));
- $endTime = time();
- $where[] = ['between', 'created_at', $begTime, $endTime];
- }
- // 关键字搜索
- !empty($params['keyword']) && $where[] = [
- 'or',
- ['like', 'name', $params['keyword']],
- ['like', 'key_word', $params['keyword']]
- ];
- $order = 'sort ' .$params["sort_type"]. ', id desc';
- $select = ['id', 'status', 'name', 'key_word', 'scan_code_num', 'poster_content', 'created_at','share_url','look_num', 'sort'];
- $params = ['select' => $select, 'where' => $where, 'order' => $order];
- $data = Poster::listPage($params, false, true);
- $this->success('获取成功', $data);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 编辑海报
- */
- public function actionEdit()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = $request->Post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name', 'poster_content','share_url'], 'required'],
- [['name', 'key_word','share_url'], 'string'],
- [['id'], 'integer'],
- [['id', 'key_word','sort'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- //share_url 限制背景图格式:只能png与jpeg格式
- $share_url_info = pathinfo($params['share_url']);
- if(!in_array($share_url_info['extension'],['png'])){
- return $this->error('海报背景请选择png格式');
- }
- // 保存数据
- $res = Poster::setData($this->mall_id, $params);
- if ($res === false) return $this->error(Poster::getStaticError());
- $this->success('保存成功');
- } else {
- $params = $request->Get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = Poster::getOne([['mall_id' => $this->mall_id], ['id' => $params['id']]], true);
- if (!empty($detail['poster_content'])) {
- $poster_content = Json::decode($detail['poster_content'], true);
- $detail['poster_content'] = $poster_content['share'];
- }
- $this->success('获取成功', $detail);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 数据操作
- */
- public function actionOperate()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->Post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['type'], 'string'],
- [['id', 'status'], 'integer'],
- [['status'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取海报信息
- $detail = Poster::findOne(['id' => $params['id'], 'mall_id' => $this->mall_id]);
- empty($detail) && $this->error('海报查询出错');
- // 根据类型进行对应的更改状态或删除操作
- switch ($params['type']) {
- case 'change_status':
- $detail->status = $params['status'];
- break;
- case 'destroy':
- $detail->status = StatusEnum::DELETE;
- break;
- default :
- $this->error('类型有误');
- break;
- }
- $res = $detail->save(false);
- if($res === false) $this->error('修改失败');
- $this->success('修改成功');
- }
- }
- }
- /**
- * 获取海报设置
- * @Author: lun
- * @DateTime: 2023/2/28 0028 14:12
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionSetting()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->Post();
- $res = Yii::$app->services->setting->mall->set($this->mall_id, $params);
- if ($res === false) return $this->error(Yii::$app->services->setting->mall->getError());
- return $this->success('保存成功');
- }
- if (Yii::$app->request->isGet) {
- $data = MallSetting::getConfByGroup($this->mall_id, 'poster_setting', true);
- return $this->success('获取成功', $data);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 修改海报状态
- *
- * @param integer $id
- * @return string
- */
- public function actionChangeStatus(int $id)
- {
- $service = new PosterService(['mall_id' => $this->mall_id]);
- return $service->changeStatus($id)
- ? $this->success('ok')
- : $this->error('修改失败');
- }
- /**
- * 清除海报缓存
- *
- * @return mixed|null
- */
- public function actionClearCache()
- {
- PosterCode::updateAll(['applet_code' => ''], ['mall_id' => $this->mall_id]);
- return $this->success();
- }
- }
|