From b91b256204898bcdced9ccb561f3aa6b82394a8b Mon Sep 17 00:00:00 2001 From: guosl Date: Wed, 21 Aug 2024 11:25:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/sql/init/user.sql | 13 +++++++------ internal/models/application/application.go | 1 + internal/models/user/user.go | 6 +++--- internal/repo/menu.go | 1 + internal/service/user/user.go | 2 +- internal/service/user/util.go | 3 ++- pkg/utils/context/context.go | 18 ++++++++++++++++++ server/user/user.go | 12 +++++++++--- 8 files changed, 42 insertions(+), 14 deletions(-) diff --git a/deploy/sql/init/user.sql b/deploy/sql/init/user.sql index 31753c9..3b07a66 100644 --- a/deploy/sql/init/user.sql +++ b/deploy/sql/init/user.sql @@ -140,6 +140,7 @@ CREATE TABLE IF NOT EXISTS `menu`( `icon` varchar(255) NOT NULL DEFAULT '' COMMENT '图标', `path` varchar(512) NOT NULL DEFAULT '' COMMENT '访问路径', `child_path` varchar(255) NOT NULL DEFAULT '' COMMENT '子级路径(前端用)', + `component` varchar(512) NOT NULL DEFAULT '' COMMENT '组件路径', `go_first_child` tinyint(1) NOT NULL DEFAULT 0 COMMENT '前端用', `is_show` int(5) NOT NULL DEFAULT 0 COMMENT '是否显示(0:否;1:是)', `created_by` VARCHAR(64) DEFAULT '' COMMENT '创建人', @@ -193,11 +194,11 @@ REPLACE INTO `user`(`account`,`name`,`mobile`,`email`,`pwd`,`salt`,`created_by`) REPLACE INTO `application` (`code`,`secret`,`name`,`description`,`created_by`) VALUES('user-busniss-center','146E5EA66B4B6036C6BBF395CC90C30E','用户中心','用户,统一登陆等','admin'); --init menu -REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`is_show`,`created_by`) VALUES('user-busniss-center','index','首页','',1,0,'home','/home','',1,'admin'); -REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`is_show`,`created_by`) VALUES('user-busniss-center','user','用户','',1,1,'user','/user','',1,'admin'); -REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`is_show`,`created_by`) VALUES('user-busniss-center','organization','组织','',1,2,'organization','/organization','',1,'admin'); -REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`is_show`,`created_by`) VALUES('user-busniss-center','role','角色','',1,3,'role','/role','',1,'admin'); -REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`is_show`,`created_by`) VALUES('user-busniss-center','application','应用','',1,4,'application','/application','',1,'admin'); +-- REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`component`,`is_show`,`created_by`) VALUES('user-busniss-center','index','首页','',1,0,'home','/home','','/view/dashboard/index.vue',1,'admin'); +REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`component`,`is_show`,`created_by`) VALUES('user-busniss-center','user','用户','',1,1,'user','/user','','/view/user/index.vue',1,'admin'); +REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`component`,`is_show`,`created_by`) VALUES('user-busniss-center','organization','组织','',1,2,'organization','/organization','','/view/organization/index.vue',1,'admin'); +REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`component`,`is_show`,`created_by`) VALUES('user-busniss-center','role','角色','',1,3,'role','/role','','/view/role/index.vue',1,'admin'); +REPLACE INTO `menu`(`app_code`,`code`,`name`,`parent_code`,`type`,`order`,`icon`,`path`,`child_path`,`component`,`is_show`,`created_by`) VALUES('user-busniss-center','application','应用','',1,4,'application','/application','','/view/application/index.vue',1,'admin'); -- init role REPLACE INTO `role` (`code`,`name`,`created_by`) VALUES('super_admin','超级管理员','admin'); @@ -205,4 +206,4 @@ REPLACE INTO `role` (`code`,`name`,`created_by`) VALUES('admin','管理员','adm REPLACE INTO `role` (`code`,`name`,`created_by`) VALUES('develop','开发者','admin'); -REPLACE INTO `user_role` (`user_account`,`role_code`,`created_by`) VALUES('admin','super_admin','admin'); \ No newline at end of file +REPLACE INTO `user_role` (`user_account`,`role_code`,`created_by`) VALUES('admin','super_admin','admin'); diff --git a/internal/models/application/application.go b/internal/models/application/application.go index c6cb184..d8abf44 100644 --- a/internal/models/application/application.go +++ b/internal/models/application/application.go @@ -28,6 +28,7 @@ type Menu struct { ChildPath string `json:"child_path"` // 子级路径(前端用) GoFirstChild int8 `json:"go_first_child"` // 前端用 IsShow int32 `json:"is_show"` // 是否显示(0:否;1:是) + Component string `json:"component"` // 组件路径 Children []Menu `json:"children"` } diff --git a/internal/models/user/user.go b/internal/models/user/user.go index 686ae21..c666712 100644 --- a/internal/models/user/user.go +++ b/internal/models/user/user.go @@ -38,9 +38,9 @@ type UserInfo struct { type User struct { UserInfo - Roles []roleModel.Role - Orgs []orgModel.Organization - Menus appModel.MenuTree + Roles []roleModel.Role `json:"roles"` + Orgs []orgModel.Organization `json:"orgs"` + Menus []appModel.Menu `json:"menus"` } type ModifyInfo struct { diff --git a/internal/repo/menu.go b/internal/repo/menu.go index 3ad3136..3f11d21 100644 --- a/internal/repo/menu.go +++ b/internal/repo/menu.go @@ -25,6 +25,7 @@ type Menu struct { Icon string `gorm:"column:icon;NOT NULL"` // 图标 Path string `gorm:"column:path;NOT NULL"` // 访问路径 ChildPath string `gorm:"column:child_path;NOT NULL"` // 子级路径(前端用) + Component string `gorm:"column:component"` // 组件路径 GoFirstChild int `gorm:"column:go_first_child;default:0;NOT NULL"` // 前端用 IsShow int `gorm:"column:is_show;default:0;NOT NULL"` // 是否显示(0:否;1:是) CreatedBy string `gorm:"column:created_by"` // 创建人 diff --git a/internal/service/user/user.go b/internal/service/user/user.go index 86ad253..07016df 100644 --- a/internal/service/user/user.go +++ b/internal/service/user/user.go @@ -310,7 +310,7 @@ func (u *userService) GetUser(ctx context.Context, req *userModel.GetUserReq) (u return } - user.Menus = appModel.MenuTree{Menus: appInfo.Menus} + user.Menus = appInfo.Menus return } diff --git a/internal/service/user/util.go b/internal/service/user/util.go index 1d2e70f..0f23d0d 100644 --- a/internal/service/user/util.go +++ b/internal/service/user/util.go @@ -57,9 +57,10 @@ func (u *userService) setLoginStatus(ctx context.Context, user repo.User, claims if c, ok := ctx.(*gin.Context); ok { expires := u.conf.Jwt.Expires domain := u.conf.App.Host + // domain := contextHelper.GetHost(ctx) c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%d; Path=/;Domain=%s", COOKIE_KEY_TOKEN, tokenStr, expires, domain)) c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%d; Path=/;Domain=%s", COOKIE_KEY_ACCOUNT, claims["account"], expires, domain)) - c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%d; Path=/;Domain=%s", COOKIE_KEY_ID, claims["id"], expires, domain)) + c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=%d; Max-Age=%d; Path=/;Domain=%s", COOKIE_KEY_ID, claims["id"], expires, domain)) } return nil diff --git a/pkg/utils/context/context.go b/pkg/utils/context/context.go index 313531b..531151d 100644 --- a/pkg/utils/context/context.go +++ b/pkg/utils/context/context.go @@ -69,3 +69,21 @@ func PutSession(ctx context.Context, sess *session.Session) context.Context { return context.WithValue(ctx, contextSessionKey{}, sess) } } + +func GetHost(ctx context.Context) string { + switch c := ctx.(type) { + case *gin.Context: + return c.Request.Host + default: + return "" + } +} + +func GetPath(ctx context.Context) string { + switch c := ctx.(type) { + case *gin.Context: + return c.Request.URL.Path + default: + return "" + } +} diff --git a/server/user/user.go b/server/user/user.go index efe6515..ec434f5 100644 --- a/server/user/user.go +++ b/server/user/user.go @@ -5,6 +5,7 @@ import ( userModel "busniess-user-center/internal/models/user" "busniess-user-center/internal/service/user" + contextUtil "busniess-user-center/pkg/utils/context" ginUtil "busniess-user-center/pkg/utils/gin" "busniess-user-center/server/user/proto" "context" @@ -37,7 +38,7 @@ func RegisterRoute(api *gin.RouterGroup) { api.POST("/logout", ginUtil.WrapNo(server.Logout)) api.POST("/modify", ginUtil.WrapNoRsp(server.Modify)) api.GET("/search", ginUtil.Wrap(server.Search)) - api.GET("/user", ginUtil.Wrap(server.GetUser)) + api.GET("/user", ginUtil.WrapNoReq(server.GetUser)) api.POST("/reset", ginUtil.WrapNoRsp(server.ResetPwd)) api.POST("/enable", ginUtil.WrapNoRsp(server.Enable)) api.POST("/disable", ginUtil.WrapNoRsp(server.Disable)) @@ -89,8 +90,13 @@ func (u *UserServer) Search(ctx context.Context, query *base.Query) (*userModel. return u.userService.Search(ctx, query) } -func (u *UserServer) GetUser(ctx context.Context, req *userModel.GetUserReq) (userModel.User, error) { - return u.userService.GetUser(ctx, req) +func (u *UserServer) GetUser(ctx context.Context) (userModel.User, error) { + session, err := contextUtil.GetSession(ctx) + if err != nil { + return userModel.User{}, err + } + + return u.userService.GetUser(ctx, &userModel.GetUserReq{AppCode: session.AppCode, Account: session.Account}) } func (u *UserServer) ResetPwd(ctx context.Context, req *userModel.ResetPwdReq) error {