RoleController.php 3.83 KB
<?php

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

use App\Enums\RoleStatusEnum;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\System\RoleRequest;
use App\Models\SystemRole;
use App\Support\Controller;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;

class RoleController 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', 'fetchType');
        $fetchType = $request->get('fetchType', 'page');
        $build     = SystemRole::filter($filter);

        return match ($fetchType) {
            'all' => $this->successWithData($build->get()),
            default => $this->successWithData($build->paginate($pageSize))
        };
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\RoleRequest $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function store(RoleRequest $request): Response
    {
        $attribute = $request->safe()->toArray();
        $role      = SystemRole::query()->create($attribute + ['user_id' => Auth::id(), 'guard' => 'Admin']);
        OperationLog::admin()->createAction()->subject($role)->content('角色《%s》', $role->getAttribute('name'));
        return $this->successWithData($role, ApiCode::CREATE_SUCCESS);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\RoleRequest $request
     * @param \App\Models\SystemRole                                       $role
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(RoleRequest $request, SystemRole $role): Response
    {
        $role->update($request->safe()->toArray());
        OperationLog::admin()->updateAction()->subject($role)->content('角色《%s》', $role->getAttribute('name'));
        return $this->successWithData($role, ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\SystemRole   $role
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function changeStatus(Request $request, SystemRole $role): Response
    {
        $request->validate(['status' => 'required|in:0,1'], ['status.required' => '请选择状态', 'status.in' => '状态值不在指定范围内']);
        $status = RoleStatusEnum::from($request->offsetGet('status'));
        $role->update(['status' => $status->value]);
        OperationLog::admin()->statusAction()->subject($role)->content('%s角色《%s》', $status->label(), $role->getAttribute('name'));
        return $this->success(ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\SystemRole   $role
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function changePermission(Request $request, SystemRole $role): Response
    {
        $request->validate(['permission' => 'present|array'], ['permission.present' => '请选择分配的权限']);
        $role->permissions()->sync($request->get('permission', []));
        OperationLog::admin()->updateAction()->subject($role)->content('角色《%s》权限内容', $role->getAttribute('name'));
        return $this->success(ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \App\Models\SystemRole $role
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function destroy(SystemRole $role): Response
    {
        SystemRole::query()->whereKey($role->getKey())->limit(1)->delete();
        OperationLog::admin()->deleteAction()->subject($role)->content('角色《%s》', $role->getAttribute('name'));
        return $this->success(ApiCode::DELETE_SUCCESS);
    }
}