You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
247 lines
7.6 KiB
247 lines
7.6 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AdminRoles;
|
|
use App\Models\AdminUsers;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\AdminUsersService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
/**
|
|
* @var AdminUsersService
|
|
*/
|
|
protected AdminUsersService $AdminUserModelService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param AdminUsersService $AdminUsersService
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
AdminUsersService $AdminUsersService,
|
|
) {
|
|
$this->responseService = $responseService;
|
|
$this->AdminUserModelService = $AdminUsersService;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = AdminUsers::query();
|
|
|
|
// 关键词搜索
|
|
if ($request->has('keyword')) {
|
|
$keyword = $request->input('keyword');
|
|
$query->where('name', 'like', "%{$keyword}%");
|
|
}
|
|
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
$total = $query->count();
|
|
$items = $query->latest()->forPage($page, $perPage)->get()->each(
|
|
function ($item) {
|
|
$item['status_str'] = $item['status'] ? __('admin.normal')
|
|
: __('admin.freeze');
|
|
$item['role_name'] = $item->roles->value('name');
|
|
unset($item['roles']);
|
|
return $item;
|
|
}
|
|
);
|
|
|
|
return $this->responseService->success([
|
|
'items' => $items,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'per_page' => $perPage,
|
|
'last_page' => ceil($total / $perPage),
|
|
]);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.get_user_info_list_failed');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'roles_list' => [],
|
|
'permissions_list' => [],
|
|
'packing_list' => []
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$this->saveValidator($request->all());
|
|
|
|
$model = $this->AdminUserModelService->createModel($request->all());
|
|
|
|
return $this->responseService->success($model, '创建用户成功');
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.create_admin_user_failed') . ':' . $e->getMessage(
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param int $id
|
|
* @return void
|
|
* @throws ValidationException
|
|
*/
|
|
private function saveValidator(array $data, int $id = 0): void
|
|
{
|
|
$rules = [
|
|
'username' => 'required|alpha_num|between:4,20',
|
|
'role_id' => 'required|numeric',
|
|
'packing_id' => 'required|numeric',
|
|
'password' => 'required|between:12,30',
|
|
];
|
|
$messages = [
|
|
'username.required' => __('validation.admin_user.l_a_empty'),
|
|
'username.alpha_num' => __('validation.admin_user.l_a_alpha_num'),
|
|
'username.between' => __('validation.admin_user.l_a_between'),
|
|
'role_id.required' => __('validation.admin_user.r_empty'),
|
|
'packing_id.required' => __('validation.admin_user.s_p_empty'),
|
|
'password.required' => __('validation.admin_user.p_empty'),
|
|
'password.between' => __('validation.admin_user.p_between'),
|
|
];
|
|
if ($id) {
|
|
$data['id'] = $id;
|
|
$rules['id'] = 'required|numeric';
|
|
$messages['id.required'] = __('validation.admin_user.id_empty');
|
|
$messages['id.numeric'] = __('validation.admin_user.id_numeric');
|
|
}
|
|
$validator = Validator::make($data, $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$model = AdminUsers::findOrFail($id);
|
|
return $this->responseService->success($model);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'item' => AdminUsers::query()
|
|
->where('id', $id)
|
|
->get()
|
|
->toArray(),
|
|
'roles' => AdminRoles::getRolesList()
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function update(Request $request, string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->saveValidator($request->all(), $id);
|
|
|
|
$this->AdminUserModelService->updateModel($request->all(), $id);
|
|
|
|
return $this->responseService->success(
|
|
null,
|
|
__('controller.rule.update_success')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.create_admin_user_failed') . ':' . $e->getMessage(
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
*/
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->AdminUserModelService->deleteModel($id);
|
|
|
|
return $this->responseService->success(null, '删除数据模型成功');
|
|
} catch (CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
'删除数据模型失败:' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|