| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace addons\Course\backend\controllers;
- use addons\Course\common\models\AddonsCourseBanners;
- use common\enums\StatusEnum;
- class BannarController extends BaseController
- {
- 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 = AddonsCourseBanners::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 = [
- [['title', 'sort', 'is_show', 'pic_path'], 'required'],
- [['title', '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 = AddonsCourseBanners::setData($this->mall_id, $post);
- if (false === $res) {
- return $this->error(AddonsCourseBanners::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 = AddonsCourseBanners::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 = AddonsCourseBanners::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('删除成功');
- }
- }
|