goframe 没有其他 orm 的 hasOne,hasMany, belongsTo 等等这样的模型关联。 goframe 说这个对开发者有一定的心智负担。goframe 有 scanList 和 自己的 with。先了解试试用吧。
https://goframe.org/pages/viewpage.action?pageId=1114326
https://goframe.org/pages/viewpage.action?pageId=7297190
之前完成了用户的登录、注册以及个人信息的获取。这个个人信息的获取,只是单表的信息。现在想要用户的其他信息。这个时候,就需要更多的查询。
如果使用 orm,就可以通过 with 就可以搞定的。但是 with 只有定义好了规则才有用。所以,这个时候就需要组合想要的 dao 了。而 dao 是通过 gf gen dao
生的,只要表不变, dao 就不变。重复生也不变,所以就不能去修改 dao。那么只能新文件中来组合了。
因为之前定义 req 和 res 的时候,已经在 model 里边加了一个文件。那么在其中再加一个组合关系就好。
于是,修改 internal/model/admin/user.go,增加以下组合。
type User struct {
entity.Users
UserExtends *entity.UserExtends `orm:"with:user_id=id" json:"user_extends"`
Roles []map[string]interface{} `json:"roles,omitempty"`
Permissions []Permission `json:"permissions,omitempty"`
}
// 用户权限
type Permission struct {
entity.Permissions
Permission []Permission `json:"permission,omitempty"`
}
从组合中可以看到使用 orm 来获取的仅仅是扩展信息。另外的 roles 和 permissions 是另外赋值进去的。
roles 和 permissions 之所以是另外赋值。是因为它们的获取会关联更多的表。不太适合嵌套去处理。
虽然 User 中定义了 with 相关的 UserExtends。 如果调用的时候,没使用 with 也是不会去查询的。其他的两个,如果不赋值也是不会有的。最基础的 Users 依然还是 entity 里的那个 user。
后来还是改了,表名还是用复数吧。所以命令回来生成的 dao 和 model 也是复数。复数就复数吧。
铺了这么多路,业务还是要落在 logic 里了。改成这样子。
func (s *sUser) UserInfo(ctx context.Context, in model.UserInfoReq) (res model.UserInfoRes, err error) {
var user *model.User
id := jwt.NewJwt().GetIdentity(ctx)
// 用户的基本信息
err = dao.Users.Ctx(ctx).WithAll().Where(do.Users{
Id: id,
}).Scan(&user)
if err != nil {
return
}
res.User = *user
// 所有角色
sql := `SELECT r.id, r.name, r.title FROM roles as r
LEFT JOIN model_has_roles as mr ON r.id = mr.role_id
WHERE mr.model_id = ` + gconv.String(user.Id)
result, err := g.DB().Query(ctx, sql)
if err != nil {
return
}
res.Roles = result.List()
// 用户拥有的权限
sql = `SELECT DISTINCT p.id,p.guard_name,p.name,p.icon, p.title, p.path,p.parent_id,p.hidden,p.always_show,p.component,p.link,p.iframe,p.redirect,p.order FROM permissions AS p
LEFT JOIN role_has_permissions AS rp ON rp.permission_id = p.id
LEFT JOIN model_has_roles AS mr ON mr.role_id = rp.role_id
LEFT JOIN users AS u ON u.id = mr.model_id
WHERE u.id =` + gconv.String(user.Id)
result, err = g.DB().Query(ctx, sql)
if err != nil {
return
}
var ps []model.Permission
err = result.Structs(&ps)
if err != nil {
return
}
res.Permissions = transPermissions(ps, 0)
return res, err
}
// 将权限递归处理一下
func transPermissions(ps []model.Permission, parent_id uint64) []model.Permission {
temp := []model.Permission{}
for _, item := range ps {
if item.ParentId == parent_id {
item.Permission = transPermissions(ps, item.Id)
temp = append(temp, item)
}
}
return temp
}
跑起来,获取用户信息看看。
返回完整的数据。
{
"code": 0,
"message": "",
"data": {
"id": 1,
"account": "13671638524",
"phone": "13671638524",
"email": "zhoulin@xiangrong.pro",
"password": "$2y$10$H6SdkTD6Sb4DHWBag2npOeMKygZZJKjUplKStQZB19kSuunjOyV6u",
"nickname": "七月羽歌",
"avatar": "",
"gender": 1,
"signature": "",
"email_verified_at": "2023-09-09 13:13:00",
"remember_token": "PCCrLK4xBt",
"created_at": "2023-09-09 13:13:00",
"updated_at": "2023-09-09 13:13:02",
"user_extends": {
"id": 1,
"user_id": 1,
"view_nums": 0,
"fans_nums": 0,
"follow_nums": 0,
"admin_role": "",
"created_at": "2023-09-09 13:13:00",
"updated_at": "2023-09-09 13:13:00"
},
"roles": [
{
"id": 1,
"name": "root",
"title": "站长"
},
{
"id": 2,
"name": "default",
"title": "呦呦鸣鹿"
}
],
"permissions": [
{
"id": 1,
"name": "home",
"guard_name": "admin",
"path": "/",
"icon": "home",
"title": "主页",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "Layout",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 0,
"order": 0,
"created_at": null,
"updated_at": null,
"permission": [
{
"id": 2,
"name": "dashboard",
"guard_name": "admin",
"path": "dashboard",
"icon": "",
"title": "仪表盘",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 1,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 3,
"name": "baidu",
"guard_name": "admin",
"path": "baidu",
"icon": "",
"title": "百度",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "https://www.baidu.com",
"iframe": "",
"redirect": "",
"parent_id": 1,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 4,
"name": "element",
"guard_name": "admin",
"path": "element",
"icon": "",
"title": "Element plus",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "https://element-plus.org/zh-CN/guide/design.html",
"redirect": "",
"parent_id": 1,
"order": 0,
"created_at": null,
"updated_at": null
}
]
},
{
"id": 5,
"name": "personal",
"guard_name": "admin",
"path": "/personal",
"icon": "personal",
"title": "个人",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 0,
"order": 0,
"created_at": null,
"updated_at": null,
"permission": [
{
"id": 6,
"name": "personal.profile",
"guard_name": "admin",
"path": "profile",
"icon": "",
"title": "个人信息",
"hidden": 1,
"keep_alive": 0,
"always_show": 1,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 5,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 7,
"name": "personal.password",
"guard_name": "admin",
"path": "password",
"icon": "",
"title": "密码",
"hidden": 1,
"keep_alive": 0,
"always_show": 1,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 5,
"order": 0,
"created_at": null,
"updated_at": null
}
]
},
{
"id": 8,
"name": "system",
"guard_name": "admin",
"path": "/system",
"icon": "system",
"title": "系统管理",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 0,
"order": 0,
"created_at": null,
"updated_at": null,
"permission": [
{
"id": 9,
"name": "permission",
"guard_name": "admin",
"path": "permission",
"icon": "",
"title": "权限管理",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 8,
"order": 0,
"created_at": null,
"updated_at": null,
"permission": [
{
"id": 10,
"name": "permission.index",
"guard_name": "admin",
"path": "index",
"icon": "",
"title": "权限",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 9,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 11,
"name": "permission.create",
"guard_name": "admin",
"path": "create",
"icon": "",
"title": "添加权限",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 9,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 12,
"name": "permission.edit",
"guard_name": "admin",
"path": "edit",
"icon": "",
"title": "编辑权限",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 9,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 13,
"name": "permission.delete",
"guard_name": "admin",
"path": "delete",
"icon": "",
"title": "删除权限",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 9,
"order": 0,
"created_at": null,
"updated_at": null
}
]
},
{
"id": 14,
"name": "role",
"guard_name": "admin",
"path": "role",
"icon": "",
"title": "角色管理",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 8,
"order": 0,
"created_at": null,
"updated_at": null,
"permission": [
{
"id": 15,
"name": "role.index",
"guard_name": "admin",
"path": "index",
"icon": "",
"title": "角色",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 14,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 16,
"name": "role.create",
"guard_name": "admin",
"path": "create",
"icon": "",
"title": "添加角色",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 14,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 17,
"name": "role.edit",
"guard_name": "admin",
"path": "edit",
"icon": "",
"title": "编辑角色",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 14,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 18,
"name": "role.delete",
"guard_name": "admin",
"path": "delete",
"icon": "",
"title": "删除角色",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 14,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 19,
"name": "role.permission",
"guard_name": "admin",
"path": "permission",
"icon": "",
"title": "分配权限",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 14,
"order": 0,
"created_at": null,
"updated_at": null
}
]
},
{
"id": 20,
"name": "user",
"guard_name": "admin",
"path": "user",
"icon": "",
"title": "用户管理",
"hidden": 0,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 8,
"order": 0,
"created_at": null,
"updated_at": null,
"permission": [
{
"id": 21,
"name": "user.index",
"guard_name": "admin",
"path": "index",
"icon": "",
"title": "用户",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 20,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 22,
"name": "user.edit",
"guard_name": "admin",
"path": "edit",
"icon": "",
"title": "编辑用户",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 20,
"order": 0,
"created_at": null,
"updated_at": null
},
{
"id": 23,
"name": "user.role",
"guard_name": "admin",
"path": "role",
"icon": "",
"title": "分配角色",
"hidden": 1,
"keep_alive": 0,
"always_show": 0,
"component": "",
"link": "",
"iframe": "",
"redirect": "",
"parent_id": 20,
"order": 0,
"created_at": null,
"updated_at": null
}
]
}
]
}
]
}
}