ArticleController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Article;
  4. use App\Models\ArticleCollection;
  5. use App\Models\Collection;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Str;
  8. use App\Http\Resources\ArticleResource;
  9. use App\Http\Api\AuthApi;
  10. use App\Http\Api\ShareApi;
  11. use App\Http\Api\StudioApi;
  12. use Illuminate\Support\Facades\DB;
  13. class ArticleController extends Controller
  14. {
  15. public static function userCanRead($user_uid,Article $article){
  16. if($article->status === 30 ){
  17. return true;
  18. }
  19. if(empty($user_uid)){
  20. return false;
  21. }
  22. //私有文章,判断是否为所有者
  23. if($user_uid === $article->owner){
  24. return true;
  25. }
  26. //非所有者
  27. //判断是否为文章协作者
  28. $power = ShareApi::getResPower($user_uid,$article->uid);
  29. if($power >= 10 ){
  30. return true;
  31. }
  32. //无读取权限
  33. //判断文集是否有读取权限
  34. $inCollection = ArticleCollection::where('article_id',$article->uid)
  35. ->select('collect_id')
  36. ->groupBy('collect_id')->get();
  37. if(!$inCollection){
  38. return false;
  39. }
  40. //查找与文章同主人的文集
  41. $collections = Collection::whereIn('uid',$inCollection)
  42. ->where('owner',$article->owner)
  43. ->select('uid')
  44. ->get();
  45. if(!$collections){
  46. return false;
  47. }
  48. //查找与文章同主人的文集是否是共享的
  49. $power = 0;
  50. foreach ($collections as $collection) {
  51. # code...
  52. $currPower = ShareApi::getResPower($user_uid,$collection->uid);
  53. if($currPower >= 10){
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. public static function userCanEdit($user_uid,$article){
  60. if(empty($user_uid)){
  61. return false;
  62. }
  63. //私有文章,判断是否为所有者
  64. if($user_uid === $article->owner){
  65. return true;
  66. }
  67. //非所有者
  68. //判断是否为文章协作者
  69. $power = ShareApi::getResPower($user_uid,$article->uid);
  70. if($power >= 20 ){
  71. return true;
  72. }
  73. //无读取权限
  74. //判断文集是否有读取权限
  75. $inCollection = ArticleCollection::where('article_id',$article->uid)
  76. ->select('collect_id')
  77. ->groupBy('collect_id')->get();
  78. if(!$inCollection){
  79. return false;
  80. }
  81. //查找与文章同主人的文集
  82. $collections = Collection::whereIn('uid',$inCollection)
  83. ->where('owner',$article->owner)
  84. ->select('uid')
  85. ->get();
  86. if(!$collections){
  87. return false;
  88. }
  89. //查找与文章同主人的文集是否是共享的
  90. $power = 0;
  91. foreach ($collections as $collection) {
  92. # code...
  93. $currPower = ShareApi::getResPower($user_uid,$collection->uid);
  94. if($currPower >= 20){
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. public static function userCanManage($user_uid,$studioName){
  101. if(empty($user_uid)){
  102. return false;
  103. }
  104. //判断是否为所有者
  105. if($user_uid === StudioApi::getIdByName($studioName)){
  106. return true;
  107. }else{
  108. return false;
  109. }
  110. }
  111. /**
  112. * Display a listing of the resource.
  113. *
  114. * @return \Illuminate\Http\Response
  115. */
  116. public function index(Request $request)
  117. {
  118. //
  119. $indexCol = ['uid','title','subtitle','summary','owner','lang','status','updated_at','created_at'];
  120. switch ($request->get('view')) {
  121. case 'studio':
  122. # 获取studio内所有channel
  123. $user = \App\Http\Api\AuthApi::current($request);
  124. if(!$user){
  125. return $this->error(__('auth.failed'));
  126. }
  127. //判断当前用户是否有指定的studio的权限
  128. $studioId = StudioApi::getIdByName($request->get('name'));
  129. if($user['user_uid'] !== $studioId){
  130. return $this->error(__('auth.failed'));
  131. }
  132. $table = Article::select($indexCol);
  133. if($request->get('view2','my')==='my'){
  134. $table = $table->where('owner', $studioId);
  135. }else{
  136. //协作
  137. $resList = ShareApi::getResList($studioId,3);
  138. $resId=[];
  139. foreach ($resList as $res) {
  140. $resId[] = $res['res_id'];
  141. }
  142. $table = $table->whereIn('uid', $resId)->where('owner','<>', $studioId);
  143. }
  144. //根据anthology过滤
  145. if($request->has('anthology')){
  146. switch ($request->get('anthology')) {
  147. case 'all':
  148. break;
  149. case 'none':
  150. # 我的文集
  151. $myCollection = Collection::where('owner',$studioId)->select('uid')->get();
  152. //收录在我的文集里面的文章
  153. $articles = ArticleCollection::whereIn('collect_id',$myCollection)
  154. ->select('article_id')->groupBy('article_id')->get();
  155. //不在这些范围之内的文章
  156. $table = $table->whereNotIn('uid',$articles);
  157. break;
  158. default:
  159. $articles = ArticleCollection::where('collect_id',$request->get('anthology'))
  160. ->select('article_id')->get();
  161. $table = $table->whereIn('uid',$articles);
  162. break;
  163. }
  164. }
  165. break;
  166. }
  167. //处理搜索
  168. if($request->has("search") && !empty($request->has("search"))){
  169. $table = $table->where('title', 'like', "%".$request->get("search")."%");
  170. }
  171. //获取记录总条数
  172. $count = $table->count();
  173. //处理排序
  174. if(isset($_GET["order"]) && isset($_GET["dir"])){
  175. $table = $table->orderBy($_GET["order"],$_GET["dir"]);
  176. }else{
  177. //默认排序
  178. $table = $table->orderBy('updated_at','desc');
  179. }
  180. //处理分页
  181. if($request->has("limit")){
  182. if($request->has("offset")){
  183. $offset = $request->get("offset");
  184. }else{
  185. $offset = 0;
  186. }
  187. $table = $table->skip($offset)->take($request->get("limit"));
  188. }
  189. //获取数据
  190. $result = $table->get();
  191. if($result){
  192. return $this->ok(["rows"=>ArticleResource::collection($result),"count"=>$count]);
  193. }else{
  194. return $this->error("没有查询到数据");
  195. }
  196. }
  197. /**
  198. * Display a listing of the resource.
  199. *
  200. * @return \Illuminate\Http\Response
  201. */
  202. public function showMyNumber(Request $request){
  203. $user = AuthApi::current($request);
  204. if(!$user){
  205. return $this->error(__('auth.failed'));
  206. }
  207. //判断当前用户是否有指定的studio的权限
  208. $studioId = StudioApi::getIdByName($request->get('studio'));
  209. if($user['user_uid'] !== $studioId){
  210. return $this->error(__('auth.failed'));
  211. }
  212. //我的
  213. $my = Article::where('owner', $studioId)->count();
  214. //协作
  215. $resList = ShareApi::getResList($studioId,3);
  216. $resId=[];
  217. foreach ($resList as $res) {
  218. $resId[] = $res['res_id'];
  219. }
  220. $collaboration = Article::whereIn('uid', $resId)->where('owner','<>', $studioId)->count();
  221. return $this->ok(['my'=>$my,'collaboration'=>$collaboration]);
  222. }
  223. /**
  224. * Store a newly created resource in storage.
  225. *
  226. * @param \Illuminate\Http\Request $request
  227. * @return \Illuminate\Http\Response
  228. */
  229. public function store(Request $request)
  230. {
  231. //判断权限
  232. $user = AuthApi::current($request);
  233. if(!$user){
  234. return $this->error(__('auth.failed'),[],401);
  235. }else{
  236. $user_uid=$user['user_uid'];
  237. }
  238. $canManage = ArticleController::userCanManage($user_uid,$request->get('studio'));
  239. if(!$canManage){
  240. return $this->error(__('auth.failed'),[],403);
  241. }
  242. //权限判断结束
  243. $studioUuid = StudioApi::getIdByName($request->get('studio'));
  244. //查询标题是否重复
  245. /*
  246. if(Article::where('title',$request->get('title'))->where('owner',$studioUuid)->exists()){
  247. return $this->error(__('validation.exists'));
  248. }*/
  249. $newOne = new Article;
  250. $newOne->id = app('snowflake')->id();
  251. $newOne->uid = Str::uuid();
  252. $newOne->title = $request->get('title');
  253. $newOne->lang = $request->get('lang');
  254. $newOne->owner = $studioUuid;
  255. $newOne->owner_id = $user['user_id'];
  256. $newOne->editor_id = $user['user_id'];
  257. $newOne->create_time = time()*1000;
  258. $newOne->modify_time = time()*1000;
  259. $newOne->save();
  260. return $this->ok($newOne);
  261. }
  262. /**
  263. * Display the specified resource.
  264. * @param \Illuminate\Http\Request $request
  265. * @param \App\Models\Article $article
  266. * @return \Illuminate\Http\Response
  267. */
  268. public function show(Request $request,Article $article)
  269. {
  270. //
  271. if(!$article){
  272. return $this->error("no recorder");
  273. }
  274. //判断权限
  275. $user = AuthApi::current($request);
  276. if(!$user){
  277. $user_uid="";
  278. }else{
  279. $user_uid=$user['user_uid'];
  280. }
  281. $canRead = ArticleController::userCanRead($user_uid,$article);
  282. if(!$canRead){
  283. return $this->error(__('auth.failed'),[],401);
  284. }
  285. return $this->ok(new ArticleResource($article));
  286. }
  287. /**
  288. * Update the specified resource in storage.
  289. *
  290. * @param \Illuminate\Http\Request $request
  291. * @param \App\Models\Article $article
  292. * @return \Illuminate\Http\Response
  293. */
  294. public function update(Request $request, Article $article)
  295. {
  296. //
  297. if(!$article){
  298. return $this->error("no recorder");
  299. }
  300. //鉴权
  301. $user = AuthApi::current($request);
  302. if(!$user){
  303. return $this->error(__('auth.failed'),[],401);
  304. }else{
  305. $user_uid=$user['user_uid'];
  306. }
  307. $canEdit = ArticleController::userCanEdit($user_uid,$article);
  308. if(!$canEdit){
  309. return $this->error(__('auth.failed'),[],401);
  310. }
  311. /*
  312. //查询标题是否重复
  313. if(Article::where('title',$request->get('title'))
  314. ->where('owner',$article->owner)
  315. ->where('uid',"<>",$article->uid)
  316. ->exists()){
  317. return $this->error(__('validation.exists'));
  318. }*/
  319. $article->title = $request->get('title');
  320. $article->subtitle = $request->get('subtitle');
  321. $article->summary = $request->get('summary');
  322. $article->content = $request->get('content');
  323. $article->lang = $request->get('lang');
  324. $article->status = $request->get('status',10);
  325. $article->editor_id = $user['user_id'];
  326. $article->modify_time = time()*1000;
  327. $article->save();
  328. return $this->ok($article);
  329. }
  330. /**
  331. * Remove the specified resource from storage.
  332. * @param \Illuminate\Http\Request $request
  333. * @param \App\Models\Article $article
  334. * @return \Illuminate\Http\Response
  335. */
  336. public function destroy(Request $request,Article $article)
  337. {
  338. //
  339. $user = AuthApi::current($request);
  340. if(!$user){
  341. return $this->error(__('auth.failed'));
  342. }
  343. //判断当前用户是否有指定的studio的权限
  344. if($user['user_uid'] !== $article->owner){
  345. return $this->error(__('auth.failed'));
  346. }
  347. $delete = 0;
  348. DB::transaction(function() use($article,$delete){
  349. //TODO 删除文集中的文章
  350. $delete = $article->delete();
  351. });
  352. return $this->ok($delete);
  353. }
  354. }