RecommendController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\Course\backend\controllers;
  3. use addons\Course\common\models\AddonsCourse;
  4. use addons\Course\common\models\AddonsCourseDiscuss;
  5. use addons\Course\common\models\AddonsCourseGroups;
  6. use common\enums\StatusEnum;
  7. class RecommendController extends BaseController
  8. {
  9. public function actionIndex()
  10. {
  11. if (\Yii::$app->request->isAjax) {
  12. if (\Yii::$app->request->isGet) {
  13. $params = \Yii::$app->request->get();
  14. $where[] = ['mall_id' => $this->mall_id];
  15. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  16. if (!empty($params['keyword'])) $where[] = ['like', 'gname', $params['keyword'] . '%', false];
  17. $params = [
  18. 'where' => $where,
  19. 'order' => 'sort desc',
  20. ];
  21. $page_data = AddonsCourseGroups::listPage($params);
  22. if (!empty($page_data['list'])) {
  23. foreach ($page_data['list'] as &$item) {
  24. $item['course_number'] = 0;
  25. $cid = explode(',', $item['cid']);
  26. if (!empty($cid)) $item['course_number'] = count($cid);
  27. }
  28. }
  29. return $this->success('success', $page_data);
  30. } else {
  31. return $this->error('请求方式错误');
  32. }
  33. } else {
  34. return $this->render('index');
  35. }
  36. }
  37. public function actionEdit()
  38. {
  39. if (\Yii::$app->request->isAjax) {
  40. if (\Yii::$app->request->isGet) {
  41. $params = \Yii::$app->request->get();
  42. $group_info = '';
  43. if (!empty($params['id'])) $group_info = AddonsCourseGroups::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id]], true);
  44. $course_list = AddonsCourse::lists([
  45. 'where' => [
  46. ['mall_id' => $this->mall_id],
  47. ['status' => StatusEnum::ENABLED]
  48. ],
  49. 'select' => 'id,name'
  50. ]);
  51. if (!empty($group_info['cid'])) $group_info['cid'] = json_decode($group_info['cid'], true);
  52. $data['group_info'] = $group_info;
  53. $data['course_list'] = $course_list;
  54. return $this->success('success', $data);
  55. } else {
  56. $params = \Yii::$app->request->post();
  57. $res = \Yii::$app->services->validate->validate($params, [
  58. [['cid', 'gname', 'sort'], 'required'],
  59. [['id'], 'integer'],
  60. [['logo'], 'safe']
  61. ]);
  62. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  63. $params['mall_id'] = $this->mall_id;
  64. if (!empty($params['cid'])) {
  65. $params['cid'] = json_encode(explode(',', $params['cid']));
  66. }
  67. $res = AddonsCourseGroups::setData($params);
  68. if ($res === false) return $this->error(AddonsCourseGroups::getStaticError());
  69. return $this->success('操作成功');
  70. }
  71. } else {
  72. return $this->render('edit');
  73. }
  74. }
  75. public function actionSwitchUse()
  76. {
  77. $post = \Yii::$app->request->post();
  78. $params['id'] = $post['id'];
  79. $params['mall_id'] = $this->mall_id;
  80. $params['status'] = $post['val'];
  81. $res = AddonsCourseGroups::setData($params);
  82. if ($res === false) return $this->error(AddonsCourseDiscuss::getStaticError());
  83. return $this->success('操作成功');
  84. }
  85. public function actionDelete()
  86. {
  87. $post = \Yii::$app->request->post();
  88. $params['id'] = $post['id'];
  89. $params['mall_id'] = $this->mall_id;
  90. $params['status'] = StatusEnum::DELETE;
  91. $res = AddonsCourseGroups::setData($params);
  92. if ($res === false) return $this->error(AddonsCourseDiscuss::getStaticError());
  93. return $this->success('操作成功');
  94. }
  95. }