| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace addons\Bargain\backend\controllers;
- use addons\Bargain\common\models\BargainBanners;
- use common\enums\StatusEnum;
- class BannerController extends BaseController
- {
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $keyword = \Yii::$app->request->get('keyword');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $where[] = ['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id];
- if (!empty($keyword)) {
- $where[] = ['like', 'title', $keyword];
- }
- $params = [
- 'order' => 'sort desc,created_at desc',
- 'where' => $where,
- 'page' => $page,
- 'limit' => $limit,
- ];
- $list = BargainBanners::listPage($params);
- if (false === $list['list']) {
- return $this->error(BargainBanners::getStaticError());
- }
- return $this->success('success', $list);
- } else {
- }
- } else {
- return $this->render('/bannar/index');
- }
- }
- public function actionStorage()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $id = \Yii::$app->request->get('id');
- if (empty($id)) {
- // 添加
- } else {
- // 编辑
- $bannar_info = BargainBanners::findOne(['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
- $bannar_info['pic_link'] = json_decode($bannar_info['pic_link'], true);
- return $this->success('操作成功', $bannar_info);
- }
- } else {
- $post = \Yii::$app->request->post();
- $rules = [
- [['sort', 'is_show', 'pic_path'], 'required'],
- [['pic_path'], 'string'],
- [['sort', 'is_show'], 'integer'],
- [['id', 'pic_link'], 'safe'],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $res = BargainBanners::setData($this->mall_id, $post);
- if (false === $res) {
- return $this->error(BargainBanners::getStaticError());
- }
- return $this->success('操作成功');
- }
- } else {
- return $this->render('/bannar/add-edit');
- }
- }
- public function actionSwitchStatus()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->post('id');
- $is_show = \Yii::$app->request->post('val');
- if (empty($id) || (empty($is_show) && 0 != $is_show)) {
- return $this->error('参数错误');
- }
- $model = BargainBanners::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- $model->is_show = $is_show;
- $model->updated_at = time();
- $res = $model->save();
- // dd($res);
- if (false === $res) {
- return $this->error('操作失败');
- } else {
- return $this->success('操作成功');
- }
- }
- public function actionDelete()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->post('id');
- if (empty($id)) {
- return $this->error('参数错误');
- }
- $model = BargainBanners::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- $model->updated_at = time();
- $model->status = StatusEnum::DELETE;
- $res = $model->save();
- if (false === $res) {
- return $this->error('删除失败');
- }
- return $this->success('删除成功');
- }
- }
|