feat: 添加用户分页/详情/新增/更新/删除接口
This commit is contained in:
157
app/admin/controller/v1/User.php
Normal file
157
app/admin/controller/v1/User.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\SysUserModel;
|
||||
use app\admin\validate\v1\SysUserValidate;
|
||||
|
||||
/**
|
||||
* 用户管理控制器
|
||||
*/
|
||||
class User
|
||||
{
|
||||
// 用户分页数据
|
||||
public function index()
|
||||
{
|
||||
$params = request()->get([
|
||||
'username',
|
||||
'status',
|
||||
'page/d' => 1,
|
||||
'size/d' => 10
|
||||
]);
|
||||
|
||||
$users = SysUserModel::withoutField([
|
||||
'password',
|
||||
'salt',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->with(['role' => function($query) {
|
||||
$query->field(['id', 'name' => 'role_name']);
|
||||
}])
|
||||
->withSearch(['username'], [
|
||||
'username' => $params['username']??null,
|
||||
])
|
||||
->status($params['status']??null)
|
||||
->order('id', 'desc')
|
||||
->paginate([
|
||||
'list_rows' => $params['size'],
|
||||
'page' => $params['page'],
|
||||
])
|
||||
->bindAttr('role', ['role_name'])
|
||||
->hidden(['role_id', 'role']);
|
||||
|
||||
return success('获取成功', $users);
|
||||
}
|
||||
|
||||
// 用户详情
|
||||
public function read($id)
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$user = SysUserModel::withoutField([
|
||||
'password',
|
||||
'salt',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
])
|
||||
->bypk($id)
|
||||
->find();
|
||||
if (empty($user)) {
|
||||
return error('用户不存在');
|
||||
}
|
||||
|
||||
return success('获取成功', $user);
|
||||
}
|
||||
|
||||
// 用户新增
|
||||
public function save()
|
||||
{
|
||||
$post = request()->post([
|
||||
'username',
|
||||
'password',
|
||||
'repassword',
|
||||
'nickname',
|
||||
'avatar',
|
||||
'mobile',
|
||||
'email',
|
||||
'role_id',
|
||||
'status' => 1,
|
||||
]);
|
||||
$post = array_merge($post, ['salt' => random_str(16)]);
|
||||
|
||||
$validate = new SysUserValidate;
|
||||
if (!$validate->scene('create')->check($post)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$post['password'] = password_with_salt($post['password'], $post['salt']);
|
||||
unset($post['repassword']);
|
||||
$user = SysUserModel::create($post);
|
||||
if ($user->isEmpty()) {
|
||||
return error('新增失败');
|
||||
}
|
||||
|
||||
return success('新增成功');
|
||||
}
|
||||
|
||||
// 用户更新
|
||||
public function update()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$put = request()->put([
|
||||
'username',
|
||||
'password',
|
||||
'repassword',
|
||||
'nickname',
|
||||
'avatar',
|
||||
'mobile',
|
||||
'email',
|
||||
'role_id',
|
||||
'status' => 1,
|
||||
]);
|
||||
$data = array_merge($put, ['id' => $id, 'salt' => random_str(16)]);
|
||||
|
||||
$validate = new SysUserValidate;
|
||||
if (!$validate->check($data)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$user = SysUserModel::bypk($id)->find();
|
||||
if (empty($user)) {
|
||||
return error('请确认要操作的对象是否存在');
|
||||
}
|
||||
|
||||
if (!empty($data['password'])) {
|
||||
$data['password'] = password_with_salt($data['password'], $data['salt']);
|
||||
} else {
|
||||
unset($data['password']);
|
||||
}
|
||||
unset($data['repassword']);
|
||||
if (!$user->save($data)) {
|
||||
return error('操作失败');
|
||||
}
|
||||
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
// 用户删除
|
||||
public function delete()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$user = SysUserModel::bypk($id)->find();
|
||||
if (empty($user)) {
|
||||
return error('请确认要操作的对象是否存在');
|
||||
}
|
||||
|
||||
if (!$user->delete()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
|
||||
return success('操作成功');
|
||||
}
|
||||
}
|
||||
14
app/admin/model/v1/SysRoleAuthorityModel.php
Normal file
14
app/admin/model/v1/SysRoleAuthorityModel.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysRoleAuthorityModel extends Model
|
||||
{
|
||||
//
|
||||
}
|
||||
14
app/admin/model/v1/SysRoleModel.php
Normal file
14
app/admin/model/v1/SysRoleModel.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use app\common\model\SysRoleBaseModel;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysRoleModel extends SysRoleBaseModel
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -4,18 +4,51 @@ declare (strict_types = 1);
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use app\common\model\SysUserBaseModel;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
/**
|
||||
* 用户模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysUserModel extends SysUserBaseModel
|
||||
{
|
||||
// 启用软件删除
|
||||
use SoftDelete;
|
||||
// 软件删除字段
|
||||
protected $deleteTime = 'deleted_at';
|
||||
// 自动写入时间格式
|
||||
protected $autoWriteTimestamp = 'datetime';
|
||||
|
||||
// 隐藏字段
|
||||
protected $hidden = ['password', 'salt'];
|
||||
|
||||
// 关联角色
|
||||
public function role()
|
||||
{
|
||||
return $this->belongsTo(SysRoleModel::class, 'role_id', 'id');
|
||||
}
|
||||
|
||||
// 用户名搜索
|
||||
public function searchUsernameAttr($query, $value)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
$query->where('username', 'like', "%{$value}%");
|
||||
}
|
||||
|
||||
// 用户名查询范围
|
||||
public function scopeUsernameOrMobile($query, $username)
|
||||
{
|
||||
return $query->where('username', '=', $username)->whereOr('mobile', '=', $username);
|
||||
$query->where('username', '=', $username)->whereOr('mobile', '=', $username);
|
||||
}
|
||||
|
||||
// 状态查询
|
||||
public function scopeStatus($query, $status)
|
||||
{
|
||||
if (empty($status)) {
|
||||
return;
|
||||
}
|
||||
$query->where('status', '=', $status);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,6 +331,24 @@ Route::group('v1', function () {
|
||||
Route::delete('delete/:id', 'AttachmentTrash/delete');
|
||||
});
|
||||
});
|
||||
|
||||
// 用户管理
|
||||
Route::group('user', function() {
|
||||
// 用户分页
|
||||
Route::get('index', 'User/index');
|
||||
|
||||
// 用户详情
|
||||
Route::get('read/:id', 'User/read');
|
||||
|
||||
// 用户新增
|
||||
Route::post('save', 'User/save');
|
||||
|
||||
// 用户更新
|
||||
Route::put('update/:id', 'User/update');
|
||||
|
||||
// 用户删除
|
||||
Route::delete('delete/:id', 'User/delete');
|
||||
});
|
||||
})->prefix('v1.');
|
||||
|
||||
Route::miss(function() {
|
||||
|
||||
68
app/admin/validate/v1/SysUserValidate.php
Normal file
68
app/admin/validate/v1/SysUserValidate.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class SysUserValidate extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'id' => 'require|integer',
|
||||
'username' => 'require|unique:sys_user|max:64',
|
||||
'password' => 'require|max:64',
|
||||
'repassword' => 'requireWith:password|confirm:password',
|
||||
'salt' => 'require|max:16',
|
||||
'role_id' => 'require|integer',
|
||||
'nickname' => 'max:64',
|
||||
'avatar' => 'max:255',
|
||||
'mobile' => 'mobile',
|
||||
'email' => 'email',
|
||||
'status' => 'in:-1,1',
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.integer' => 'ID必须为整数',
|
||||
'username.require' => '用户名不能为空',
|
||||
'username.unique' => '用户名已存在',
|
||||
'username.max' => '用户名最多不能超过64个字符',
|
||||
'password.require' => '密码不能为空',
|
||||
'password.max' => '密码最多不能超过64个字符',
|
||||
'repassword.requireWith' => '确认密码不能为空',
|
||||
'repassword.confirm' => '两次密码输入不一致',
|
||||
'salt.require' => '盐不能为空',
|
||||
'salt.max' => '盐最多不能超过16个字符',
|
||||
'role_id.require' => '角色ID不能为空',
|
||||
'role_id.integer' => '角色ID必须为整数',
|
||||
'nickname.max' => '昵称最多不能超过64个字符',
|
||||
'avatar.max' => '头像最多不能超过255个字符',
|
||||
'mobile' => '手机号格式错误',
|
||||
'email' => '邮箱格式错误',
|
||||
'status.in' => '状态值必须是-1或1',
|
||||
];
|
||||
|
||||
// 新增校验场景
|
||||
public function sceneCreate()
|
||||
{
|
||||
return $this->remove('id', 'require|integer');
|
||||
}
|
||||
|
||||
// 更新校验场景
|
||||
public function sceneUpdate()
|
||||
{
|
||||
return $this->remove('repassword', 'requireWith:password|confirm:password');
|
||||
}
|
||||
}
|
||||
29
app/common/model/SysRoleBaseModel.php
Normal file
29
app/common/model/SysRoleBaseModel.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
/**
|
||||
* 角色模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysRoleBaseModel extends BaseModel
|
||||
{
|
||||
// 数据表名
|
||||
protected $name = 'sys_role';
|
||||
|
||||
// 主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 字段信息
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'pid' => 'int',
|
||||
'name' => 'string',
|
||||
'desc' => 'string',
|
||||
'status' => 'int',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'deleted_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
@@ -3,12 +3,11 @@ declare (strict_types = 1);
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 用户模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysUserBaseModel extends Model
|
||||
class SysUserBaseModel extends BaseModel
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'sys_user';
|
||||
@@ -30,5 +29,6 @@ class SysUserBaseModel extends Model
|
||||
'status' => 'int',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'deleted_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user