CategoryController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace addons\Course\backend\controllers;
  3. use addons\Course\common\models\AddonsCourse;
  4. use addons\Course\common\models\CourseCategory;
  5. use common\enums\StatusEnum;
  6. class CategoryController extends BaseController
  7. {
  8. /**
  9. * @name:
  10. * @msg: 创建/编辑分类
  11. * @param {*}
  12. * @return {*}
  13. */
  14. public function actionEdit()
  15. {
  16. if (\Yii::$app->request->isAjax) {
  17. if (\Yii::$app->request->isGet) {
  18. $cate_id = \Yii::$app->request->get('cate_id');
  19. if (empty($cate_id)) return $this->error('参数错误');
  20. $category = CourseCategory::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $cate_id]);
  21. return $this->success('success', $category);
  22. } else {
  23. $post = \Yii::$app->request->post();
  24. $rules = [
  25. [['cname', 'logo', 'is_navicat'], 'required'],
  26. [['cname', 'logo'], 'string'],
  27. [['cname'], 'string', 'min' => 2, 'max' => 4],
  28. ['is_navicat', 'integer'],
  29. [['id', 'sort',], 'safe'],
  30. ];
  31. $res = \Yii::$app->services->validate->validate($post, $rules);
  32. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  33. $category = CourseCategory::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'cname' => $post['cname']]);
  34. if (!empty($category) && $category->id != $post['id']) {
  35. return $this->error('分类名称已经存在');
  36. }
  37. $params = [
  38. 'cname' => $post['cname'],
  39. 'logo' => $post['logo'],
  40. 'is_navicat' => $post['is_navicat'],
  41. 'sort' => $post['sort'],
  42. ];
  43. if (!empty($post['id'])) {
  44. $params['id'] = $post['id'];
  45. }
  46. $res = CourseCategory::setData($this->mall_id, $params);
  47. if (false === $res) {
  48. return $this->error('操作失败');
  49. }
  50. return $this->success('操作成功');
  51. }
  52. } else {
  53. return $this->render('/category/edit');
  54. }
  55. }
  56. /**
  57. * @name:
  58. * @msg: 切换分类导航状态
  59. * @param {*}
  60. * @return {*}
  61. */
  62. public function actionSwitchNavicat()
  63. {
  64. if (!\Yii::$app->request->isPost) {
  65. return $this->error('请求方式错误');
  66. }
  67. $id = \Yii::$app->request->post('id');
  68. $is_navicat = \Yii::$app->request->post('is_navicat');
  69. if (empty($id) || !isset($is_navicat)) {
  70. return $this->error('参数错误');
  71. }
  72. $params = [
  73. 'is_navicat' => $is_navicat,
  74. 'id' => $id,
  75. ];
  76. $res = CourseCategory::setData($this->mall_id, $params);
  77. if (false === $res) {
  78. return $this->error('操作失败');
  79. }
  80. return $this->success('操作成功');
  81. }
  82. /**
  83. * @name:
  84. * @msg: 删除分类
  85. * @param {*}
  86. * @return {*}
  87. */
  88. public function actionDelete()
  89. {
  90. if (!\Yii::$app->request->isPost) {
  91. return $this->error('请求方式错误');
  92. }
  93. $id = \Yii::$app->request->post('id');
  94. if (empty($id)) {
  95. return $this->error('参数错误');
  96. }
  97. $course = AddonsCourse::find()
  98. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'cat_id' => $id])
  99. ->limit(1)
  100. ->one();
  101. if (!empty($course)) {
  102. return $this->error('当前分类有关联课程,不可删除');
  103. }
  104. $params = [
  105. 'id' => $id,
  106. 'status' => StatusEnum::DELETE,
  107. ];
  108. $res = CourseCategory::setData($this->mall_id, $params);
  109. if (false === $res) {
  110. return $this->error('删除失败');
  111. }
  112. return $this->success('删除成功');
  113. }
  114. }