CertifyController.php
3.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
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
<?php
namespace App\Http\Container\AdminSection\Controllers\User;
use App\Exceptions\SingerLimitException;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\User\CertifyUpdateRequest;
use App\Http\Service\UserService;
use App\Models\UserCertify;
use App\Models\Views\UserLastCertify;
use App\Notifications\UserCertifyFailNotification;
use App\Notifications\UserCertifySuccessNotification;
use App\Support\Controller;
use Auth;
use DB;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use RuntimeException;
class CertifyController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function index(Request $request): Response
{
$pageSize = $request->get('pageSize', 20);
$filter = $request->except(['page', 'pageSize']);
$data = UserLastCertify::filter($filter)
->with([
'user:id,avatar,nick_name,real_name,sex,identity,register_type,register_remark,inviter_id',
'user.authTags:id,name',
'user.inviter:id,nick_name,identity',
'records:id,user_id,operator_id,status,reason,updated_at',
'records.operator:id,nick_name,real_name'
])
->paginate($pageSize);
return $this->successWithData($data);
}
/**
* @param \App\Http\Container\AdminSection\Requests\User\CertifyUpdateRequest $request
* @param \App\Models\UserCertify $certify
* @param \App\Http\Service\UserService $userService
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function update(CertifyUpdateRequest $request, UserCertify $certify): Response
{
$data = $request->safe()->merge(['operator_id' => Auth::id()])->toArray();
if ($certify->getAttribute('status') !== 0) {
return $this->fail(ApiCode::VALIDATION_ERROR, '申请已被处理');
}
$user = $certify->loadMissing('user')->getAttribute('user');
try {
DB::beginTransaction();
$certify->update($data);
if ($data['status'] === 1) {
UserService::updateAuthTags($user, $certify->getAttribute('tags'), ['audit_at' => now()->toDateTimeString()]);
}
OperationLog::admin()->subject($user)->updateAction()->content('用户《%s》认证的申请,', $user->getFullName());
DB::commit();
match ($certify->getAttribute('status')) {
1 => Notification::send($certify->getAttribute('user'), new UserCertifySuccessNotification()),
2 => Notification::send($certify->getAttribute('user'), new UserCertifyFailNotification())
};
} catch (SingerLimitException $exception) {
DB::rollBack();
return $this->fail(ApiCode::VALIDATION_ERROR, '团队内歌手名额已满');
} catch (RuntimeException $exception) {
DB::rollBack();
throw $exception;
}
return $this->success(ApiCode::UPDATE_SUCCESS);
}
}