CollectionService.php 3.8 KB

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