CollectionService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Api\ShareApi;
  4. use App\Http\Api\StudioApi;
  5. use App\Http\Resources\CollectionResource;
  6. use App\Models\Collection;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Log;
  10. class CollectionService
  11. {
  12. protected $indexCol = [
  13. 'uid',
  14. 'title',
  15. 'subtitle',
  16. 'summary',
  17. 'article_list',
  18. 'owner',
  19. 'status',
  20. 'default_channel',
  21. 'lang',
  22. 'updated_at',
  23. 'created_at',
  24. ];
  25. /**
  26. * 判断用户是否有编辑权限
  27. */
  28. public function userCanEdit(string $user_uid, Collection $collection): bool
  29. {
  30. if ($collection->owner === $user_uid) {
  31. return true;
  32. }
  33. return ShareApi::getResPower($user_uid, $collection->uid) >= 20;
  34. }
  35. /**
  36. * 判断用户是否有读取权限
  37. */
  38. public function userCanRead(string $user_uid, Collection $collection): bool
  39. {
  40. if ($collection->owner === $user_uid) {
  41. return true;
  42. }
  43. return ShareApi::getResPower($user_uid, $collection->uid) >= 10;
  44. }
  45. /**
  46. * 获取当前 studio 下我的数量与协作数量
  47. */
  48. public function getMyNumber(Request $request): array
  49. {
  50. $user = AuthService::current($request);
  51. if (! $user) {
  52. return ['error' => __('auth.failed'), 'code' => 403];
  53. }
  54. $studioId = StudioApi::getIdByName($request->get('studio'));
  55. if ($user['user_uid'] !== $studioId) {
  56. return ['error' => __('auth.failed'), 'code' => 403];
  57. }
  58. $my = Collection::where('owner', $studioId)->count();
  59. $resList = ShareApi::getResList($studioId, 4);
  60. $resId = array_column($resList, 'res_id');
  61. $collaboration = Collection::whereIn('uid', $resId)
  62. ->where('owner', '<>', $studioId)
  63. ->count();
  64. return ['data' => ['my' => $my, 'collaboration' => $collaboration]];
  65. }
  66. public function buildStudioListQuery(): Builder
  67. {
  68. return Collection::select(['owner'])
  69. ->selectRaw('count(*) as count')
  70. ->where('status', 30)
  71. ->groupBy('owner');
  72. }
  73. public function buildStudioQuery(string $userUid, string $studioId, string $view2 = 'my'): Builder
  74. {
  75. $table = Collection::select($this->indexCol);
  76. if ($view2 === 'my') {
  77. return $table->where('owner', $studioId);
  78. }
  79. $resList = ShareApi::getResList($studioId, 4);
  80. $resId = array_column($resList, 'res_id');
  81. return $table->whereIn('uid', $resId)->where('owner', '<>', $studioId);
  82. }
  83. public function buildPublicQuery(?string $studioId = null): Builder
  84. {
  85. $table = Collection::select($this->indexCol)->where('status', 30);
  86. if ($studioId) {
  87. $table = $table->where('owner', $studioId);
  88. }
  89. return $table;
  90. }
  91. public function getPublicList(int $pageSize, int $currPage): array
  92. {
  93. $table = Collection::select($this->indexCol)->where('status', 30);
  94. $count = $table->count();
  95. $result = $table
  96. ->orderBy('updated_at', 'desc')
  97. ->skip(($currPage - 1) * $pageSize)
  98. ->take($pageSize)
  99. ->get();
  100. return ['data' => CollectionResource::collection($result), 'total' => $count];
  101. }
  102. public function getCollection(string $id): array
  103. {
  104. $result = Collection::where('uid', $id)->first();
  105. if (! $result) {
  106. Log::warning("没有查询到数据 id={$id}");
  107. return ['error' => "没有查询到数据 id={$id}", 'code' => 404];
  108. }
  109. $result->fullArticleList = true;
  110. return ['data' => new CollectionResource($result)];
  111. }
  112. }