ShareController.php
2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace App\Http\Container\AppSection\Controllers\Auth;
use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivityCreateFormEnum;
use App\Enums\ActivitySongTypeEnum;
use App\Enums\ActivityStatusEnum;
use App\Enums\UserScopeEnum;
use App\Helpers\JsonResource;
use App\Http\Service\UserService;
use App\Models\Activity;
use App\Support\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ShareController extends Controller
{
protected UserScopeEnum $scope;
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function demo(Request $request): JsonResponse
{
$pageSize = $request->integer('size', 15);
$search = $request->get('search');
$filter = $request->except(['page', 'size']);
$this->scope = Auth::user()?->getAttribute('scope');
$data = Activity::filter($filter)->with(['tags:id,name', 'project:id,name,cover', 'user:id,nick_name,avatar'])
->select(['id', 'song_name', 'song_type', 'sub_title', 'user_id', 'cover', 'project_id'])
->when($search, fn(Builder $query) => $query->where('song_name', 'like', "%$search%"))
->when($this->scope !== UserScopeEnum::SYSTEM, fn(Builder $query) => $query->where(function (Builder $query) {
if (UserService::hasDemoPermission(Auth::user())) {
$query->where(['user_id' => Auth::id(), 'created_form' => ActivityCreateFormEnum::USER]);
}
$projectIds = $this->scope === UserScopeEnum::PROJECT ? UserService::getManageProjectIds(Auth::id()) : [];
if (count($projectIds) !== 0) {
$query->orWhereIn('project_id', $projectIds);
}
}))
->where('song_type', ActivitySongTypeEnum::DEMO)
->where('audit_status', ActivityAuditStatusEnum::SUCCESS)
->where('status', ActivityStatusEnum::UP)
->orderByDesc('audit_at')->paginate($pageSize);
return JsonResource::success(JsonResource::SUCCESS, ['data' => $data->items(), 'count' => $data->total()]);
}
}