ArticleMapController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\ArticleCollection;
  4. use App\Models\Article;
  5. use App\Models\Collection;
  6. use App\Http\Api\ShareApi;
  7. use App\Services\AuthService;
  8. use Illuminate\Http\Request;
  9. use App\Http\Resources\ArticleMapResource;
  10. class ArticleMapController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index(Request $request)
  18. {
  19. //
  20. switch ($request->input('view')) {
  21. case 'anthology':
  22. $table = ArticleCollection::where('collect_id', $request->input('id'))
  23. ->leftJoin('articles', 'articles.uid', '=', 'article_collections.article_id');
  24. break;
  25. case 'article':
  26. $table = ArticleCollection::where('article_id', $request->input('id'))
  27. ->leftJoin('articles', 'articles.uid', '=', 'article_collections.article_id');
  28. break;
  29. }
  30. $count = $table->count();
  31. $result = [];
  32. if (!empty($request->input('parent'))) {
  33. //输出某节点的子节点
  34. $node = $table->where('article_id', $request->input('parent'))->first();
  35. if ($node) {
  36. $nodeList = ArticleCollection::where('collect_id', $request->input('id'))
  37. ->where('id', '>', (int)$node->id)->orderBy('id')->get();
  38. foreach ($nodeList as $key => $curr) {
  39. if ($curr->level <= $node->level) {
  40. break;
  41. }
  42. if ($request->has('lazy')) {
  43. if ($curr->level === $node->level + 1) {
  44. $result[] = $curr;
  45. }
  46. } else {
  47. $result[] = $curr;
  48. }
  49. }
  50. }
  51. } else {
  52. if ($request->has('lazy') && $count > 300) {
  53. $table = $table->where('level', 1);
  54. }
  55. $result = $table->select([
  56. 'article_collections.id',
  57. 'collect_id',
  58. 'article_id',
  59. 'level',
  60. 'article_collections.title',
  61. 'children',
  62. 'article_collections.editor_id',
  63. 'article_collections.deleted_at',
  64. 'articles.status'
  65. ])->orderBy('id')->get();
  66. }
  67. return $this->ok(["rows" => ArticleMapResource::collection($result), "count" => $count]);
  68. }
  69. /**
  70. * Store a newly created resource in storage.
  71. *
  72. * @param \Illuminate\Http\Request $request
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function store(Request $request)
  76. {
  77. //
  78. $validated = $request->validate([
  79. 'anthology_id' => 'required',
  80. 'operation' => 'required'
  81. ]);
  82. $collection = Collection::find($request->input('anthology_id'));
  83. if (!$collection) {
  84. return $this->error("no recorder");
  85. }
  86. //鉴权
  87. $user = AuthService::current($request);
  88. if (!$user) {
  89. return $this->error(__('auth.failed'));
  90. }
  91. if (!CollectionController::UserCanEdit($user["user_uid"], $collection)) {
  92. return $this->error(__('auth.failed'));
  93. }
  94. switch ($validated['operation']) {
  95. case 'add':
  96. # 添加多个文章到文集
  97. $count = 0;
  98. foreach ($request->input('article_id') as $key => $article) {
  99. # code...
  100. if (!ArticleCollection::where('article_id', $article)
  101. ->where('collect_id', $request->input('anthology_id'))
  102. ->exists()) {
  103. $new = new ArticleCollection;
  104. $new->id = app('snowflake')->id();
  105. $new->article_id = $article;
  106. $new->collect_id = $request->input('anthology_id');
  107. $new->title = Article::find($article)->title;
  108. $new->level = 1;
  109. $new->editor_id = $user["user_id"];
  110. $new->save();
  111. $count++;
  112. }
  113. }
  114. return $this->ok($count);
  115. break;
  116. default:
  117. return $this->error('unknown operation');
  118. break;
  119. }
  120. }
  121. /**
  122. * Display the specified resource.
  123. *
  124. * @param \App\Models\ArticleCollection $articleCollection
  125. * @return \Illuminate\Http\Response
  126. */
  127. public function show(string $articleCollection)
  128. {
  129. //
  130. $id = explode('_', $articleCollection);
  131. $result = ArticleCollection::where('article_id', $id[0])
  132. ->where('collect_id', $id[1])
  133. ->first();
  134. if ($result) {
  135. return $this->ok(new ArticleMapResource($result));
  136. } else {
  137. return $this->error('no');
  138. }
  139. }
  140. /**
  141. * Update the specified resource in storage.
  142. *
  143. * @param \Illuminate\Http\Request $request
  144. * @param string $id
  145. * @return \Illuminate\Http\Response
  146. */
  147. public function update(Request $request, string $id)
  148. {
  149. //
  150. $validated = $request->validate([
  151. 'operation' => 'required'
  152. ]);
  153. $collection = Collection::find($id);
  154. if (!$collection) {
  155. return $this->error("no recorder");
  156. }
  157. //鉴权
  158. $user = AuthService::current($request);
  159. if (!$user) {
  160. return $this->error(__('auth.failed'));
  161. }
  162. if (!CollectionController::UserCanEdit($user["user_uid"], $collection)) {
  163. return $this->error(__('auth.failed'));
  164. }
  165. switch ($validated['operation']) {
  166. case 'anthology':
  167. $delete = ArticleCollection::where('collect_id', $id)->delete();
  168. $count = 0;
  169. foreach ($request->input('data') as $key => $row) {
  170. # code...
  171. $new = new ArticleCollection;
  172. $new->id = app('snowflake')->id();
  173. $new->article_id = $row["article_id"];
  174. $new->collect_id = $id;
  175. $new->title = $row["title"];
  176. $new->level = $row["level"];
  177. $new->children = $row["children"];
  178. $new->editor_id = $user["user_id"];
  179. if (isset($row["deleted_at"])) {
  180. $new->deleted_at = $row["deleted_at"];
  181. }
  182. $new->save();
  183. $count++;
  184. }
  185. ArticleMapController::updateCollection($id);
  186. return $this->ok($count);
  187. break;
  188. }
  189. }
  190. /**
  191. * Remove the specified resource from storage.
  192. *
  193. * @param \App\Models\ArticleCollection $articleCollection
  194. * @return \Illuminate\Http\Response
  195. */
  196. public function destroy(ArticleCollection $articleCollection)
  197. {
  198. //
  199. }
  200. public static function deleteArticle(string $articleId)
  201. {
  202. //查找有这个文章的文集
  203. $collections = ArticleCollection::where('article_id', $articleId)
  204. ->select('collect_id')
  205. ->groupBy('collect_id')
  206. ->get();
  207. //设置为删除
  208. ArticleCollection::where('article_id', $articleId)
  209. ->update(['deleted_at' => now()]);
  210. //查找没有下级文章的文集
  211. $updateCollections = ArticleCollection::where('article_id', $articleId)
  212. ->where('children', 0)
  213. ->select('collect_id')
  214. ->groupBy('collect_id')
  215. ->get();
  216. //真的删除没有下级文章的文集中的文章
  217. $count = ArticleCollection::where('article_id', $articleId)
  218. ->where('children', 0)
  219. ->delete();
  220. //更新改动的文集
  221. foreach ($updateCollections as $collection) {
  222. # code...
  223. ArticleMapController::updateCollection($collection->collect_id);
  224. }
  225. return [count($collections), $count];
  226. }
  227. public static function deleteCollection(string $collectionId)
  228. {
  229. $count = ArticleCollection::where('collect_id', $collectionId)
  230. ->delete();
  231. return $count;
  232. }
  233. /**
  234. * 用表中的数据生成json,更新collection 表中的字段
  235. */
  236. public static function updateCollection(string $collectionId)
  237. {
  238. $result = ArticleCollection::where('collect_id', $collectionId)
  239. ->select(['article_id', 'level', 'title'])
  240. ->orderBy('id')->get();
  241. Collection::where('uid', $collectionId)
  242. ->update(['article_list' => json_encode($result, JSON_UNESCAPED_UNICODE)]);
  243. return count($result);
  244. }
  245. }