feature: 用户管理
This commit is contained in:
@@ -4,6 +4,7 @@ from .department import department_api
|
||||
from .passport import passport_api
|
||||
from .rights import rights_api
|
||||
from .role import role_api
|
||||
from .user import user_api
|
||||
|
||||
|
||||
def register_apis(app: Flask):
|
||||
@@ -13,5 +14,6 @@ def register_apis(app: Flask):
|
||||
apis.register_blueprint(rights_api)
|
||||
apis.register_blueprint(role_api)
|
||||
apis.register_blueprint(department_api)
|
||||
apis.register_blueprint(user_api)
|
||||
|
||||
app.register_blueprint(apis)
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
from datetime import datetime
|
||||
|
||||
from flask import Blueprint, request
|
||||
from flask_sqlalchemy.pagination import Pagination
|
||||
|
||||
from pear_admin.extensions import db
|
||||
from pear_admin.orms import UserORM
|
||||
|
||||
user_api = Blueprint("user", __name__)
|
||||
|
||||
|
||||
@user_api.get("/user")
|
||||
def user_list():
|
||||
page = request.args.get("page", default=1, type=int)
|
||||
per_page = request.args.get("limit", default=10, type=int)
|
||||
q = db.select(UserORM)
|
||||
|
||||
pages: Pagination = db.paginate(q, page=page, per_page=per_page)
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "获取用户数据成功",
|
||||
"data": [item.json() for item in pages.items],
|
||||
"count": pages.total,
|
||||
}
|
||||
|
||||
|
||||
@user_api.post("/user")
|
||||
def create_user():
|
||||
data = request.get_json()
|
||||
if data["id"]:
|
||||
del data["id"]
|
||||
role = UserORM(**data)
|
||||
create_at = data['create_at']
|
||||
if create_at:
|
||||
role.create_at = datetime.strptime(create_at, '%Y-%m-%d %H:%M:%S')
|
||||
role.password = '123456'
|
||||
role.save()
|
||||
return {"code": 0, "message": "新增用户成功"}
|
||||
|
||||
|
||||
@user_api.put("/user/<int:uid>")
|
||||
def change_user(uid):
|
||||
data = request.get_json()
|
||||
del data["id"]
|
||||
|
||||
user_obj = UserORM.query.get(uid)
|
||||
for key, value in data.items():
|
||||
if key == 'create_at':
|
||||
value = datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
|
||||
setattr(user_obj, key, value)
|
||||
user_obj.save()
|
||||
return {"code": 0, "message": "修改用户信息成功"}
|
||||
|
||||
|
||||
@user_api.delete("/user/<int:rid>")
|
||||
def del_user(rid):
|
||||
user_obj = UserORM.query.get(rid)
|
||||
user_obj.delete()
|
||||
return {"code": 0, "message": "删除用户成功"}
|
||||
@@ -41,7 +41,7 @@ class UserORM(BaseORM):
|
||||
"nickname": self.nickname,
|
||||
"mobile": self.mobile,
|
||||
"email": self.email,
|
||||
"create_at": self.create_at,
|
||||
"create_at": self.create_at.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
}
|
||||
|
||||
@property
|
||||
|
||||
@@ -16,3 +16,8 @@ def role_view():
|
||||
@views_bp.get("/department")
|
||||
def department_view():
|
||||
return render_template("views/department/index.html")
|
||||
|
||||
|
||||
@views_bp.get("/user")
|
||||
def user_view():
|
||||
return render_template("views/user/index.html")
|
||||
|
||||
@@ -15,11 +15,11 @@ id,name,code,type,url,icon_sign,status,sort,open_type,pid
|
||||
113,修改部门,ums:department:update,auth,,,,0,,111
|
||||
114,删除部门,ums:department:delete,auth,,,,0,,111
|
||||
115,查询部门,ums:department:read,auth,,,,0,,111
|
||||
116,员工管理,ums:user,path,/views/user,layui-icon layui-icon-console,,4,,100
|
||||
117,新增员工,ums:user:create,auth,,,,0,,116
|
||||
118,修改员工,ums:user:update,auth,,,,0,,116
|
||||
119,查询员工,ums:user:read,auth,,,,0,,116
|
||||
120,查询员工,ums:user:read,auth,,,,0,,116
|
||||
116,用户管理,ums:user,path,/views/user,layui-icon layui-icon-console,,4,,100
|
||||
117,新增用户,ums:user:create,auth,,,,0,,116
|
||||
118,修改用户,ums:user:update,auth,,,,0,,116
|
||||
119,查询用户,ums:user:read,auth,,,,0,,116
|
||||
120,查询用户,ums:user:read,auth,,,,0,,116
|
||||
121,数据图表,,menu,,layui-icon layui-icon-chart,,3,,0
|
||||
122,折线图,,path,/views/echarts/line,layui-icon layui-icon-face-smile,,0,,121
|
||||
123,柱状图,,path,/views/echarts/column,layui-iconlayui-icon-face-smile,,0,,121
|
||||
|
||||
|
@@ -155,7 +155,7 @@
|
||||
{ field: "id", title: "ID", width: 80, sort: true },
|
||||
{ field: "name", title: "用户名", width: 120 },
|
||||
{ field: "code", title: "用户标识", width: 160 },
|
||||
{ field: "desc", title: "描述", minWidth: 80 },
|
||||
{ field: "desc", title: "描述", maxWidth: 120 },
|
||||
{ field: "ids", title: "权限列表", width: 100 },
|
||||
{
|
||||
fixed: "right",
|
||||
@@ -176,7 +176,7 @@
|
||||
content: $("#role-form"),
|
||||
area: ["50%", "80%"],
|
||||
});
|
||||
form.render($("#role"));
|
||||
form.render($("#role-form"));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-cn">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>用户管理</title>
|
||||
<meta name="renderer" content="webkit"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1, maximum-scale=1"
|
||||
/>
|
||||
</head>
|
||||
<body>
|
||||
<div style="padding: 16px">
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<table
|
||||
class="layui-hide"
|
||||
id="user-table"
|
||||
lay-filter="user-table"
|
||||
></table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<form
|
||||
class="layui-form"
|
||||
lay-filter="user-form"
|
||||
id="user-form"
|
||||
action=""
|
||||
style="padding: 15px; display: none"
|
||||
>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">ID</label>
|
||||
<div class="layui-input-block">
|
||||
<input
|
||||
type="text"
|
||||
name="id"
|
||||
value="0"
|
||||
lay-verify="required"
|
||||
autocomplete="off"
|
||||
class="layui-input"
|
||||
disabled
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">用户名</label>
|
||||
<div class="layui-input-block">
|
||||
<input
|
||||
type="text"
|
||||
name="username"
|
||||
placeholder="请输入用户名"
|
||||
lay-verify="required"
|
||||
autocomplete="off"
|
||||
class="layui-input"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">昵称</label>
|
||||
<div class="layui-input-block">
|
||||
<input
|
||||
type="text"
|
||||
name="nickname"
|
||||
placeholder="请输入用户标识"
|
||||
lay-verify="required"
|
||||
autocomplete="off"
|
||||
class="layui-input"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">手机号</label>
|
||||
<div class="layui-input-block">
|
||||
<input
|
||||
type="text"
|
||||
name="mobile"
|
||||
placeholder="请输入用户描述"
|
||||
autocomplete="off"
|
||||
class="layui-input"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">邮箱</label>
|
||||
<div class="layui-input-block">
|
||||
<input
|
||||
type="text"
|
||||
name="email"
|
||||
placeholder="请输入用户权限列表"
|
||||
autocomplete="off"
|
||||
class="layui-input"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">创建时间</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="create_at" class="layui-input" id="form_create_at"
|
||||
placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" lay-verify="required">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-input-block">
|
||||
<button
|
||||
type="submit"
|
||||
class="layui-btn"
|
||||
lay-submit
|
||||
lay-filter="user-form-btn"
|
||||
>
|
||||
立即提交
|
||||
</button>
|
||||
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<script type="text/html" id="user-toolbar">
|
||||
<div class="layui-btn-container">
|
||||
<button class="layui-btn layui-btn-sm" lay-event="user-toolbar-add">
|
||||
新增角色
|
||||
</button>
|
||||
</div>
|
||||
</script>
|
||||
<script type="text/html" id="user-tool">
|
||||
<div class="layui-btn-container">
|
||||
<button
|
||||
class="layui-btn layui-btn-sm layui-bg-blue"
|
||||
lay-event="user-tool-edit"
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
<button
|
||||
class="layui-btn layui-btn-sm layui-bg-yellow"
|
||||
lay-event="user-tool-detail"
|
||||
>
|
||||
授权
|
||||
</button>
|
||||
<button
|
||||
class="layui-btn layui-btn-sm layui-bg-red"
|
||||
lay-event="user-tool-del"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
</div>
|
||||
</script>
|
||||
<script>
|
||||
layui.use(function () {
|
||||
var table = layui.table;
|
||||
var $ = layui.$;
|
||||
var form = layui.form;
|
||||
var laydate = layui.laydate;
|
||||
|
||||
table.render({
|
||||
elem: "#user-table",
|
||||
id: "user-table",
|
||||
url: "/api/v1/user", // 此处为静态模拟数据,实际使用时需换成真实接口
|
||||
height: "full-35", // 最大高度减去其他容器已占有的高度差
|
||||
toolbar: "#user-toolbar",
|
||||
page: true,
|
||||
cols: [
|
||||
[
|
||||
//标题栏
|
||||
{field: "id", title: "ID", width: 80, sort: true},
|
||||
{field: "username", title: "用户名", width: 120},
|
||||
{field: "nickname", title: "昵称", width: 120},
|
||||
{field: "mobile", title: "手机号", width: 160},
|
||||
{field: "email", title: "邮箱", width: 120},
|
||||
{field: "create_at", title: "创建时间", width: 180},
|
||||
{
|
||||
fixed: "right",
|
||||
title: "操作",
|
||||
width: 180,
|
||||
align: "center",
|
||||
toolbar: "#user-tool",
|
||||
},
|
||||
],
|
||||
],
|
||||
});
|
||||
|
||||
table.on("toolbar(user-table)", function (obj) {
|
||||
if (obj.event === "user-toolbar-add") {
|
||||
layer.open({
|
||||
type: 1,
|
||||
shade: false,
|
||||
content: $("#user-form"),
|
||||
area: ["50%", "80%"],
|
||||
});
|
||||
form.render($("#user-form"));
|
||||
}
|
||||
});
|
||||
|
||||
table.on("tool(user-table)", function (obj) {
|
||||
if (obj.event === "user-tool-edit") {
|
||||
form.val("user-form", obj.data);
|
||||
layer.open({
|
||||
type: 1,
|
||||
shade: false,
|
||||
content: $("#user-form"),
|
||||
area: ["50%", "80%"],
|
||||
});
|
||||
} else if (obj.event === "user-tool-del") {
|
||||
$.ajax({
|
||||
url: `/api/v1/user/${obj.data.id}`,
|
||||
type: "DELETE",
|
||||
contentType: "application/json",
|
||||
headers: {
|
||||
"X-CSRF-TOKEN": getCookie("csrf_access_token"),
|
||||
},
|
||||
success: function (res) {
|
||||
if (!res.code) {
|
||||
table.reloadData("user-table");
|
||||
}
|
||||
},
|
||||
});
|
||||
} else if (obj.event === "user-tool-detail") {
|
||||
console.log("角色授权");
|
||||
}
|
||||
});
|
||||
|
||||
form.on("submit(user-form-btn)", function (data) {
|
||||
var field = data.field;
|
||||
let method, url;
|
||||
if (field.id == 0) {
|
||||
field.id = null;
|
||||
method = "POST";
|
||||
url = "/api/v1/user";
|
||||
} else {
|
||||
method = "PUT";
|
||||
url = `/api/v1/user/${field.id}`;
|
||||
}
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: method,
|
||||
contentType: "application/json",
|
||||
headers: {
|
||||
"X-CSRF-TOKEN": getCookie("csrf_access_token"),
|
||||
},
|
||||
data: JSON.stringify(field),
|
||||
success: function (res) {
|
||||
if (!res.code) {
|
||||
layer.closeAll();
|
||||
table.reloadData("user-table");
|
||||
}
|
||||
},
|
||||
});
|
||||
return false;
|
||||
});
|
||||
|
||||
// form_create_at
|
||||
laydate.render({
|
||||
elem: '#form_create_at',
|
||||
type: 'datetime'
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user