| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Services;
- use App\Models\Collection;
- use App\Http\Api\AuthApi;
- use App\Http\Api\StudioApi;
- use App\Http\Api\ShareApi;
- use Illuminate\Http\Request;
- use App\Http\Resources\CollectionResource;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Database\Eloquent\Builder;
- class CollectionService
- {
- protected $indexCol = [
- 'uid',
- 'title',
- 'subtitle',
- 'summary',
- 'article_list',
- 'owner',
- 'status',
- 'default_channel',
- 'lang',
- 'updated_at',
- 'created_at',
- ];
- /**
- * 判断用户是否有编辑权限
- */
- public function userCanEdit(string $user_uid, Collection $collection): bool
- {
- if ($collection->owner === $user_uid) {
- return true;
- }
- return ShareApi::getResPower($user_uid, $collection->uid) >= 20;
- }
- /**
- * 判断用户是否有读取权限
- */
- public function userCanRead(string $user_uid, Collection $collection): bool
- {
- if ($collection->owner === $user_uid) {
- return true;
- }
- return ShareApi::getResPower($user_uid, $collection->uid) >= 10;
- }
- /**
- * 获取当前 studio 下我的数量与协作数量
- */
- public function getMyNumber(Request $request): array
- {
- $user = AuthApi::current($request);
- if (!$user) {
- return ['error' => __('auth.failed'), 'code' => 403];
- }
- $studioId = StudioApi::getIdByName($request->get('studio'));
- if ($user['user_uid'] !== $studioId) {
- return ['error' => __('auth.failed'), 'code' => 403];
- }
- $my = Collection::where('owner', $studioId)->count();
- $resList = ShareApi::getResList($studioId, 4);
- $resId = array_column($resList, 'res_id');
- $collaboration = Collection::whereIn('uid', $resId)
- ->where('owner', '<>', $studioId)
- ->count();
- return ['data' => ['my' => $my, 'collaboration' => $collaboration]];
- }
- public function buildStudioListQuery(): Builder
- {
- return Collection::select(['owner'])
- ->selectRaw('count(*) as count')
- ->where('status', 30)
- ->groupBy('owner');
- }
- public function buildStudioQuery(string $userUid, string $studioId, string $view2 = 'my'): Builder
- {
- $table = Collection::select($this->indexCol);
- if ($view2 === 'my') {
- return $table->where('owner', $studioId);
- }
- $resList = ShareApi::getResList($studioId, 4);
- $resId = array_column($resList, 'res_id');
- return $table->whereIn('uid', $resId)->where('owner', '<>', $studioId);
- }
- public function buildPublicQuery(?string $studioId = null): Builder
- {
- $table = Collection::select($this->indexCol)->where('status', 30);
- if ($studioId) {
- $table = $table->where('owner', $studioId);
- }
- return $table;
- }
- public function getPublicList(int $pageSize, int $currPage): array
- {
- $table = Collection::select($this->indexCol)->where('status', 30);
- $count = $table->count();
- $result = $table
- ->orderBy('updated_at', 'desc')
- ->skip(($currPage - 1) * $pageSize)
- ->take($pageSize)
- ->get();
- return ['data' => CollectionResource::collection($result), 'total' => $count];
- }
- public function getCollection(string $id): array
- {
- $result = Collection::where('uid', $id)->first();
- if (!$result) {
- Log::warning("没有查询到数据 id={$id}");
- return ['error' => "没有查询到数据 id={$id}", 'code' => 404];
- }
- $result->fullArticleList = true;
- return ['data' => new CollectionResource($result)];
- }
- }
|