DefaultController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. namespace addons\Course\backend\controllers;
  3. use addons\Course\common\enums\CourseEnum;
  4. use addons\Course\common\models\AddonsCourse;
  5. use addons\Course\common\models\AddonsCourseBanners;
  6. use addons\Course\common\models\AddonsCourseChapter;
  7. use addons\Course\common\models\AddonsCourseDirectorys;
  8. use addons\Course\common\models\CourseCategory;
  9. use addons\Course\common\services\CourseService;
  10. use common\enums\AddonsEnum;
  11. use common\enums\StatusEnum;
  12. use common\models\goods\Goods;
  13. use common\models\mall\MallHost;
  14. use common\models\user\UserLevel;
  15. use Exception;
  16. use Swoole\Http\Status;
  17. use Yii;
  18. /**
  19. * 默认控制器
  20. *
  21. * Class DefaultController
  22. * @package addons\Course\backend\controllers
  23. */
  24. class DefaultController extends BaseController
  25. {
  26. /**
  27. * 课程列表
  28. *
  29. * @return string
  30. */
  31. public function actionIndex()
  32. {
  33. if (\Yii::$app->request->isAjax) {
  34. if (\Yii::$app->request->isGet) {
  35. $page = \Yii::$app->request->get('page', 1);
  36. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  37. $keyword = \Yii::$app->request->get('keyword');
  38. $start_time = \Yii::$app->request->get('start_time');
  39. $end_time = \Yii::$app->request->get('end_time');
  40. $cate_id = \Yii::$app->request->get('cate_id');
  41. $wheres[] = ["status" => [StatusEnum::ENABLED, StatusEnum::DISABLED], "mall_id" => $this->mall_id];
  42. if (!empty($keyword)) {
  43. $wheres[] = ['like', 'name', $keyword];
  44. }
  45. if (!empty($start_time)) {
  46. $wheres[] = ['>=', 'created_at', strtotime($start_time)];
  47. }
  48. if (!empty($end_time)) {
  49. $wheres[] = ['<=', 'created_at', strtotime($end_time . ' 23:59:59')];
  50. }
  51. if (!empty($cate_id)) {
  52. $categorys = CourseCategory::find()
  53. ->select('id,cname')
  54. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $cate_id])
  55. ->asArray()
  56. ->indexBy('id')
  57. ->all();
  58. }
  59. $params = [
  60. 'select' => 'id,mall_id,cat_id as cate_id,name,cat_id,sell_type,price,line_off_price,sales,logo,is_open_vm,vm_likes,vm_shares,vm_views,likes,shares,views,comments,status,created_at,allow_member_levels,sort',
  61. 'where' => $wheres,
  62. 'order' => 'sort desc,id desc',
  63. 'page' => $page,
  64. 'limit' => $limit,
  65. ];
  66. $page_data = AddonsCourse::listPage($params);
  67. if (empty($page_data['list']) && !is_array($page_data['list'])) return $this->error(AddonsCourse::getStaticError());
  68. if (empty($categorys)) {
  69. $cate_ids = array_column($page_data['list'], 'cate_id');
  70. $categorys = CourseCategory::find()
  71. ->select('id,cname')
  72. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $cate_ids])
  73. ->asArray()
  74. ->indexBy('id')
  75. ->all();
  76. }
  77. foreach ($page_data['list'] as &$list) {
  78. $list['price'] = number_format($list['price'], 2);
  79. $list['line_off_price'] = number_format($list['line_off_price'], 2);
  80. $list['cat_name'] = $categorys[$list['cate_id']]['cname']??'';
  81. if (CourseEnum::COURSE_SELL_TYPE_2 == $list['sell_type']) {
  82. if (!empty($list['allow_member_levels'])) {
  83. $levels = explode(',', $list['allow_member_levels']);
  84. $level_names = UserLevel::find()
  85. ->select('name')
  86. ->where(['mall_id' => $this->mall_id, 'level' => $levels, 'status' => StatusEnum::ENABLED])
  87. ->asArray()
  88. ->column();
  89. } elseif ($list['allow_member_levels'] == 0) {
  90. $level_names = ['全部'];
  91. }
  92. $list['allow_member_levels_name'] = implode(',', $level_names);
  93. }
  94. }
  95. $web_url = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.web_url');
  96. $url_info = explode('?', $web_url);
  97. $page_data['h5_domain'] = MallHost::getHost($this->mall_id,'h5_url').'/#/';
  98. $page_data['mall_sign'] = substr($url_info[1], mb_strlen('sign='));
  99. return $this->success('success', $page_data);
  100. } else {
  101. return $this->error('请求方式错误');
  102. }
  103. } else {
  104. return $this->render('/course/index');
  105. }
  106. }
  107. /**
  108. * @name:
  109. * @msg: 基础设置
  110. * @param {*}
  111. * @return {*}
  112. */
  113. /* public function actionSettings()
  114. {
  115. if (\Yii::$app->request->isAjax) {
  116. if (\Yii::$app->request->isGet) {
  117. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, 'basic');
  118. return $this->success('success', ['basic' => $settings]);
  119. } else {
  120. $post = \Yii::$app->request->post();
  121. $post['basic']['index_link'] = 'plugins/course/index';
  122. unset($post['_csrf']);
  123. $settings = \Yii::$app->services->setting->addons->set($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, $post);
  124. return $this->success('操作成功');
  125. }
  126. } else {
  127. return $this->render('/setting/index');
  128. }
  129. } */
  130. /**
  131. * @name:
  132. * @msg: 课程分类列表
  133. * @param {*}
  134. * @return {*}
  135. */
  136. public function actionCategorys()
  137. {
  138. if (\Yii::$app->request->isAjax) {
  139. if (\Yii::$app->request->isGet) {
  140. $keyword = \Yii::$app->request->get('keyword');
  141. $page = \Yii::$app->request->get('page', 1);
  142. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  143. $wheres[] = ['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id];
  144. if (!empty($keyword)) {
  145. $wheres[] = ['like', 'cname', $keyword];
  146. }
  147. $params = [
  148. 'select' => 'id,mall_id,cname,logo,sort,is_navicat,created_at',
  149. 'order' => 'sort asc,created_at desc',
  150. 'where' => $wheres,
  151. 'page' => $page,
  152. 'limit' => $limit,
  153. ];
  154. $list = CourseCategory::listPage($params);
  155. if (false === $list['list']) {
  156. return $this->error(CourseCategory::getStaticError());
  157. }
  158. return $this->success('success', $list);
  159. } else {
  160. }
  161. } else {
  162. return $this->render('/category/index');
  163. }
  164. }
  165. /**
  166. * @name:
  167. * @msg: 章节列表
  168. * @param {*}
  169. * @return {*}
  170. */
  171. public function actionChapters()
  172. {
  173. if (\Yii::$app->request->isAjax) {
  174. if (\Yii::$app->request->isGet) {
  175. $keyword = \Yii::$app->request->get('keyword');
  176. $course_id = \Yii::$app->request->get('course_id');
  177. $dir_id = \Yii::$app->request->get('dir_id');
  178. $parent_id = \Yii::$app->request->get('parent_id');
  179. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  180. $page = \Yii::$app->request->get('page');
  181. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  182. if (!empty($course_id)) {
  183. $wheres[] = ['course_id' => $course_id];
  184. $course_info = AddonsCourse::find()
  185. ->select('id,name as course_name,logo,introduce,price,status,sales')
  186. ->where(['mall_id' => $this->mall_id, 'id' => $course_id])
  187. ->andWhere(['!=', 'status', StatusEnum::DELETE])
  188. ->asArray()
  189. ->limit(1)
  190. ->one();
  191. if (!empty($course_info)) {
  192. $course_info['price'] = number_format($course_info['price'], 2);
  193. } else {
  194. $course_info = [];
  195. }
  196. } else {
  197. $course_info = [];
  198. }
  199. if (!empty($keyword)) {
  200. $wheres[] = ['like', 'name', $keyword];
  201. }
  202. if (!empty($dir_id)) {
  203. $all_dir_ids[] = $dir_id;
  204. if (0 == $parent_id) {
  205. // 点击了一级目录,获取当前目录下以及其所有子级目录下的所有的章节
  206. $first_level_dir = AddonsCourseDirectorys::find()
  207. ->select('id,name')
  208. ->where(['mall_id' => $this->mall_id, 'parent_id' => $dir_id, 'status' => StatusEnum::ENABLED])
  209. ->asArray()
  210. ->all();
  211. // dd($first_level_dir);
  212. if (!empty($first_level_dir)) {
  213. $dir_ids = array_column($first_level_dir, 'id');
  214. $all_dir_ids = array_merge($all_dir_ids, $dir_ids);
  215. }
  216. }
  217. $wheres[] = ['dir_id' => $all_dir_ids];
  218. } elseif ($dir_id == 0 && is_numeric($dir_id)) {
  219. // 未分类的情况
  220. $wheres[] = ['dir_id' => 0];
  221. }
  222. $params = [
  223. 'select' => 'id,course_id,dir_id,name,video_url,type,goods_list,can_see,can_see_time,views,sort,thumb_url,created_at,video_length,select_type',
  224. 'where' => $wheres,
  225. 'order' => 'sort desc,created_at desc',
  226. 'page' => $page,
  227. 'limit' => $limit,
  228. ];
  229. $page_data = AddonsCourseChapter::listPage($params);
  230. if (false === $page_data['list']) {
  231. return $this->error(AddonsCourseChapter::getStaticError());
  232. }
  233. if (!empty($page_data['list'])) {
  234. // 相关课程数据
  235. $course_ids = array_unique(array_column($page_data['list'], 'course_id'));
  236. $courses = AddonsCourse::find()
  237. ->select('id,name as course_name')
  238. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $course_ids])
  239. ->asArray()
  240. ->indexBy('id')
  241. ->all();
  242. // 关联商品数据
  243. $good_is_strs = array_unique(array_column($page_data['list'], 'goods_list'));
  244. $good_ids = array_map(function ($val) {
  245. $ids = explode(',', $val);
  246. return $ids[0];
  247. }, $good_is_strs);
  248. $goods = Goods::find()
  249. ->select('id,goods_name,cover_pic,price')
  250. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids])
  251. ->asArray()
  252. ->indexBy('id')
  253. ->all();
  254. // 相关目录数据
  255. if (!empty($all_dir_ids)) {
  256. $dir_ids = $all_dir_ids;
  257. } else {
  258. $dir_ids = array_unique(array_column($page_data['list'], 'dir_id'));
  259. }
  260. $directorys = AddonsCourseDirectorys::find()
  261. ->select('id,name')
  262. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $dir_ids])
  263. ->asArray()
  264. ->indexBy('id')
  265. ->all();
  266. foreach ($page_data['list'] as &$list) {
  267. $course = $courses[$list['course_id']] ?? [];
  268. $directory = $directorys[$list['dir_id']] ?? [];
  269. $list['course_name'] = $course['course_name'] ?? '';
  270. if (0 == $list['dir_id']) {
  271. $list['directory_name'] = '未分类';
  272. } else {
  273. $list['directory_name'] = $directory['name'];
  274. }
  275. $good_id = explode(',', $list['goods_list'])[0];
  276. $good = $goods[$good_id] ?? [];
  277. if (!empty($good)) {
  278. $list['goods_info'][] = $good;
  279. } else {
  280. $list['goods_info'] = [];
  281. }
  282. }
  283. }
  284. // dd($page_data);
  285. $page_data['course_info'] = $course_info;
  286. return $this->success('success', $page_data);
  287. } else {
  288. }
  289. } else {
  290. return $this->render('/chapter/index');
  291. }
  292. }
  293. /**
  294. * @name:
  295. * @msg: 轮播图
  296. * @param {*}
  297. * @return {*}
  298. */
  299. public function actionBannars()
  300. {
  301. if (\Yii::$app->request->isAjax) {
  302. if (\Yii::$app->request->isGet) {
  303. $keyword = \Yii::$app->request->get('keyword');
  304. $page = \Yii::$app->request->get('page', 1);
  305. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  306. $where[] = ['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id];
  307. if (!empty($keyword)) {
  308. $where[] = ['like', 'title', $keyword];
  309. }
  310. $params = [
  311. 'order' => 'sort asc,created_at desc',
  312. 'where' => $where,
  313. 'page' => $page,
  314. 'limit' => $limit,
  315. ];
  316. $list = AddonsCourseBanners::listPage($params);
  317. if (false === $list['list']) {
  318. return $this->error(AddonsCourseBanners::getStaticError());
  319. }
  320. return $this->success('success', $list);
  321. } else {
  322. }
  323. } else {
  324. return $this->render('/bannar/index');
  325. }
  326. }
  327. //查找用户
  328. public function actionSearchUser()
  329. {
  330. $request = Yii::$app->request;
  331. if ($request->isAjax && $request->isGet) {
  332. try {
  333. $get = $request->get();
  334. $res = Yii::$app->services->validate->validate($get, [
  335. [['keyword'], 'string'],
  336. ]);
  337. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  338. $list = (new CourseService(['mall_id' => $this->mall_id]))->searchUser($get);
  339. return $this->success('获取成功', ['list' => $list]);
  340. } catch (\Exception $e) {
  341. return $this->error($e->getMessage());
  342. }
  343. }
  344. }
  345. public function actionGiveUserCourse()
  346. {
  347. $request = Yii::$app->request;
  348. if ($request->isAjax && $request->isPost) {
  349. try {
  350. $post = $request->post();
  351. $res = Yii::$app->services->validate->validate($post, [
  352. [['id', 'user_id'], 'required'],
  353. [['id', 'user_id'], 'integer'],
  354. ]);
  355. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  356. $res = (new CourseService(['mall_id' => $this->mall_id]))->giveUserCourse($post);
  357. return $this->success('发放成功');
  358. } catch (\Exception $e) {
  359. return $this->error($e->getMessage());
  360. }
  361. }
  362. }
  363. }