CertifyController.php 3.21 KB
<?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);
    }
}