UserAuthController.php
7.03 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace App\Http\Container\AppSection\Controllers;
use App\Enums\UserScopeEnum;
use App\Exceptions\SingerLimitException;
use App\Helpers\ConfigHelper;
use App\Helpers\JsonResource;
use App\Http\Container\AppSection\Requests\UserAuthCreateRequest;
use App\Http\Container\AppSection\Requests\UserAuthExamineRequests;
use App\Http\Service\UserService;
use App\Jobs\UserApplyCertifyJob;
use App\Jobs\UserSingerLimitJob;
use App\Models\GroupHasMember;
use App\Models\SystemTag;
use App\Models\User;
use App\Models\UserCertify;
use App\Models\Views\UserLastCertify;
use App\Notifications\UserCertifyFailNotification;
use App\Notifications\UserCertifySuccessNotification;
use DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;
use RuntimeException;
class UserAuthController
{
public function story(UserAuthCreateRequest $request)
{
$data = $request->validated();
$user_id = Auth::id();
$user = Auth::user();
$is_submit = UserCertify::query()->where(['user_id' => $user_id, 'status' => 0])->exists();
if ($is_submit) {
return JsonResource::fail(JsonResource::REPEAT_SUBMIT);
}
//判断被邀请人是否有歌手标签且自己团队歌手标签没超过限制
$group_id = GroupHasMember::query()->where(['member_id' => $user_id])->first()->group_id ?? 0;
$singerTags = ConfigHelper::getSingerTagIds();
$is_singer_tag = ConfigHelper::hasSingerTag($data['tags'], $singerTags);
$ids = GroupHasMember::query()->where(['group_id' => $group_id, 'role' => 3])->whereNot('member_id', $user_id)->pluck('member_id')->toArray();
//计算团队歌手标签数,过滤掉当前用户
$singer_tag_count = User::query()->whereIn('id', $ids)->whereNot('id', $user_id)
->whereHas('authTags', fn(Builder $builder) => $builder->whereIn('system_tags.id', $singerTags))
->count();
$masterId = GroupHasMember::query()->where(['group_id' => $group_id, 'role' => 1])->first()->member_id ?? 0;
if ($masterId) {
$maxSingerNum = User::query()->whereKey($masterId)->value('business_singer_limit');
if ($is_singer_tag && $singer_tag_count >= $maxSingerNum) {
$master = User::query()->find($masterId, ['id', 'nick_name']);
UserSingerLimitJob::dispatch([
'title' => sprintf('%s:歌手额度已满', $master?->getAttribute('nick_name')),
'content' => sprintf('%s:提交歌手认证', $user?->getAttribute('nick_name'))
], [
'title' => sprintf('%s:提交歌手认证', $user?->getAttribute('nick_name')),
'content' => sprintf('经纪人:%s', $master->getAttribute('nick_name')),
'limit' => $maxSingerNum
]);
return JsonResource::fail(JsonResource::TEAM_INVITE_SINGER_LIMIT, code: 403);
}
}
$data['user_id'] = $user_id;
$certify = UserCertify::create($data);
UserApplyCertifyJob::dispatch($certify);
return JsonResource::success(JsonResource::CREATE_SUCCESS);
}
public function list(Request $request)
{
$size = $request->get('size', 10);
$user = Auth::user();
if ($user?->getAttribute('scope') === UserScopeEnum::SYSTEM) {
$datas = User::query()
->join('user_auth_infos', 'user_auth_infos.user_id', '=', 'users.id')
->where('user_auth_infos.status', 0)
->where('user_auth_infos.deleted_at', NULL)
->select('users.id', 'users.nick_name', 'users.avatar', 'user_auth_infos.created_at', 'user_auth_infos.tags')
->orderBy('user_auth_infos.created_at', 'desc')
->paginate($size);
foreach ($datas as $data) {
$data['tag_list'] = SystemTag::query()->whereIn('id', json_decode($data->tags))->pluck('name');
unset($data->tags);
}
return JsonResource::success(JsonResource::SUCCESS, ['data' => $datas->items(), 'count' => $datas->total()]);
}
return JsonResource::fail(JsonResource::NO_PERMISSION);
}
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function detail(Request $request): JsonResponse
{
$user = User::query()
->with(['authTags:id,name'])
->select(['id', 'nick_name', 'avatar', 'real_name', 'identity', 'register_type', 'register_remark', 'inviter_id'])
->findOrFail($request->get('id'));
$user->setAttribute('auth_info', UserLastCertify::query()->where('user_id', $user->getKey())->first());
$user->setAttribute('inviter', User::query()
->select(['id', 'nick_name', 'avatar', 'real_name', 'identity', 'user_tag_id'])
->with(['authTags:id,name', 'userTag:id,name,icon,frame'])
->find($user->getAttribute('inviter_id')));
return JsonResource::success(JsonResource::SUCCESS, $user);
}
public function examine(UserAuthExamineRequests $request)
{
$data = $request->validated();
$id = $data['id'];
$status = $data['status'];
$reason = $data['reason'] ?? NULL;
$item = UserCertify::with('user')->find($id);
$user = $item->getAttribute('user');
try {
DB::beginTransaction();
//审核认证
$item->update(['operator_id' => Auth::id(), 'status' => $status, 'reason' => $reason]);
if ($status === 1) {
UserService::updateAuthTags($user, $item->getAttribute('tags'), ['audit_at' => now()->toDateTimeString()]);
}
DB::commit();
match ($item->getAttribute('status')) {
1 => Notification::send($user, new UserCertifySuccessNotification()),
2 => Notification::send($user, new UserCertifyFailNotification())
};
return JsonResource::success(JsonResource::UPDATE_SUCCESS);
} catch (SingerLimitException $exception) {
DB::rollBack();
$master = User::query()->find($exception->masterId, ['id', 'nick_name']);
UserSingerLimitJob::dispatch([
'title' => sprintf('%s:歌手额度已满', $master?->getAttribute('nick_name')),
'content' => sprintf('审核歌手认证:%s', $user->getAttribute('nick_name'))
], [
'title' => sprintf('%s:审核歌手认证', $user->getAttribute('nick_name')),
'content' => sprintf('经纪人:%s', $master->getAttribute('nick_name')),
'limit' => $exception->maxSingerNum
]);
return JsonResource::fail(JsonResource::TEAM_INVITE_SINGER_LIMIT, code: 403);
} catch (RuntimeException $exception) {
DB::rollBack();
throw $exception;
}
}
}