Files
amb_wechatapp/application/api/controller/course/Group.php
T

351 lines
9.7 KiB
PHP

<?php
namespace app\api\controller\course;
use app\common\controller\Api;
use app\admin\model\course\Group as GroupModel;
use app\api\model\course\Group as ApiGroupModel;
/**
* 课程分组接口
*/
class Group extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new GroupModel;
}
/**
* 获取分组列表(支持指定层级)
*
* @param int $id 指定分组ID,不传则获取所有一级分组
* @return void
*/
public function index()
{
$id = $this->request->param('id', 0);
// 获取课程配置
$courseConfig = \app\common\model\config\System::getConfig('course');
$groupLevel = isset($courseConfig['group_level']) ? intval($courseConfig['group_level']) : 4;
// 获取一级分组
$query = $this->model
->where('parent_id', 0)
->where('status', 1)
->where('uniacid', UNIACID)
->order('sort', 'asc')
->order('id', 'asc');
if ($id) {
$query = $query->where('id', $id);
}
$list = $query->select();
if (empty($list)) {
$this->error(__('No Results were found'));
}
// 获取完整的分类树
$result = [];
foreach ($list as $item) {
$result[] = $this->getGroupTree($item, $groupLevel);
}
$this->success('获取成功', [
'list' => $result,
'group_level' => $groupLevel
]);
}
/**
* 获取所有分组树
*/
public function all()
{
// 获取课程配置
$courseConfig = \app\common\model\config\System::getConfig('course');
$groupLevel = isset($courseConfig['group_level']) ? intval($courseConfig['group_level']) : 4;
$list = $this->model
->where('parent_id', 0)
->where('status', 1)
->where('uniacid', UNIACID)
->order('sort', 'asc')
->order('id', 'asc')
->select();
$result = [];
foreach ($list as $item) {
$result[] = $this->getGroupTree($item, $groupLevel);
}
$this->success('获取成功', $result);
}
/**
* 获取指定层级分组
*/
public function level()
{
$level = $this->request->param('level', 1);
$parentId = $this->request->param('parent_id', 0);
// 获取课程配置
$courseConfig = \app\common\model\config\System::getConfig('course');
$groupLevel = isset($courseConfig['group_level']) ? intval($courseConfig['group_level']) : 4;
if ($level > $groupLevel) {
$this->error('超出最大层级限制,最大支持' . $groupLevel . '级');
}
$list = $this->model
->where('parent_id', $parentId)
->where('status', 1)
->where('uniacid', UNIACID)
->order('sort', 'asc')
->order('id', 'asc')
->select();
$result = [];
foreach ($list as $item) {
$data = [
'id' => $item['id'],
'name' => $item['name'],
'parent_id' => $item['parent_id'],
'image' => $item['image'] ?? '',
'sort' => $item['sort'] ?? 0,
];
$result[] = $data;
}
$this->success('获取成功', $result);
}
/**
* 递归获取分组树
*/
protected function getGroupTree($group, $maxLevel = 4, $currentLevel = 1)
{
$data = [
'id' => $group['id'],
'name' => $group['name'],
'parent_id' => $group['parent_id'],
'image' => $group['image'] ?? '',
'sort' => $group['sort'] ?? 0,
];
$data['is_virtual_pay'] = $this->checkGroupHasVirtualPay($group['id']);
// 如果未达到最大层级,继续获取子分组
if ($currentLevel < $maxLevel) {
$children = $this->model
->where('parent_id', $group['id'])
->where('status', 1)
->where('uniacid', UNIACID)
->order('sort', 'asc')
->order('id', 'asc')
->select();
if (!empty($children)) {
$data['children'] = [];
foreach ($children as $child) {
$data['children'][] = $this->getGroupTree($child, $maxLevel, $currentLevel + 1);
}
}
}
return $data;
}
/**
* 检查分组下是否有虚拟支付课程
* @param int $groupId 分组ID
* @return int 1-有虚拟支付课程 0-无
*/
protected function checkGroupHasVirtualPay($groupId)
{
$virtualPayConfig = \app\common\model\config\System::getConfig('virtual_pay');
if (!$virtualPayConfig || ($virtualPayConfig['status'] ?? 'close') !== 'open') {
return 0;
}
$virtualGoodsTypes = $virtualPayConfig['virtual_goods_types'] ?? [];
if (empty($virtualGoodsTypes) || !is_array($virtualGoodsTypes)) {
return 0;
}
$prefix = \think\Config::get('database.prefix');
$courseTypes = \think\Db::name('course_bind_group')
->alias('gb')
->join($prefix . 'course c', 'gb.course_id = c.id')
->where('gb.group_id', $groupId)
->where('c.status', 1)
->where('c.uniacid', UNIACID)
->column('c.type');
foreach ($courseTypes as $type) {
if (in_array($type, $virtualGoodsTypes, true)) {
return 1;
}
}
return 0;
}
/**
* 获取分组详情
*/
public function detail()
{
$id = $this->request->param('id', 0);
if (!$id) {
$this->error('分组ID不能为空');
}
$group = $this->model
->where('id', $id)
->where('status', 1)
->where('uniacid', UNIACID)
->find();
if (!$group) {
$this->error(__('No Results were found'));
}
$data = [
'id' => $group['id'],
'name' => $group['name'],
'parent_id' => $group['parent_id'],
'image' => $group['image'] ?? '',
'sort' => $group['sort'] ?? 0,
];
// 获取父级信息
if ($group['parent_id'] > 0) {
$parent = $this->model
->where('id', $group['parent_id'])
->where('uniacid', UNIACID)
->find();
$data['parent_name'] = $parent ? $parent['name'] : '';
} else {
$data['parent_name'] = '';
}
$this->success('获取成功', $data);
}
/**
* 获取课程分组列表(兼容旧接口)
* @return void
*/
public function getList(){
$groupId = $this->request->param('group', 0);
$isDeep = $this->request->param('is_deep', 1);
$parentId = $this->request->param('parent_id', 0);
if ($parentId) {
$list = $this->model
->where('parent_id', $parentId)
->where('status', 1)
->where('uniacid', UNIACID)
->order('sort', 'asc')
->order('id', 'asc')
->select();
$this->success('返回成功', $list);
return;
}
$groupIds = [];
if ($groupId) {
if ($isDeep) {
$groupIds = $this->getRelatedGroupIds($groupId);
} else {
$groupIds = [$groupId];
}
}
if (!empty($groupIds)) {
$groupBindModel = new \app\api\model\course\GroupBind();
$list = $groupBindModel->getCourseListByGroupIds($groupIds);
} else {
$apiModel = new ApiGroupModel();
$list = $apiModel->getList();
}
$this->success('返回成功', $list);
}
/**
* 获取分组及其所有相关分组ID(包括上级和下级)
* @param int $groupId
* @return array
*/
protected function getRelatedGroupIds($groupId)
{
$groupIds = [$groupId];
$this->getParentGroupIds($groupId, $groupIds);
$this->getChildGroupIdsRecursive($groupId, $groupIds);
return array_unique($groupIds);
}
/**
* 递归获取上级分组ID
* @param int $groupId
* @param array $groupIds
*/
protected function getParentGroupIds($groupId, &$groupIds)
{
$group = $this->model
->where('id', $groupId)
->where('status', 1)
->where('uniacid', UNIACID)
->find();
if ($group && $group['parent_id'] > 0) {
$parent = $this->model
->where('id', $group['parent_id'])
->where('status', 1)
->where('uniacid', UNIACID)
->find();
if ($parent) {
$groupIds[] = $parent['id'];
$this->getParentGroupIds($parent['id'], $groupIds);
}
}
}
/**
* 递归获取下级分组ID
* @param int $parentId
* @param array $groupIds
*/
protected function getChildGroupIdsRecursive($parentId, &$groupIds)
{
$children = $this->model
->where('parent_id', $parentId)
->where('status', 1)
->where('uniacid', UNIACID)
->column('id');
foreach ($children as $childId) {
$groupIds[] = $childId;
$this->getChildGroupIdsRecursive($childId, $groupIds);
}
}
}