hyperf-chat/app/Controller/Api/V1/ArticleController.php

558 lines
16 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2020-11-21 19:53:01 +08:00
declare(strict_types=1);
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
2020-11-04 22:58:49 +08:00
use App\Service\ArticleService;
2020-11-27 19:48:41 +08:00
use App\Service\UploadService;
2020-11-05 17:40:51 +08:00
use App\Support\RedisLock;
2020-11-04 22:58:49 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
2020-11-24 23:23:12 +08:00
use App\Middleware\JWTAuthMiddleware;
2020-11-27 19:48:41 +08:00
use Hyperf\Utils\Str;
2020-11-04 11:57:16 +08:00
2020-11-04 22:58:49 +08:00
/**
* Class ArticleController
*
* @Controller(path="/api/v1/article")
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
2020-11-04 11:57:16 +08:00
class ArticleController extends CController
{
2020-11-04 22:58:49 +08:00
/**
* @Inject
* @var ArticleService
*/
private $articleService;
2020-11-04 11:57:16 +08:00
2020-11-27 19:48:41 +08:00
/**
* @inject
* @var UploadService
*/
private $uploadService;
2020-11-04 22:58:49 +08:00
/**
* 获取笔记分类列表
*
* @RequestMapping(path="article-class", methods="get")
*/
public function getArticleClass()
{
2020-11-13 23:09:56 +08:00
return $this->response->success([
'rows' => $this->articleService->getUserClass($this->uid())
]);
2020-11-04 22:58:49 +08:00
}
/**
* 获取笔记标签列表
*
* @RequestMapping(path="article-tags", methods="get")
*/
public function getArticleTags()
{
2020-11-13 23:09:56 +08:00
return $this->response->success([
'tags' => $this->articleService->getUserTags($this->uid())
]);
2020-11-04 22:58:49 +08:00
}
/**
* 获取笔记列表
*
* @RequestMapping(path="article-list", methods="get")
*/
public function getArticleList()
{
$this->validate($this->request->all(), [
'keyword' => "present",
'find_type' => 'required|in:0,1,2,3,4,5,6',
'cid' => 'present|integer|min:-1',
'page' => 'present|integer|min:1'
]);
// 查询类型 $findType 1:获取近期日记 2:获取星标日记 3:获取指定分类文章 4:获取指定标签文章 5:获取已删除文章 6:关键词搜索
$findType = $this->request->input('find_type', 0);
$keyword = $this->request->input('keyword', '');// 搜索关键词
$cid = $this->request->input('cid', -1);// 分类ID
$page = $this->request->input('page', 1);
$user_id = $this->uid();
$params = [];
$params['find_type'] = $findType;
if (in_array($findType, [3, 4])) {
$params['class_id'] = $cid;
}
if (!empty($keyword)) {
$params['keyword'] = $keyword;
}
return $this->response->success(
2020-11-21 19:53:01 +08:00
$this->articleService->getUserArticleList($user_id, intval($page), 10000, $params)
2020-11-04 22:58:49 +08:00
);
}
/**
* 获取笔记详情
*
* @RequestMapping(path="article-detail", methods="get")
*/
public function getArticleDetail()
{
$this->validate($this->request->all(), [
'article_id' => 'required|integer',
]);
2020-11-05 17:40:51 +08:00
return $this->response->success(
$this->articleService->getArticleDetail(
2020-11-24 23:23:12 +08:00
(int)$this->request->input('article_id'),
2020-11-05 17:40:51 +08:00
$this->uid()
)
2020-11-04 22:58:49 +08:00
);
}
/**
* 添加或编辑笔记分类
*
* @RequestMapping(path="edit-article-class", methods="post")
*/
public function editArticleClass()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'class_id' => 'required|integer',
'class_name' => 'required',
]);
$class_id = $this->articleService->editArticleClass($this->uid(), $params['class_id'], $params['class_name']);
if (!$class_id) {
return $this->response->fail('笔记分类编辑失败...');
}
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $this->response->success(['id' => $class_id]);
2020-11-04 22:58:49 +08:00
}
/**
* 删除笔记分类
*
* @RequestMapping(path="del-article-class", methods="post")
*/
public function delArticleClass()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'class_id' => 'required|integer'
]);
2020-11-24 23:23:12 +08:00
if (!$this->articleService->delArticleClass($this->uid(), (int)$params['class_id'])) {
2020-11-05 17:40:51 +08:00
return $this->response->fail('笔记分类删除失败...');
}
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $this->response->success([], '笔记分类删除成功...');
2020-11-04 22:58:49 +08:00
}
/**
* 笔记分类列表排序接口
*
* @RequestMapping(path="article-class-sort", methods="post")
*/
public function articleClassSort()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'class_id' => 'required|integer',
'sort_type' => 'required|in:1,2'
]);
$lockKey = "article_class_sort:{$params['class_id']}_{$params['sort_type']}";
// 获取Redis锁
2020-11-14 17:37:55 +08:00
if (RedisLock::lock($lockKey, 0, 3)) {
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->articleClassSort($this->uid(), (int)$params['class_id'], (int)$params['sort_type']);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
// 释放Redis锁
RedisLock::release($lockKey, 0);
} else {
$isTrue = false;
}
return $isTrue
? $this->response->success([], '排序完成...')
: $this->response->fail('排序失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 笔记分类合并接口
*
* @RequestMapping(path="merge-article-class", methods="post")
*/
public function mergeArticleClass()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'class_id' => 'required|integer',
'toid' => 'required|integer'
]);
2020-11-04 22:58:49 +08:00
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->mergeArticleClass($this->uid(), (int)$params['class_id'], (int)$params['toid']);
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '合并完成...')
: $this->response->fail('合并完成...');
2020-11-04 22:58:49 +08:00
}
/**
* 添加或编辑笔记标签
*
* @RequestMapping(path="edit-article-tag", methods="post")
*/
public function editArticleTags()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'tag_id' => 'required|integer|min:0',
'tag_name' => 'required'
]);
$id = $this->articleService->editArticleTag(
$this->uid(),
2020-11-24 23:23:12 +08:00
(int)$this->request->post('tag_id', 0),
2020-11-05 17:40:51 +08:00
$this->request->post('tag_name', '')
);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $id
? $this->response->success(['id' => $id])
: $this->response->fail('笔记标签编辑失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 删除笔记标签
*
* @RequestMapping(path="del-article-tag", methods="post")
*/
public function delArticleTags()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'tag_id' => 'required|integer|min:0'
]);
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->delArticleTags($this->uid(), (int)$params['tag_id']);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记标签删除完成...')
: $this->response->fail('笔记标签删除失败...');
2020-11-04 22:58:49 +08:00
}
/**
2020-11-05 17:40:51 +08:00
* 添加或编辑笔记
2020-11-04 22:58:49 +08:00
*
* @RequestMapping(path="edit-article", methods="post")
*/
public function editArticle()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'article_id' => 'required|integer|min:0',
'class_id' => 'required|integer|min:0',
'title' => 'required|max:255',
'content' => 'required',
'md_content' => 'required',
]);
2020-11-24 23:23:12 +08:00
$id = $this->articleService->editArticle($this->uid(), (int)$params['article_id'], [
2020-11-05 17:40:51 +08:00
'title' => $params['title'],
'abstract' => mb_substr(strip_tags($params['content']), 0, 200),
'class_id' => $params['class_id'],
'image' => get_html_images($params['content']),
'md_content' => htmlspecialchars($params['md_content']),
'content' => htmlspecialchars($params['content'])
]);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $id
? $this->response->success(['aid' => $id], '笔记编辑成功...')
: $this->response->fail('笔记编辑失败...', ['id' => null]);
2020-11-04 22:58:49 +08:00
}
/**
* 删除笔记
*
* @RequestMapping(path="delete-article", methods="post")
*/
public function deleteArticle()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'article_id' => 'required|integer|min:0'
]);
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->updateArticleStatus($this->uid(), (int)$params['article_id'], 2);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记删除成功...')
: $this->response->fail('笔记删除失败...');
2020-11-04 22:58:49 +08:00
}
/**
2020-11-05 17:40:51 +08:00
* 恢复删除笔记
2020-11-04 22:58:49 +08:00
*
* @RequestMapping(path="recover-article", methods="post")
*/
public function recoverArticle()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'article_id' => 'required|integer|min:0'
]);
2020-11-04 22:58:49 +08:00
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->updateArticleStatus($this->uid(), (int)$params['article_id'], 1);
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记恢复成功...')
: $this->response->fail('笔记恢复失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 笔记图片上传接口
*
* @RequestMapping(path="upload-article-image", methods="post")
*/
public function uploadArticleImage()
{
2020-11-27 19:48:41 +08:00
$file = $this->request->file('image');
if (!$file->isValid()) {
return $this->response->fail();
}
$ext = $file->getExtension();
if (!in_array($ext, ['jpg', 'png', 'jpeg', 'gif', 'webp'])) {
return $this->response->fail('图片格式错误目前仅支持jpg、png、jpeg、gif和webp');
}
//获取图片信息
$imgInfo = getimagesize($file->getRealPath());
2020-11-04 22:58:49 +08:00
2020-11-27 19:48:41 +08:00
$path = $this->uploadService->media($file, 'media/images/notes/', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
if (!$path) {
return $this->response->fail();
}
return $this->response->success([
'save_path' => get_media_url($path)
]);
2020-11-04 22:58:49 +08:00
}
/**
* 移动笔记至指定分类
*
* @RequestMapping(path="move-article", methods="post")
*/
public function moveArticle()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'article_id' => 'required|integer|min:0',
'class_id' => 'required|integer|min:0'
]);
$isTrue = $this->articleService->moveArticle(
$this->uid(),
$params['article_id'],
$params['class_id']
);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记移动成功...')
: $this->response->fail('笔记移动失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 笔记标记星号接口
*
* @RequestMapping(path="set-asterisk-article", methods="post")
*/
public function setAsteriskArticle()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'article_id' => 'required|integer|min:0',
'type' => 'required|in:1,2'
]);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
$isTrue = $this->articleService->setAsteriskArticle(
$this->uid(),
2020-11-24 23:23:12 +08:00
(int)$params['article_id'],
(int)$params['type']
2020-11-05 17:40:51 +08:00
);
return $isTrue
? $this->response->success([], '笔记标记成功...')
: $this->response->fail('笔记标记失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 更新笔记关联标签ID
*
* @RequestMapping(path="update-article-tag", methods="post")
*/
public function updateArticleTag()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'article_id' => 'required|integer|min:0',
'tags' => 'required|array'
]);
2020-11-04 22:58:49 +08:00
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->updateArticleTag($this->uid(), (int)$params['article_id'], $params['tags']);
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], 'success...')
: $this->response->fail('编辑失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 永久删除笔记文章
*
* @RequestMapping(path="forever-delete-article", methods="post")
*/
public function foreverDelArticle()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'article_id' => 'required|integer|min:0'
]);
2020-11-04 22:58:49 +08:00
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->foreverDelArticle($this->uid(), (int)$params['article_id']);
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记删除成功...')
: $this->response->fail('笔记删除失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 上传笔记附件
*
* @RequestMapping(path="upload-article-annex", methods="post")
*/
public function uploadArticleAnnex()
{
2020-11-27 19:48:41 +08:00
$params = $this->request->inputs(['article_id']);
$this->validate($params, [
'article_id' => 'required|integer|min:0'
]);
2020-11-13 23:09:56 +08:00
$file = $this->request->file('annex');
2020-11-27 19:48:41 +08:00
if (!$file->isValid()) {
return $this->response->fail('上传文件验证失败...');
}
$annex = [
'file_suffix' => $file->getExtension(),
'file_size' => $file->getSize(),
'save_dir' => '',
'original_name' => $file->getClientFilename()
];
$path = $this->uploadService->media($file,
'files/notes/' . date('Ymd'),
"[{$annex['file_suffix']}]" . uniqid() . Str::random(16) . '.' . 'tmp'
);
if (!$path) {
return $this->response->fail();
}
$annex['save_dir'] = $path;
$insId = $this->articleService->insertArticleAnnex($this->uid(), (int)$params['article_id'], $annex);
if (!$insId) {
return $this->response->fail('附件上传失败,请稍后再试...');
}
$annex['id'] = $insId;
return $this->response->success($annex, '笔记附件上传成功...');
2020-11-04 22:58:49 +08:00
}
/**
* 删除笔记附件
*
* @RequestMapping(path="delete-article-annex", methods="post")
*/
public function deleteArticleAnnex()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'annex_id' => 'required|integer|min:0'
]);
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->updateArticleAnnexStatus($this->uid(), (int)$params['annex_id'], 2);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记附件删除成功...')
: $this->response->fail('笔记附件删除失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 恢复笔记附件
*
* @RequestMapping(path="recover-article-annex", methods="post")
*/
public function recoverArticleAnnex()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'annex_id' => 'required|integer|min:0'
]);
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->updateArticleAnnexStatus($this->uid(), (int)$params['annex_id'], 1);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记附件恢复成功...')
: $this->response->fail('笔记附件恢复失败...');
2020-11-04 22:58:49 +08:00
}
/**
* 获取附件回收站列表
*
* @RequestMapping(path="recover-annex-list", methods="get")
*/
public function recoverAnnexList()
{
2020-11-05 17:40:51 +08:00
$rows = $this->articleService->recoverAnnexList($this->uid());
if ($rows) {
$getDay = function ($delete_at) {
$last_time = strtotime('+30 days', strtotime($delete_at));
return (time() > $last_time) ? 0 : diff_date(date('Y-m-d', $last_time), date('Y-m-d'));
};
array_walk($rows, function (&$item) use ($getDay) {
$item['day'] = $getDay($item['deleted_at']);
$item['visible'] = false;
});
}
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $this->response->success(['rows' => $rows]);
2020-11-04 22:58:49 +08:00
}
/**
* 永久删除笔记附件(从已删除附件中永久删除)
*
2020-11-05 17:40:51 +08:00
* @RequestMapping(path="forever-delete-annex", methods="post")
2020-11-04 22:58:49 +08:00
*/
public function foreverDelAnnex()
{
2020-11-05 17:40:51 +08:00
$params = $this->request->all();
$this->validate($params, [
'annex_id' => 'required|integer|min:0'
]);
2020-11-24 23:23:12 +08:00
$isTrue = $this->articleService->foreverDelAnnex($this->uid(), (int)$params['annex_id']);
2020-11-04 22:58:49 +08:00
2020-11-05 17:40:51 +08:00
return $isTrue
? $this->response->success([], '笔记附件删除成功...')
: $this->response->fail('笔记附件删除失败...');
2020-11-04 22:58:49 +08:00
}
2020-11-04 11:57:16 +08:00
}