CollectionController.php 9.9 KB

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