CollectionController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Collection;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Support\Facades\Log;
  7. use App\Http\Api\AuthApi;
  8. use App\Http\Api\StudioApi;
  9. use App\Http\Api\ShareApi;
  10. use App\Http\Resources\CollectionResource;
  11. use Illuminate\Support\Facades\DB;
  12. class CollectionController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. public function index(Request $request)
  20. {
  21. $result=false;
  22. $indexCol = ['uid','title','subtitle','summary',
  23. 'article_list','owner','status',
  24. 'default_channel','lang',
  25. 'updated_at','created_at'];
  26. switch ($request->get('view')) {
  27. case 'studio_list':
  28. $indexCol = ['owner'];
  29. //TODO ?
  30. $table = Collection::select($indexCol)
  31. ->selectRaw('count(*) as count')
  32. ->where('status', 30)
  33. ->groupBy('owner');
  34. break;
  35. case 'studio':
  36. $user = AuthApi::current($request);
  37. if(!$user){
  38. return $this->error(__('auth.failed'));
  39. }
  40. $studioId = StudioApi::getIdByName($request->get('name'));
  41. //判断当前用户是否有指定的studio的权限
  42. if($user['user_uid'] !== $studioId){
  43. return $this->error(__('auth.failed'));
  44. }
  45. $table = Collection::select($indexCol);
  46. if($request->get('view2','my')==='my'){
  47. $table = $table->where('owner', $studioId);
  48. }else{
  49. //协作
  50. $resList = ShareApi::getResList($studioId,4);
  51. $resId=[];
  52. foreach ($resList as $res) {
  53. $resId[] = $res['res_id'];
  54. }
  55. $table = $table->whereIn('uid', $resId)->where('owner','<>', $studioId);
  56. }
  57. break;
  58. case 'public':
  59. //全网公开
  60. $table = Collection::select($indexCol)->where('status', 30);
  61. if($request->has('studio')){
  62. $studioId = StudioApi::getIdByName($request->get('studio'));
  63. $table = $table->where('owner',$studioId);
  64. }
  65. break;
  66. default:
  67. # code...
  68. return $this->error("无法识别的view参数",200,200);
  69. break;
  70. }
  71. if($request->has("search") && !empty($request->has("search"))){
  72. $table = $table->where('title', 'like', "%".$request->get("search")."%");
  73. }
  74. $count = $table->count();
  75. if($request->has("order") && $request->has("dir")){
  76. $table = $table->orderBy($request->get("order"),$request->get("dir"));
  77. }else{
  78. if($request->get('view') === 'studio_list'){
  79. $table = $table->orderBy('count','desc');
  80. }else{
  81. $table = $table->orderBy('updated_at','desc');
  82. }
  83. }
  84. $table = $table->skip($request->get("offset",0))
  85. ->take($request->get("limit",1000));
  86. $result = $table->get();
  87. return $this->ok(["rows"=>CollectionResource::collection($result),"count"=>$count]);
  88. }
  89. /**
  90. * Display a listing of the resource.
  91. *
  92. * @return \Illuminate\Http\Response
  93. */
  94. public function showMyNumber(Request $request){
  95. $user = AuthApi::current($request);
  96. if(!$user){
  97. return $this->error(__('auth.failed'));
  98. }
  99. //判断当前用户是否有指定的studio的权限
  100. $studioId = StudioApi::getIdByName($request->get('studio'));
  101. if($user['user_uid'] !== $studioId){
  102. return $this->error(__('auth.failed'));
  103. }
  104. //我的
  105. $my = Collection::where('owner', $studioId)->count();
  106. //协作
  107. $resList = ShareApi::getResList($studioId,4);
  108. $resId=[];
  109. foreach ($resList as $res) {
  110. $resId[] = $res['res_id'];
  111. }
  112. $collaboration = Collection::whereIn('uid', $resId)->where('owner','<>', $studioId)->count();
  113. return $this->ok(['my'=>$my,'collaboration'=>$collaboration]);
  114. }
  115. public static function UserCanEdit($user_uid,$collection){
  116. if($collection->owner === $user_uid){
  117. return true;
  118. }
  119. //查协作
  120. $currPower = ShareApi::getResPower($user_uid,$collection->uid);
  121. if($currPower >= 20){
  122. return true;
  123. }
  124. return false;
  125. }
  126. public static function UserCanRead($user_uid,$collection){
  127. if($collection->owner === $user_uid){
  128. return true;
  129. }
  130. //查协作
  131. $currPower = ShareApi::getResPower($user_uid,$collection->uid);
  132. if($currPower >= 10){
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Store a newly created resource in storage.
  139. *
  140. * @param \Illuminate\Http\Request $request
  141. * @return \Illuminate\Http\Response
  142. */
  143. public function store(Request $request)
  144. {
  145. $user = \App\Http\Api\AuthApi::current($request);
  146. if(!$user){
  147. return $this->error(__('auth.failed'),401,401);
  148. }
  149. //判断当前用户是否有指定的studio的权限
  150. if($user['user_uid'] !== \App\Http\Api\StudioApi::getIdByName($request->get('studio'))){
  151. return $this->error(__('auth.failed'),403,403);
  152. }
  153. //查询是否重复
  154. if(Collection::where('title',$request->get('title'))->where('owner',$user['user_uid'])->exists()){
  155. return $this->error(__('validation.exists'),200,200);
  156. }else{
  157. $newOne = new Collection;
  158. $newOne->id = app('snowflake')->id();
  159. $newOne->uid = Str::uuid();
  160. $newOne->title = $request->get('title');
  161. $newOne->lang = $request->get('lang');
  162. $newOne->article_list = "[]";
  163. $newOne->owner = $user['user_uid'];
  164. $newOne->owner_id = $user['user_id'];
  165. $newOne->editor_id = $user['user_id'];
  166. $newOne->create_time = time()*1000;
  167. $newOne->modify_time = time()*1000;
  168. $newOne->save();
  169. return $this->ok(new CollectionResource($newOne));
  170. }
  171. }
  172. /**
  173. * Display the specified resource.
  174. * @param \Illuminate\Http\Request $request
  175. * @param string $id
  176. * @return \Illuminate\Http\Response
  177. */
  178. public function show(Request $request,$id)
  179. {
  180. $result = Collection::where('uid', $id)->first();
  181. if(!$result){
  182. return $this->error("没有查询到数据");
  183. }
  184. if($result->status<30){
  185. //私有文章,判断权限
  186. Log::error('私有文章,判断权限'.$id);
  187. $user = \App\Http\Api\AuthApi::current($request);
  188. if(!$user){
  189. Log::error('未登录');
  190. return $this->error(__('auth.failed'),401,401);
  191. }
  192. //判断当前用户是否有指定的studio的权限
  193. if($user['user_uid'] !== $result->owner){
  194. Log::error($user["user_uid"].'私有文章,判断权限'.$id);
  195. //非所有者
  196. if(CollectionController::UserCanRead($user['user_uid'],$result)===false){
  197. Log::error($user["user_uid"].'没有读取权限');
  198. return $this->error(__('auth.failed'),403,403);
  199. }
  200. }
  201. }
  202. $result->fullArticleList = true;
  203. return $this->ok(new CollectionResource($result));
  204. }
  205. /**
  206. * Update the specified resource in storage.
  207. *
  208. * @param \Illuminate\Http\Request $request
  209. * @param string $id
  210. * @return \Illuminate\Http\Response
  211. */
  212. public function update(Request $request, string $id)
  213. {
  214. //
  215. $collection = Collection::find($id);
  216. if(!$collection){
  217. return $this->error("no recorder");
  218. }
  219. //鉴权
  220. $user = AuthApi::current($request);
  221. if(!$user){
  222. return $this->error(__('auth.failed'),401,401);
  223. }
  224. if(!CollectionController::UserCanEdit($user["user_uid"],$collection)){
  225. return $this->error(__('auth.failed'),403,403);
  226. }
  227. $collection->title = $request->get('title');
  228. $collection->subtitle = $request->get('subtitle');
  229. $collection->summary = $request->get('summary');
  230. if($request->has('aritcle_list')){
  231. $collection->article_list = \json_encode($request->get('aritcle_list'));
  232. }
  233. $collection->lang = $request->get('lang');
  234. $collection->status = $request->get('status');
  235. $collection->default_channel = $request->get('default_channel');
  236. $collection->modify_time = time()*1000;
  237. $collection->save();
  238. return $this->ok(new CollectionResource($collection));
  239. }
  240. /**
  241. * Remove the specified resource from storage.
  242. * @param \Illuminate\Http\Request $request
  243. * @param string $id
  244. * @return \Illuminate\Http\Response
  245. */
  246. public function destroy(Request $request,string $id)
  247. {
  248. //
  249. $user = AuthApi::current($request);
  250. if(!$user){
  251. return $this->error(__('auth.failed'));
  252. }
  253. //判断当前用户是否有指定的studio的权限
  254. $collection = Collection::find($id);
  255. if($user['user_uid'] !== $collection['owner']){
  256. return $this->error(__('auth.failed'));
  257. }
  258. $delete = 0;
  259. DB::transaction(function() use($collection,$delete){
  260. //TODO 删除文集中的文章
  261. $delete = $collection->delete();
  262. });
  263. return $this->ok($delete);
  264. }
  265. }