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('删除成功'); } }