| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace addons\Course\backend\controllers;
- use addons\Course\common\models\AddonsCourse;
- use addons\Course\common\models\CourseCategory;
- use common\enums\StatusEnum;
- class CategoryController extends BaseController
- {
- /**
- * @name:
- * @msg: 创建/编辑分类
- * @param {*}
- * @return {*}
- */
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $cate_id = \Yii::$app->request->get('cate_id');
- if (empty($cate_id)) return $this->error('参数错误');
- $category = CourseCategory::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $cate_id]);
- return $this->success('success', $category);
- } else {
- $post = \Yii::$app->request->post();
- $rules = [
- [['cname', 'logo', 'is_navicat'], 'required'],
- [['cname', 'logo'], 'string'],
- [['cname'], 'string', 'min' => 2, 'max' => 4],
- ['is_navicat', 'integer'],
- [['id', 'sort',], 'safe'],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $category = CourseCategory::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'cname' => $post['cname']]);
- if (!empty($category) && $category->id != $post['id']) {
- return $this->error('分类名称已经存在');
- }
- $params = [
- 'cname' => $post['cname'],
- 'logo' => $post['logo'],
- 'is_navicat' => $post['is_navicat'],
- 'sort' => $post['sort'],
- ];
- if (!empty($post['id'])) {
- $params['id'] = $post['id'];
- }
- $res = CourseCategory::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('操作失败');
- }
- return $this->success('操作成功');
- }
- } else {
- return $this->render('/category/edit');
- }
- }
- /**
- * @name:
- * @msg: 切换分类导航状态
- * @param {*}
- * @return {*}
- */
- public function actionSwitchNavicat()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->post('id');
- $is_navicat = \Yii::$app->request->post('is_navicat');
- if (empty($id) || !isset($is_navicat)) {
- return $this->error('参数错误');
- }
- $params = [
- 'is_navicat' => $is_navicat,
- 'id' => $id,
- ];
- $res = CourseCategory::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('操作失败');
- }
- return $this->success('操作成功');
- }
- /**
- * @name:
- * @msg: 删除分类
- * @param {*}
- * @return {*}
- */
- public function actionDelete()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->post('id');
- if (empty($id)) {
- return $this->error('参数错误');
- }
- $course = AddonsCourse::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'cat_id' => $id])
- ->limit(1)
- ->one();
- if (!empty($course)) {
- return $this->error('当前分类有关联课程,不可删除');
- }
- $params = [
- 'id' => $id,
- 'status' => StatusEnum::DELETE,
- ];
- $res = CourseCategory::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('删除失败');
- }
- return $this->success('删除成功');
- }
- }
|