实现角色接口增删改查
This commit is contained in:
@ -19,6 +19,27 @@ type sUser struct{}
|
||||
|
||||
func init() {
|
||||
service.RegisterUser(New())
|
||||
go checkUserRole()
|
||||
}
|
||||
|
||||
func checkUserRole() {
|
||||
ctx := context.Background()
|
||||
exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.UserRoleCode}).Exist()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !exist {
|
||||
_, err := dao.Roles.Ctx(ctx).Data(do.Roles{
|
||||
Name: "用户",
|
||||
Code: consts.UserRoleCode,
|
||||
Description: "用户角色",
|
||||
Status: 1,
|
||||
IsDeletable: false,
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func New() service.IUser {
|
||||
@ -26,12 +47,15 @@ func New() service.IUser {
|
||||
}
|
||||
|
||||
func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.UserLoginOut, err error) {
|
||||
value, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.UserRoleCode}).Fields(dao.Roles.Columns().Code).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找角色失败")
|
||||
}
|
||||
// 根据 OpenId 查找用户
|
||||
exist, err := dao.Users.Ctx(ctx).Where(do.Users{WxOpenId: in.OpenId}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找用户失败")
|
||||
}
|
||||
|
||||
var userId int64
|
||||
if !exist {
|
||||
// 用户不存在,创建新用户
|
||||
@ -62,8 +86,8 @@ func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.Us
|
||||
|
||||
// 生成 token
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{
|
||||
UserId: userId,
|
||||
Permission: consts.UserPermission,
|
||||
UserId: userId,
|
||||
Role: value.String(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("生成token失败")
|
||||
|
||||
Reference in New Issue
Block a user