MemberController.php 1.61 KB
<?php

namespace App\Http\Container\AdminSection\Controllers\User;

use App\Helpers\OperationLog;
use App\Http\Service\UserService;
use App\Models\GroupHasMember;
use App\Models\User;
use App\Support\Controller;
use Hikoon\LaravelApi\Facades\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MemberController extends Controller
{
    /**
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function index(Request $request, int $id): Response
    {
        $pageSize = $request->get('pageSize', 20);
        $filter   = $request->except(['page', 'pageSize']);

        $data = GroupHasMember::filter($filter)
            ->with('member:id,nick_name,real_name,avatar,email,phone,area_code,sex,official_status,status,scope,identity')
            ->whereRelation('group', 'groups.user_id', $id)
            ->where('role', '!=', 1)
            ->paginate($pageSize);

        return $this->successWithData($data);
    }

    /**
     * @param int                           $userId
     * @param \App\Http\Service\UserService $userService
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function destroy(int $userId): Response
    {
        $user = User::query()->findOrFail($userId);
        DB::transaction(static function () use ($user) {
            UserService::deleteGroupMember($user);
            OperationLog::admin()->subject($user)->updateAction()->content('用户《%s》解绑经纪人', $user->getFullName());
        });

        return $this->successWithData($user);
    }
}