| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
- namespace addons\Course\backend\controllers;
- use addons\Course\common\enums\CourseEnum;
- use addons\Course\common\models\AddonsCourse;
- use addons\Course\common\models\AddonsCourseBanners;
- use addons\Course\common\models\AddonsCourseChapter;
- use addons\Course\common\models\AddonsCourseDirectorys;
- use addons\Course\common\models\CourseCategory;
- use addons\Course\common\services\CourseService;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\mall\MallHost;
- use common\models\user\UserLevel;
- use Exception;
- use Swoole\Http\Status;
- use Yii;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Course\backend\controllers
- */
- class DefaultController extends BaseController
- {
- /**
- * 课程列表
- *
- * @return string
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $keyword = \Yii::$app->request->get('keyword');
- $start_time = \Yii::$app->request->get('start_time');
- $end_time = \Yii::$app->request->get('end_time');
- $cate_id = \Yii::$app->request->get('cate_id');
- $wheres[] = ["status" => [StatusEnum::ENABLED, StatusEnum::DISABLED], "mall_id" => $this->mall_id];
- if (!empty($keyword)) {
- $wheres[] = ['like', 'name', $keyword];
- }
- if (!empty($start_time)) {
- $wheres[] = ['>=', 'created_at', strtotime($start_time)];
- }
- if (!empty($end_time)) {
- $wheres[] = ['<=', 'created_at', strtotime($end_time . ' 23:59:59')];
- }
- if (!empty($cate_id)) {
- $categorys = CourseCategory::find()
- ->select('id,cname')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $cate_id])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- $params = [
- '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',
- 'where' => $wheres,
- 'order' => 'sort desc,id desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $page_data = AddonsCourse::listPage($params);
- if (empty($page_data['list']) && !is_array($page_data['list'])) return $this->error(AddonsCourse::getStaticError());
- if (empty($categorys)) {
- $cate_ids = array_column($page_data['list'], 'cate_id');
- $categorys = CourseCategory::find()
- ->select('id,cname')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $cate_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- foreach ($page_data['list'] as &$list) {
- $list['price'] = number_format($list['price'], 2);
- $list['line_off_price'] = number_format($list['line_off_price'], 2);
- $list['cat_name'] = $categorys[$list['cate_id']]['cname']??'';
- if (CourseEnum::COURSE_SELL_TYPE_2 == $list['sell_type']) {
- if (!empty($list['allow_member_levels'])) {
- $levels = explode(',', $list['allow_member_levels']);
- $level_names = UserLevel::find()
- ->select('name')
- ->where(['mall_id' => $this->mall_id, 'level' => $levels, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->column();
- } elseif ($list['allow_member_levels'] == 0) {
- $level_names = ['全部'];
- }
- $list['allow_member_levels_name'] = implode(',', $level_names);
- }
- }
- $web_url = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.web_url');
- $url_info = explode('?', $web_url);
- $page_data['h5_domain'] = MallHost::getHost($this->mall_id,'h5_url').'/#/';
- $page_data['mall_sign'] = substr($url_info[1], mb_strlen('sign='));
- return $this->success('success', $page_data);
- } else {
- return $this->error('请求方式错误');
- }
- } else {
- return $this->render('/course/index');
- }
- }
- /**
- * @name:
- * @msg: 基础设置
- * @param {*}
- * @return {*}
- */
- /* public function actionSettings()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $settings = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, 'basic');
- return $this->success('success', ['basic' => $settings]);
- } else {
- $post = \Yii::$app->request->post();
- $post['basic']['index_link'] = 'plugins/course/index';
- unset($post['_csrf']);
- $settings = \Yii::$app->services->setting->addons->set($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, $post);
- return $this->success('操作成功');
- }
- } else {
- return $this->render('/setting/index');
- }
- } */
- /**
- * @name:
- * @msg: 课程分类列表
- * @param {*}
- * @return {*}
- */
- public function actionCategorys()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $keyword = \Yii::$app->request->get('keyword');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $wheres[] = ['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id];
- if (!empty($keyword)) {
- $wheres[] = ['like', 'cname', $keyword];
- }
- $params = [
- 'select' => 'id,mall_id,cname,logo,sort,is_navicat,created_at',
- 'order' => 'sort asc,created_at desc',
- 'where' => $wheres,
- 'page' => $page,
- 'limit' => $limit,
- ];
- $list = CourseCategory::listPage($params);
- if (false === $list['list']) {
- return $this->error(CourseCategory::getStaticError());
- }
- return $this->success('success', $list);
- } else {
- }
- } else {
- return $this->render('/category/index');
- }
- }
- /**
- * @name:
- * @msg: 章节列表
- * @param {*}
- * @return {*}
- */
- public function actionChapters()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $keyword = \Yii::$app->request->get('keyword');
- $course_id = \Yii::$app->request->get('course_id');
- $dir_id = \Yii::$app->request->get('dir_id');
- $parent_id = \Yii::$app->request->get('parent_id');
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $page = \Yii::$app->request->get('page');
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
- if (!empty($course_id)) {
- $wheres[] = ['course_id' => $course_id];
- $course_info = AddonsCourse::find()
- ->select('id,name as course_name,logo,introduce,price,status,sales')
- ->where(['mall_id' => $this->mall_id, 'id' => $course_id])
- ->andWhere(['!=', 'status', StatusEnum::DELETE])
- ->asArray()
- ->limit(1)
- ->one();
- if (!empty($course_info)) {
- $course_info['price'] = number_format($course_info['price'], 2);
- } else {
- $course_info = [];
- }
- } else {
- $course_info = [];
- }
- if (!empty($keyword)) {
- $wheres[] = ['like', 'name', $keyword];
- }
- if (!empty($dir_id)) {
- $all_dir_ids[] = $dir_id;
- if (0 == $parent_id) {
- // 点击了一级目录,获取当前目录下以及其所有子级目录下的所有的章节
- $first_level_dir = AddonsCourseDirectorys::find()
- ->select('id,name')
- ->where(['mall_id' => $this->mall_id, 'parent_id' => $dir_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->all();
- // dd($first_level_dir);
- if (!empty($first_level_dir)) {
- $dir_ids = array_column($first_level_dir, 'id');
- $all_dir_ids = array_merge($all_dir_ids, $dir_ids);
- }
- }
- $wheres[] = ['dir_id' => $all_dir_ids];
- } elseif ($dir_id == 0 && is_numeric($dir_id)) {
- // 未分类的情况
- $wheres[] = ['dir_id' => 0];
- }
- $params = [
- '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',
- 'where' => $wheres,
- 'order' => 'sort desc,created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $page_data = AddonsCourseChapter::listPage($params);
- if (false === $page_data['list']) {
- return $this->error(AddonsCourseChapter::getStaticError());
- }
- if (!empty($page_data['list'])) {
- // 相关课程数据
- $course_ids = array_unique(array_column($page_data['list'], 'course_id'));
- $courses = AddonsCourse::find()
- ->select('id,name as course_name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $course_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- // 关联商品数据
- $good_is_strs = array_unique(array_column($page_data['list'], 'goods_list'));
- $good_ids = array_map(function ($val) {
- $ids = explode(',', $val);
- return $ids[0];
- }, $good_is_strs);
- $goods = Goods::find()
- ->select('id,goods_name,cover_pic,price')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- // 相关目录数据
- if (!empty($all_dir_ids)) {
- $dir_ids = $all_dir_ids;
- } else {
- $dir_ids = array_unique(array_column($page_data['list'], 'dir_id'));
- }
- $directorys = AddonsCourseDirectorys::find()
- ->select('id,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $dir_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($page_data['list'] as &$list) {
- $course = $courses[$list['course_id']] ?? [];
- $directory = $directorys[$list['dir_id']] ?? [];
- $list['course_name'] = $course['course_name'] ?? '';
- if (0 == $list['dir_id']) {
- $list['directory_name'] = '未分类';
- } else {
- $list['directory_name'] = $directory['name'];
- }
- $good_id = explode(',', $list['goods_list'])[0];
- $good = $goods[$good_id] ?? [];
- if (!empty($good)) {
- $list['goods_info'][] = $good;
- } else {
- $list['goods_info'] = [];
- }
- }
- }
- // dd($page_data);
- $page_data['course_info'] = $course_info;
- return $this->success('success', $page_data);
- } else {
- }
- } else {
- return $this->render('/chapter/index');
- }
- }
- /**
- * @name:
- * @msg: 轮播图
- * @param {*}
- * @return {*}
- */
- public function actionBannars()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $keyword = \Yii::$app->request->get('keyword');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $where[] = ['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id];
- if (!empty($keyword)) {
- $where[] = ['like', 'title', $keyword];
- }
- $params = [
- 'order' => 'sort asc,created_at desc',
- 'where' => $where,
- 'page' => $page,
- 'limit' => $limit,
- ];
- $list = AddonsCourseBanners::listPage($params);
- if (false === $list['list']) {
- return $this->error(AddonsCourseBanners::getStaticError());
- }
- return $this->success('success', $list);
- } else {
- }
- } else {
- return $this->render('/bannar/index');
- }
- }
- //查找用户
- public function actionSearchUser()
- {
- $request = Yii::$app->request;
- if ($request->isAjax && $request->isGet) {
- try {
- $get = $request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['keyword'], 'string'],
- ]);
- if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
- $list = (new CourseService(['mall_id' => $this->mall_id]))->searchUser($get);
- return $this->success('获取成功', ['list' => $list]);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- public function actionGiveUserCourse()
- {
- $request = Yii::$app->request;
- if ($request->isAjax && $request->isPost) {
- try {
- $post = $request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['id', 'user_id'], 'required'],
- [['id', 'user_id'], 'integer'],
- ]);
- if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
- $res = (new CourseService(['mall_id' => $this->mall_id]))->giveUserCourse($post);
- return $this->success('发放成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- }
|