ArticleMapController.php 8.8 KB

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