CollectionController.php 9.3 KB

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