2026-08-28 15:06:01 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strconv"
|
2026-08-28 17:03:03 +08:00
|
|
|
|
"strings"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/common/httpx"
|
2026-08-28 17:03:03 +08:00
|
|
|
|
tokenx "bj_power_mes/common/token"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"bj_power_mes/internal/logic"
|
|
|
|
|
|
"bj_power_mes/internal/svc"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/rest/pathvar"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ctxUserId 从 JWT 上下文取用户ID(go-zero 将自定义 claim 放入 context)
|
|
|
|
|
|
func ctxUserId(r *http.Request) int {
|
|
|
|
|
|
switch v := r.Context().Value("userId").(type) {
|
|
|
|
|
|
case float64:
|
|
|
|
|
|
return int(v)
|
|
|
|
|
|
case int:
|
|
|
|
|
|
return v
|
|
|
|
|
|
case int64:
|
|
|
|
|
|
return int(v)
|
|
|
|
|
|
case string:
|
|
|
|
|
|
n, _ := strconv.Atoi(v)
|
|
|
|
|
|
return n
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 17:03:03 +08:00
|
|
|
|
// uidFromRequest 取当前登录用户ID:优先 context,缺失时回退解析 Authorization Bearer token(更稳健)
|
|
|
|
|
|
func uidFromRequest(r *http.Request, svcCtx *svc.ServiceContext) int {
|
|
|
|
|
|
if uid := ctxUserId(r); uid > 0 {
|
|
|
|
|
|
return uid
|
|
|
|
|
|
}
|
|
|
|
|
|
auth := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
|
|
|
|
|
if auth == "" || auth == r.Header.Get("Authorization") {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
claims, err := tokenx.Parse(svcCtx.Config.Auth.AccessSecret, auth)
|
|
|
|
|
|
if err == nil && claims.UserId > 0 {
|
|
|
|
|
|
return claims.UserId
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// ctxOperator 当前操作人:优先取 name,其次 username
|
|
|
|
|
|
func ctxOperator(r *http.Request) string {
|
|
|
|
|
|
if v, ok := r.Context().Value("name").(string); ok && v != "" {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := r.Context().Value("username").(string); ok && v != "" {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
return "system"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// pathId 读取路由参数 :id
|
|
|
|
|
|
func pathId(r *http.Request) int {
|
|
|
|
|
|
vars := pathvar.Vars(r)
|
|
|
|
|
|
if vars == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
n, _ := strconv.Atoi(vars["id"])
|
|
|
|
|
|
return n
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.LoginReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
data, err := logic.New(svcCtx).Login(r.Context(), req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1001, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func RefreshHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.RefreshReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
data, err := logic.New(svcCtx).Refresh(r.Context(), req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1002, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UserInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2026-08-28 17:03:03 +08:00
|
|
|
|
data, err := logic.New(svcCtx).UserInfo(r.Context(), uidFromRequest(r, svcCtx))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1003, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SeedHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if err := logic.New(svcCtx).Seed(r.Context()); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1004, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "初始化完成", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 用户管理 ----------
|
|
|
|
|
|
|
|
|
|
|
|
func ListUsersHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
data, err := logic.New(svcCtx).ListUsers(r.Context())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1005, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CreateUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.UserReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := logic.New(svcCtx).CreateUser(r.Context(), req); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1006, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "创建成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UpdateUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.UserReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := logic.New(svcCtx).UpdateUser(r.Context(), req); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1007, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "更新成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DeleteUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if err := logic.New(svcCtx).DeleteUser(r.Context(), pathId(r)); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1008, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "删除成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 17:03:03 +08:00
|
|
|
|
// ---------- 修改自己的密码 ----------
|
|
|
|
|
|
|
|
|
|
|
|
func ChangePasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.ChangePasswordReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := logic.New(svcCtx).ChangePassword(r.Context(), uidFromRequest(r, svcCtx), req); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1017, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "密码修改成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// ---------- 角色管理 ----------
|
|
|
|
|
|
|
|
|
|
|
|
func ListRolesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
data, err := logic.New(svcCtx).ListRoles(r.Context())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1009, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CreateRoleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.RoleReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := logic.New(svcCtx).CreateRole(r.Context(), req); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1010, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "创建成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UpdateRoleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.RoleReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := logic.New(svcCtx).UpdateRole(r.Context(), req); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1011, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "更新成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DeleteRoleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if err := logic.New(svcCtx).DeleteRole(r.Context(), pathId(r)); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1012, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "删除成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 权限管理 ----------
|
|
|
|
|
|
|
|
|
|
|
|
func ListPermissionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
data, err := logic.New(svcCtx).ListPermissions(r.Context())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1013, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CreatePermissionHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.PermissionReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := logic.New(svcCtx).CreatePermission(r.Context(), req); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1014, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "创建成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UpdatePermissionHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req logic.PermissionReq
|
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := logic.New(svcCtx).UpdatePermission(r.Context(), req); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1015, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "更新成功", nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DeletePermissionHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if err := logic.New(svcCtx).DeletePermission(r.Context(), pathId(r)); err != nil {
|
|
|
|
|
|
httpx.Fail(w, 1016, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpx.OkMessage(w, "删除成功", nil)
|
|
|
|
|
|
}
|
2026-08-28 17:03:03 +08:00
|
|
|
|
}
|