You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
6.1 KiB
233 lines
6.1 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | 学员端知识库控制器
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\api\controller\student;
|
|
|
|
use app\service\api\student\KnowledgeService;
|
|
use core\base\BaseController;
|
|
use think\Response;
|
|
|
|
/**
|
|
* 学员知识库管理控制器
|
|
* 提供知识库相关功能接口
|
|
*/
|
|
class KnowledgeController extends BaseController
|
|
{
|
|
/**
|
|
* 获取知识文章列表
|
|
* @param int $student_id 学员ID
|
|
* @return Response
|
|
*/
|
|
public function getKnowledgeList($student_id)
|
|
{
|
|
$data = $this->request->params([
|
|
['category', ''],
|
|
['page', 1],
|
|
['limit', 10],
|
|
['keyword', '']
|
|
]);
|
|
$data['student_id'] = $student_id;
|
|
|
|
$this->validate($data, [
|
|
'student_id' => 'require|integer|gt:0',
|
|
'page' => 'integer|egt:1',
|
|
'limit' => 'integer|between:1,50'
|
|
]);
|
|
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->getKnowledgeList($data);
|
|
|
|
return success($result, '获取知识文章列表成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取知识分类列表
|
|
* @return Response
|
|
*/
|
|
public function getKnowledgeCategories()
|
|
{
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->getKnowledgeCategories('student');
|
|
|
|
return success($result, '获取知识分类成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取推荐文章
|
|
* @param int $student_id 学员ID
|
|
* @return Response
|
|
*/
|
|
public function getRecommendArticles($student_id)
|
|
{
|
|
$data = $this->request->params([
|
|
['limit', 5]
|
|
]);
|
|
$data['student_id'] = $student_id;
|
|
|
|
$this->validate($data, [
|
|
'student_id' => 'require|integer|gt:0',
|
|
'limit' => 'integer|between:1,20'
|
|
]);
|
|
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->getRecommendArticles($data);
|
|
|
|
return success($result, '获取推荐文章成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取文章详情
|
|
* @param int $id 文章ID
|
|
* @return Response
|
|
*/
|
|
public function getKnowledgeDetail($id)
|
|
{
|
|
$data = $this->request->params([
|
|
['student_id', 0]
|
|
]);
|
|
$data['id'] = $id;
|
|
|
|
$this->validate($data, [
|
|
'id' => 'require|integer|gt:0',
|
|
'student_id' => 'require|integer|gt:0'
|
|
]);
|
|
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->getKnowledgeDetail($data);
|
|
|
|
return success($result, '获取文章详情成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 标记文章已读
|
|
* @return Response
|
|
*/
|
|
public function markArticleRead()
|
|
{
|
|
$data = $this->request->params([
|
|
['article_id', 0],
|
|
['student_id', 0]
|
|
]);
|
|
|
|
$this->validate($data, [
|
|
'article_id' => 'require|integer|gt:0',
|
|
'student_id' => 'require|integer|gt:0'
|
|
]);
|
|
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->markArticleRead($data);
|
|
|
|
return success($result, '标记已读成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 收藏/取消收藏文章
|
|
* @return Response
|
|
*/
|
|
public function toggleArticleFavorite()
|
|
{
|
|
$data = $this->request->params([
|
|
['article_id', 0],
|
|
['student_id', 0],
|
|
['action', 'add'] // add-收藏, remove-取消收藏
|
|
]);
|
|
|
|
$this->validate($data, [
|
|
'article_id' => 'require|integer|gt:0',
|
|
'student_id' => 'require|integer|gt:0',
|
|
'action' => 'require|in:add,remove'
|
|
]);
|
|
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->toggleArticleFavorite($data);
|
|
|
|
return success($result, $data['action'] === 'add' ? '收藏成功' : '取消收藏成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取知识库统计信息
|
|
* @param int $student_id 学员ID
|
|
* @return Response
|
|
*/
|
|
public function getKnowledgeStats($student_id)
|
|
{
|
|
$this->validate(['student_id' => $student_id], [
|
|
'student_id' => 'require|integer|gt:0'
|
|
]);
|
|
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->getKnowledgeStats($student_id);
|
|
|
|
return success($result, '获取知识库统计成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 搜索知识文章
|
|
* @param int $student_id 学员ID
|
|
* @return Response
|
|
*/
|
|
public function searchKnowledgeArticles($student_id)
|
|
{
|
|
$data = $this->request->params([
|
|
['keyword', ''],
|
|
['category', ''],
|
|
['page', 1],
|
|
['limit', 10]
|
|
]);
|
|
$data['student_id'] = $student_id;
|
|
|
|
$this->validate($data, [
|
|
'student_id' => 'require|integer|gt:0',
|
|
'keyword' => 'require|length:1,50',
|
|
'page' => 'integer|egt:1',
|
|
'limit' => 'integer|between:1,50'
|
|
]);
|
|
|
|
try {
|
|
$service = new KnowledgeService();
|
|
$result = $service->searchKnowledgeArticles($data);
|
|
|
|
return success($result, '搜索知识文章成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
}
|