diff --git a/pear_admin/apis/passport.py b/pear_admin/apis/passport.py index 3752553..5280f6a 100644 --- a/pear_admin/apis/passport.py +++ b/pear_admin/apis/passport.py @@ -1,16 +1,12 @@ from collections import OrderedDict from copy import deepcopy -from flask import Blueprint, make_response, redirect, request +from flask import Blueprint, make_response, request from flask_jwt_extended import ( create_access_token, create_refresh_token, get_current_user, jwt_required, - set_access_cookies, - set_refresh_cookies, - unset_access_cookies, - unset_refresh_cookies, ) from pear_admin.extensions import db @@ -35,10 +31,14 @@ def login_in(): access_token = create_access_token(user) refresh_token = create_refresh_token(user) - response = make_response({"code": 0, "message": "登录成功"}) - - set_access_cookies(response, access_token) - set_refresh_cookies(response, refresh_token) + response = make_response( + { + "code": 0, + "message": "登录成功", + "access_token": access_token, + "refresh_token": refresh_token, + } + ) return response @@ -46,10 +46,7 @@ def login_in(): @passport_api.route("/logout", methods=["GET", "POST"]) @jwt_required() def logout(): - response = make_response(redirect("/login")) - unset_access_cookies(response) - unset_refresh_cookies(response) - return response + return {"msg": "退出登录成功", "code": 0} @passport_api.get("/menu") diff --git a/pear_admin/apis/role.py b/pear_admin/apis/role.py index ff3b9d6..08a9319 100644 --- a/pear_admin/apis/role.py +++ b/pear_admin/apis/role.py @@ -1,5 +1,3 @@ -from copy import deepcopy - from flask import Blueprint, request from flask_sqlalchemy.pagination import Pagination diff --git a/pear_admin/apis/user.py b/pear_admin/apis/user.py index 0baaedc..dd6702f 100644 --- a/pear_admin/apis/user.py +++ b/pear_admin/apis/user.py @@ -1,6 +1,7 @@ from datetime import datetime from flask import Blueprint, request +from flask_jwt_extended import current_user, jwt_required from flask_sqlalchemy.pagination import Pagination from pear_admin.extensions import db @@ -89,3 +90,13 @@ def change_user_role(rid): user.role_list = [r[0] for r in role_obj_list] user.save() return {"code": 0, "message": "授权成功"} + + +@user_api.get("/user/profile") +@jwt_required() +def user_profile(): + return { + "code": 0, + "msg": "获取个人数据成功", + "data": current_user.json(), + } diff --git a/pear_admin/extensions/init_jwt.py b/pear_admin/extensions/init_jwt.py index 3d077b6..21ea0f8 100644 --- a/pear_admin/extensions/init_jwt.py +++ b/pear_admin/extensions/init_jwt.py @@ -19,9 +19,9 @@ def user_lookup_callback(_jwt_header, jwt_data): @jwt.expired_token_loader def expired_token_callback(): - return redirect("/login") + return {"msg": "token 已过期,请重新登录", "code": -1}, 403 @jwt.unauthorized_loader def missing_token_callback(error): - return redirect("/login") + return {"msg": "操作未授权,请重新登录", "code": -1}, 403 diff --git a/pear_admin/views/__init__.py b/pear_admin/views/__init__.py index 36feac2..7c95125 100644 --- a/pear_admin/views/__init__.py +++ b/pear_admin/views/__init__.py @@ -1,9 +1,9 @@ from flask import Flask from .index import index_bp -from .views import views_bp +from .system import system_bp def register_views(app: Flask): app.register_blueprint(index_bp) - app.register_blueprint(views_bp) + app.register_blueprint(system_bp) diff --git a/pear_admin/views/index.py b/pear_admin/views/index.py index dbc425f..a5c9b4d 100644 --- a/pear_admin/views/index.py +++ b/pear_admin/views/index.py @@ -1,24 +1,21 @@ -from flask import Blueprint, render_template, send_from_directory -from flask_jwt_extended import jwt_required +from flask import Blueprint, render_template, send_file, send_from_directory index_bp = Blueprint("index", __name__) @index_bp.route("/") -@jwt_required() def index(): - return render_template("index.html") + return render_template("view/index.html") -@index_bp.route("/login.html") -@index_bp.route("/login") +@index_bp.route("/view/login.html") def login(): - return render_template("login.html") + return render_template("view/login.html") -@index_bp.route("/register.html") +@index_bp.route("/view/register.html") def register(): - return render_template("register.html") + return render_template("view/register.html") @index_bp.route("/view/console/index.html") diff --git a/pear_admin/views/system.py b/pear_admin/views/system.py new file mode 100644 index 0000000..6799ae2 --- /dev/null +++ b/pear_admin/views/system.py @@ -0,0 +1,23 @@ +from flask import Blueprint, render_template + +system_bp = Blueprint("system", __name__) + + +@system_bp.get("/system//") +def system_view(path1, path2): + return render_template(f"system/{path1}/{path2}") + + +@system_bp.get("/views/role.html") +def role_view(): + return render_template("system/role/index.html") + + +@system_bp.get("/views/department.html") +def department_view(): + return render_template("system/department/index.html") + + +@system_bp.get("/views/user.html") +def user_view(): + return render_template("system/user/index.html") diff --git a/pear_admin/views/views.py b/pear_admin/views/views.py deleted file mode 100644 index c897da8..0000000 --- a/pear_admin/views/views.py +++ /dev/null @@ -1,33 +0,0 @@ -from flask import Blueprint, render_template - -views_bp = Blueprint("views", __name__) - - -@views_bp.get("/views/rights") -def rights_view(): - return render_template("views/rights/index.html") - - -@views_bp.get("/views/role") -def role_view(): - return render_template("views/role/index.html") - - -@views_bp.get("/views/role_rights/") -def role_rights_view(rid): - return render_template("views/role/role_rights.html", rid=rid) - - -@views_bp.get("/views/user_role/") -def user_role_view(rid): - return render_template("views/user/user_role.html", rid=rid) - - -@views_bp.get("/views/department") -def department_view(): - return render_template("views/department/index.html") - - -@views_bp.get("/views/user") -def user_view(): - return render_template("views/user/index.html") diff --git a/static/data/ums_rights.csv b/static/data/ums_rights.csv index 11f7a48..032768e 100644 --- a/static/data/ums_rights.csv +++ b/static/data/ums_rights.csv @@ -1,21 +1,21 @@ id,name,code,type,url,icon_sign,status,sort,open_type,pid 100,系统管理,ums:rights,menu,,layui-icon layui-icon-set-fill,,2,,0 -101,权限管理,ums:rights:list,path,/views/rights,layui-icon layui-icon-console,,1,,100 +101,权限管理,ums:rights:list,path,/system/rights/index.html,layui-icon layui-icon-console,,1,,100 102,新增权限,ums:rights:create,auth,,,,0,,101 103,修改权限,ums:rights:update,auth,,,,0,,101 104,获取权限,ums:rights:read,auth,,,,0,,101 105,删除权限,ums:rights:delete,auth,,,,0,,101 -106,角色管理,ums:role,path,/views/role,layui-icon layui-icon-console,,2,,100 +106,角色管理,ums:role,path,/system/role/index.html,layui-icon layui-icon-console,,2,,100 107,新增角色,ums:role:create,auth,,,,0,,106 109,修改角色,ums:role:update,auth,,,,0,,106 108,查询角色,ums:role:read,auth,,,,0,,106 110,删除角色,ums:role:delete,auth,,,,0,,106 -111,部门管理,ums:department,path,/views/department,layui-icon layui-icon-console,,3,,100 +111,部门管理,ums:department,path,/system/department/index.html,layui-icon layui-icon-console,,3,,100 112,新建部门,ums:department:create,auth,,,,0,,111 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 +116,用户管理,ums:user,path,/system/user/index.html,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 diff --git a/templates/views/department/index.html b/templates/system/department/index.html similarity index 100% rename from templates/views/department/index.html rename to templates/system/department/index.html diff --git a/templates/views/rights/index.html b/templates/system/rights/index.html similarity index 100% rename from templates/views/rights/index.html rename to templates/system/rights/index.html diff --git a/templates/views/role/index.html b/templates/system/role/index.html similarity index 70% rename from templates/views/role/index.html rename to templates/system/role/index.html index 8c03998..fa846a6 100644 --- a/templates/views/role/index.html +++ b/templates/system/role/index.html @@ -136,11 +136,45 @@ + + + + diff --git a/templates/views/user/index.html b/templates/system/user/index.html similarity index 71% rename from templates/views/user/index.html rename to templates/system/user/index.html index 22642a2..fba89fb 100644 --- a/templates/views/user/index.html +++ b/templates/system/user/index.html @@ -150,12 +150,44 @@ + + +
+
+
    +
    +
    +
    + + +
    +
    +
    diff --git a/templates/index.html b/templates/view/index.html similarity index 82% rename from templates/index.html rename to templates/view/index.html index 363d56f..6d1ab00 100644 --- a/templates/index.html +++ b/templates/view/index.html @@ -120,13 +120,42 @@ var admin = layui.admin; var popup = layui.popup; + // 请求后端,严重是否已经登录 + fetch("/api/v1/user/profile", { + method: "GET", + headers: { + Authorization: "Bearer " + localStorage.getItem("access_token"), + }, + }) + .then((res) => res.json()) + .then(function (data) { + if (data.code !== 0) { + location.href = "/view/login.html"; + } + }); + + // 给 pear-admin-layui 的默认 ajax 请求添加权限 + $.ajaxSetup({ + headers: { + Authorization: "Bearer " + localStorage.getItem("access_token"), + }, + }); + admin.setConfigurationPath("/static/config/pear.config.json"); admin.render(); admin.logout(function () { popup.success("注销成功", function () { - location.href = "/api/v1/logout"; + fetch("/api/v1/logout") + .then((res) => res.json()) + .then(function (data) { + if (data.code !== 0) { + localStorage.removeItem("access_token"); + localStorage.removeItem("refresh_token"); + location.href = "/view/login.html"; + } + }); }); // 清空 tabs 缓存 diff --git a/templates/login.html b/templates/view/login.html similarity index 95% rename from templates/login.html rename to templates/view/login.html index a86d1fa..1f95cff 100644 --- a/templates/login.html +++ b/templates/view/login.html @@ -94,7 +94,7 @@ @@ -127,6 +127,8 @@ data: JSON.stringify(data.field), contentType: "application/json", success: function (res) { + localStorage.setItem("access_token", res.access_token); + localStorage.setItem("refresh_token", res.refresh_token); btn.stop(function () { popup.success("登录成功", function () { location.href = "/"; diff --git a/templates/register.html b/templates/view/register.html similarity index 98% rename from templates/register.html rename to templates/view/register.html index 8a45c52..ba616c3 100644 --- a/templates/register.html +++ b/templates/view/register.html @@ -118,7 +118,7 @@ diff --git a/templates/views/role/role_rights.html b/templates/views/role/role_rights.html deleted file mode 100644 index dbd898a..0000000 --- a/templates/views/role/role_rights.html +++ /dev/null @@ -1,109 +0,0 @@ - - - - - 角色管理 - - - - - - - - - - - -
    -
    -
    -
      -
      -
      -
      - - -
      -
      -
      -
      - - - - diff --git a/templates/views/user/user_role.html b/templates/views/user/user_role.html deleted file mode 100644 index 9143409..0000000 --- a/templates/views/user/user_role.html +++ /dev/null @@ -1,112 +0,0 @@ - - - - - 用户授权 - - - - - - - - - - - -
      -
      -
      -
        -
        -
        -
        - - -
        -
        -
        -
        - - - -