2020-11-04 11:57:16 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller\Api\V1;
|
|
|
|
|
|
2021-09-04 19:13:25 +08:00
|
|
|
|
use App\Helpers\DateHelper;
|
|
|
|
|
use App\Helpers\StringHelper;
|
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-29 14:44:11 +08:00
|
|
|
|
use App\Service\ArticleService;
|
2021-06-30 19:27:49 +08:00
|
|
|
|
use League\Flysystem\Filesystem;
|
2021-04-22 16:14:34 +08:00
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2021-05-20 22:56:55 +08:00
|
|
|
|
use App\Cache\Repository\LockRedis;
|
2020-11-04 11:57:16 +08:00
|
|
|
|
|
2020-11-04 22:58:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* Class ArticleController
|
2021-07-05 21:52:44 +08:00
|
|
|
|
* @Controller(prefix="/api/v1/article")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
* @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-04 22:58:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取笔记分类列表
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="classifys", methods="get")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function getArticleClass(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2021-08-27 23:50:40 +08:00
|
|
|
|
$rows = $this->articleService->getUserClass($this->uid());
|
|
|
|
|
foreach ($rows as &$row) {
|
|
|
|
|
$row['count'] = is_null($row['count']) ? 0 : $row['count'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success(['rows' => $rows]);
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取笔记标签列表
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="tags", methods="get")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function getArticleTags(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取笔记列表
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="search", methods="get")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function getArticleList(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 17:47:25 +08:00
|
|
|
|
$params1 = $this->request->inputs(['keyword', 'find_type', 'cid', 'page']);
|
|
|
|
|
$this->validate($params1, [
|
|
|
|
|
// 搜索关键词
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'keyword' => "present",
|
2020-12-01 17:47:25 +08:00
|
|
|
|
// 查询类型 $findType 1:获取近期日记 2:获取星标日记 3:获取指定分类文章 4:获取指定标签文章 5:获取已删除文章 6:关键词搜索
|
2020-11-04 22:58:49 +08:00
|
|
|
|
'find_type' => 'required|in:0,1,2,3,4,5,6',
|
2020-12-01 17:47:25 +08:00
|
|
|
|
// 分类ID
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'cid' => 'present|integer|min:-1',
|
|
|
|
|
'page' => 'present|integer|min:1'
|
2020-11-04 22:58:49 +08:00
|
|
|
|
]);
|
|
|
|
|
|
2021-04-20 16:30:57 +08:00
|
|
|
|
$params = [];
|
2020-12-01 17:47:25 +08:00
|
|
|
|
$params['find_type'] = $params1['find_type'];
|
|
|
|
|
if (in_array($params1['find_type'], [3, 4])) {
|
|
|
|
|
$params['class_id'] = $params1['cid'];
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:47:25 +08:00
|
|
|
|
if (!empty($params1['keyword'])) {
|
|
|
|
|
$params['keyword'] = $params1['keyword'];
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success(
|
2020-12-01 17:47:25 +08:00
|
|
|
|
$this->articleService->getUserArticleList($this->uid(), 1, 10000, $params)
|
2020-11-04 22:58:49 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取笔记详情
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="detail", methods="get")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function getArticleDetail(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 17:47:25 +08:00
|
|
|
|
$params = $this->request->inputs(['article_id']);
|
|
|
|
|
$this->validate($params, [
|
2020-11-29 17:39:24 +08:00
|
|
|
|
'article_id' => 'required|integer'
|
2020-11-04 22:58:49 +08:00
|
|
|
|
]);
|
|
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
|
return $this->response->success(
|
2020-12-01 17:47:25 +08:00
|
|
|
|
$this->articleService->getArticleDetail((int)$params['article_id'], $this->uid())
|
2020-11-04 22:58:49 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加或编辑笔记分类
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="classify/editor", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function editArticleClass(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['class_id', 'class_name']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$this->validate($params, [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'class_id' => 'required|integer',
|
2021-01-04 17:37:14 +08:00
|
|
|
|
'class_name' => 'required|max:20'
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$class_id = $this->articleService->editArticleClass($this->uid(), $params['class_id'], $params['class_name']);
|
|
|
|
|
if (!$class_id) {
|
2021-05-13 18:01:34 +08:00
|
|
|
|
return $this->response->fail('笔记分类编辑失败!');
|
2020-11-05 17:40:51 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除笔记分类
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="classify/delete", methods="post")
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @return ResponseInterface
|
2021-06-29 17:30:43 +08:00
|
|
|
|
* @throws \Exception
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function delArticleClass(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['class_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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'])) {
|
2021-05-13 18:01:34 +08:00
|
|
|
|
return $this->response->fail('笔记分类删除失败!');
|
2020-11-05 17:40:51 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 笔记分类列表排序接口
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="classify/sort", methods="post")
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @return ResponseInterface
|
2021-04-22 16:54:01 +08:00
|
|
|
|
* @throws \Exception
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function articleClassSort(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['class_id', 'sort_type']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$this->validate($params, [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'class_id' => 'required|integer',
|
2020-11-05 17:40:51 +08:00
|
|
|
|
'sort_type' => 'required|in:1,2'
|
|
|
|
|
]);
|
|
|
|
|
|
2021-05-20 22:56:55 +08:00
|
|
|
|
$lockKey = "article:sort_{$params['class_id']}_{$params['sort_type']}";
|
2020-11-05 17:40:51 +08:00
|
|
|
|
|
2021-05-20 22:56:55 +08:00
|
|
|
|
$lock = LockRedis::getInstance();
|
|
|
|
|
if ($lock->lock($lockKey, 3, 500)) {
|
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
|
|
|
|
|
2021-05-20 22:56:55 +08:00
|
|
|
|
$lock->delete($lockKey);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
} else {
|
|
|
|
|
$isTrue = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $isTrue
|
|
|
|
|
? $this->response->success([], '排序完成...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('排序失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 笔记分类合并接口
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="classify/merge", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function mergeArticleClass(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['class_id', 'toid']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$this->validate($params, [
|
|
|
|
|
'class_id' => 'required|integer',
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'toid' => 'required|integer'
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
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([], '合并完成...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('合并失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加或编辑笔记标签
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="tag/editor", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function editArticleTags(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['tag_id', 'tag_name']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$this->validate($params, [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'tag_id' => 'required|integer|min:0',
|
2021-01-04 17:37:14 +08:00
|
|
|
|
'tag_name' => 'required|max:20'
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
|
|
|
|
|
2020-12-01 17:47:25 +08:00
|
|
|
|
$id = $this->articleService->editArticleTag($this->uid(), (int)$params['tag_id'], $params['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])
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记标签编辑失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除笔记标签
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="del-article-tag", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function delArticleTags(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['tag_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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([], '笔记标签删除完成...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记标签删除失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-11-05 17:40:51 +08:00
|
|
|
|
* 添加或编辑笔记
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="editor", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function editArticle(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$params = $this->request->all();
|
|
|
|
|
$this->validate($params, [
|
|
|
|
|
'article_id' => 'required|integer|min:0',
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'class_id' => 'required|integer|min:0',
|
|
|
|
|
'title' => 'required|max:255',
|
|
|
|
|
'content' => 'required',
|
2020-11-29 17:39:24 +08:00
|
|
|
|
'md_content' => 'required'
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
|
|
|
|
|
2020-11-24 23:23:12 +08:00
|
|
|
|
$id = $this->articleService->editArticle($this->uid(), (int)$params['article_id'], [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'title' => $params['title'],
|
|
|
|
|
'abstract' => mb_substr(strip_tags($params['content']), 0, 200),
|
|
|
|
|
'class_id' => $params['class_id'],
|
2021-09-04 19:13:25 +08:00
|
|
|
|
'image' => StringHelper::getHtmlImage($params['content']),
|
2020-11-05 17:40:51 +08:00
|
|
|
|
'md_content' => htmlspecialchars($params['md_content']),
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'content' => htmlspecialchars($params['content'])
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
2020-11-04 22:58:49 +08:00
|
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
|
return $id
|
|
|
|
|
? $this->response->success(['aid' => $id], '笔记编辑成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记编辑失败!', ['id' => null]);
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除笔记
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="delete", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function deleteArticle(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['article_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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([], '笔记删除成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记删除失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-11-05 17:40:51 +08:00
|
|
|
|
* 恢复删除笔记
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="recover", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function recoverArticle(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['article_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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-29 17:39:24 +08:00
|
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
|
return $isTrue
|
|
|
|
|
? $this->response->success([], '笔记恢复成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记恢复失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 笔记图片上传接口
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="upload-image", methods="post")
|
2021-06-30 19:27:49 +08:00
|
|
|
|
* @param Filesystem $filesystem
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @return ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function uploadArticleImage(Filesystem $filesystem): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-11-27 19:48:41 +08:00
|
|
|
|
$file = $this->request->file('image');
|
2021-05-28 18:38:10 +08:00
|
|
|
|
if (!$file || !$file->isValid()) {
|
2020-11-27 19:48:41 +08:00
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
|
try {
|
|
|
|
|
$path = 'media/images/notes/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath()));
|
|
|
|
|
$filesystem->write($path, file_get_contents($file->getRealPath()));
|
|
|
|
|
} catch (\Exception $e) {
|
2020-11-27 19:48:41 +08:00
|
|
|
|
return $this->response->fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success([
|
|
|
|
|
'save_path' => get_media_url($path)
|
|
|
|
|
]);
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移动笔记至指定分类
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="move", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function moveArticle(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['article_id', 'class_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$this->validate($params, [
|
|
|
|
|
'article_id' => 'required|integer|min:0',
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'class_id' => 'required|integer|min:0'
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$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([], '笔记移动成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记移动失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 笔记标记星号接口
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="asterisk", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function setAsterisk(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['article_id', 'type']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$this->validate($params, [
|
|
|
|
|
'article_id' => 'required|integer|min:0',
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'type' => 'required|in:1,2'
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
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([], '笔记标记成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记标记失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新笔记关联标签ID
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="update-tag", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function updateArticleTag(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['article_id', 'tags']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$this->validate($params, [
|
|
|
|
|
'article_id' => 'required|integer|min:0',
|
2021-06-29 17:30:43 +08:00
|
|
|
|
'tags' => 'present|array'
|
2020-11-05 17:40:51 +08:00
|
|
|
|
]);
|
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-29 17:39:24 +08:00
|
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
|
return $isTrue
|
|
|
|
|
? $this->response->success([], 'success...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('编辑失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 永久删除笔记文章
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="forever-delete", methods="post")
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @return ResponseInterface
|
2021-04-22 16:54:01 +08:00
|
|
|
|
* @throws \Exception
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function foreverDelArticle(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['article_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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([], '笔记删除成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记删除失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上传笔记附件
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="annex/upload", methods="post")
|
|
|
|
|
* @param Filesystem $filesystem
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @return ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function uploadArticleAnnex(Filesystem $filesystem): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
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');
|
2021-05-28 18:38:10 +08:00
|
|
|
|
if (!$file || !$file->isValid()) {
|
2021-05-13 18:01:34 +08:00
|
|
|
|
return $this->response->fail('上传文件验证失败!');
|
2020-11-27 19:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$annex = [
|
2021-06-30 19:27:49 +08:00
|
|
|
|
'file_suffix' => pathinfo($file->getClientFilename(), PATHINFO_EXTENSION),
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'file_size' => $file->getSize(),
|
|
|
|
|
'save_dir' => '',
|
2020-11-27 19:48:41 +08:00
|
|
|
|
'original_name' => $file->getClientFilename()
|
|
|
|
|
];
|
|
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
|
try {
|
|
|
|
|
$path = 'files/notes/' . date('Ymd') . '/' . "[{$annex['file_suffix']}]" . create_random_filename('tmp');
|
|
|
|
|
$filesystem->write($path, file_get_contents($file->getRealPath()));
|
|
|
|
|
} catch (\Exception $e) {
|
2020-11-27 19:48:41 +08:00
|
|
|
|
return $this->response->fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$annex['save_dir'] = $path;
|
2021-04-20 16:30:57 +08:00
|
|
|
|
$annex['id'] = $this->articleService->insertArticleAnnex($this->uid(), (int)$params['article_id'], $annex);
|
2021-06-30 19:27:49 +08:00
|
|
|
|
|
2020-12-26 21:33:40 +08:00
|
|
|
|
if (!$annex['id']) {
|
2021-05-13 18:01:34 +08:00
|
|
|
|
return $this->response->fail('附件上传失败,请稍后再试!');
|
2020-11-27 19:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success($annex, '笔记附件上传成功...');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除笔记附件
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="annex/delete", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function deleteArticleAnnex(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['annex_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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([], '笔记附件删除成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记附件删除失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 恢复笔记附件
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="annex/recover", methods="post")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function recoverArticleAnnex(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-26 21:33:40 +08:00
|
|
|
|
$params = $this->request->inputs(['annex_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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([], '笔记附件恢复成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记附件恢复失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取附件回收站列表
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="annex/recover-list", methods="get")
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function recoverAnnexList(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
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));
|
2021-09-04 19:13:25 +08:00
|
|
|
|
return (time() > $last_time) ? 0 : DateHelper::diff(date('Y-m-d', $last_time), date('Y-m-d'));
|
2020-11-05 17:40:51 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
array_walk($rows, function (&$item) use ($getDay) {
|
2021-04-20 16:30:57 +08:00
|
|
|
|
$item['day'] = $getDay($item['deleted_at']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 永久删除笔记附件(从已删除附件中永久删除)
|
2021-04-22 16:14:34 +08:00
|
|
|
|
*
|
2021-09-11 12:41:28 +08:00
|
|
|
|
* @RequestMapping(path="annex/forever-delete", methods="post")
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws \Exception
|
2020-11-04 22:58:49 +08:00
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
|
public function foreverDelAnnex(): ResponseInterface
|
2020-11-04 22:58:49 +08:00
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$params = $this->request->inputs(['annex_id']);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
$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([], '笔记附件删除成功...')
|
2021-05-13 18:01:34 +08:00
|
|
|
|
: $this->response->fail('笔记附件删除失败!');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
}
|
2020-11-04 11:57:16 +08:00
|
|
|
|
}
|