|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
use App\Models\Admin;
|
|
|
use App\Models\OperateLog;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
|
|
class AdminController extends CommonController
|
|
|
{
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/admin",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="获取后台用户",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
|
|
|
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
|
|
|
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
|
|
|
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
|
|
|
* @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=true, description="关键词"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="获取后台用户"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function index()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$data = (new Admin())->with(["roles", "department"])->orderBy("sortnumber");
|
|
|
if (request()->keyword) {
|
|
|
$data = $data->where(function ($query) {
|
|
|
$query->where("name", "like", "%" . request()->keyword . "%")
|
|
|
->orWhere("username", "like", "%" . request()->keyword . "%")
|
|
|
->orWhere("mobile", "like", "%" . request()->keyword . "%");
|
|
|
});
|
|
|
}
|
|
|
$data = $data->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')->paginate($all['page_size'] ?? 20);
|
|
|
return $this->success($data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/show",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="获取后台用户详情",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="获取后台用户"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function show()
|
|
|
{
|
|
|
$data = (new Admin())->with(["roles", "department"])->find(request()->id);
|
|
|
return $this->success($data);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/admin/save",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="保存后台用户",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), description="菜单ID,为空表示新增,不为空表示更新"),
|
|
|
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="名称"),
|
|
|
* @OA\Parameter(name="username", in="query", @OA\Schema(type="string"), required=true, description="用户名"),
|
|
|
* @OA\Parameter(name="password", in="query", @OA\Schema(type="string"), description="密码,新建时请前端处理为必填,修改时留空表示不修改"),
|
|
|
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), description="手机号码"),
|
|
|
* @OA\Parameter(name="department_id", in="query", @OA\Schema(type="integer"), description="所属部门ID"),
|
|
|
* @OA\Parameter(name="position", in="query", @OA\Schema(type="string"), description="职位"),
|
|
|
* @OA\Parameter(name="sortnumber", in="query", @OA\Schema(type="integer"), description="同级排序,默认为0"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="保存后台用户"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function save()
|
|
|
{
|
|
|
if (request()->id) {
|
|
|
return $this->update();
|
|
|
} else {
|
|
|
return $this->store();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function store()
|
|
|
{
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
$data = (new Admin())->filterRequestColumns(request(), ["id"]);
|
|
|
$model = Admin::create($data);
|
|
|
DB::commit();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "新增管理员[{$model->name}]成功");
|
|
|
return $this->success($model);
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "新增管理员失败", $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function update()
|
|
|
{
|
|
|
DB::beginTransaction();
|
|
|
$model = Admin::find(request()->id);
|
|
|
try {
|
|
|
$data = $model->filterRequestColumns(request(), ["id"]);
|
|
|
$model->update($data);
|
|
|
DB::commit();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "更新管理员[{$model->name}]信息成功");
|
|
|
return $this->success($model);
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "更新管理员[{$model->name}]信息失败", $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/admin/delete",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="删除后台用户",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), description="ID"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="删除后台用户"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function delete(Request $request)
|
|
|
{
|
|
|
$admin = Admin::find($request->id);
|
|
|
try {
|
|
|
$admin->delete();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "删除管理员[{$admin->name}]用户成功");
|
|
|
return $this->success("删除成功");
|
|
|
} catch (\Exception $exception) {
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "删除管理员[{$admin->name}]失败", $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/admin/set-roles",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="后台用户授权",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=true, description="ID"),
|
|
|
* @OA\Parameter(name="role_id", in="query", @OA\Schema(type="object"), required=true, description="角色id,数组形式"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="后台用户授权"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
|
|
|
public function setRoles(Request $request)
|
|
|
{
|
|
|
$admin = (new Admin())->find($request->id);
|
|
|
$roles = (new Role())->whereIn("id", (array)$request->role_id)->get();
|
|
|
|
|
|
try {
|
|
|
$admin->syncRoles($roles);
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "用户[{$admin->name}]授权成功");
|
|
|
return $this->success("授权成功");
|
|
|
} catch (\Exception $exception) {
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "用户[{$admin->name}]授权失败", $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/admin/set-roles-many",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="后台用户授权批量处理",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="ids", in="query", @OA\Schema(type="object"), required=true, description="用户id数组"),
|
|
|
* @OA\Parameter(name="role_id", in="query", @OA\Schema(type="object"), required=true, description="角色id"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="后台用户授权"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
|
|
|
public function setRolesMany(Request $request)
|
|
|
{
|
|
|
$ids = $request->get('ids', []);
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
$model_has_roles = config("permission.table_names.model_has_roles");
|
|
|
$admin_roles = [];
|
|
|
foreach ($ids as $id) {
|
|
|
$admin_roles[] = [
|
|
|
"model_type" => Admin::class,
|
|
|
"model_id" => $id,
|
|
|
"role_id" => $request->role_id
|
|
|
];
|
|
|
}
|
|
|
DB::table($model_has_roles)->where('role_id', $request->role_id)->where('model_type', Admin::class)->delete();
|
|
|
DB::table($model_has_roles)->insert($admin_roles);
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), '用户批量授权成功');
|
|
|
DB::commit();
|
|
|
return $this->success("授权成功");
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), '用户批量授权失败', $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|