实现角色接口增删改查
This commit is contained in:
@ -2,6 +2,7 @@ package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"server/internal/consts"
|
||||
"server/internal/dao"
|
||||
@ -23,20 +24,53 @@ func New() service.IAdmin {
|
||||
|
||||
func checkAdmin() {
|
||||
ctx := context.Background()
|
||||
exist, err := dao.Admins.Ctx(ctx).Where(do.Admins{Username: "admin"}).Exist()
|
||||
if err != nil {
|
||||
panic("初始化管理员失败")
|
||||
}
|
||||
if !exist {
|
||||
passwordHash, _ := utility.EncryptPassword("Aa123456")
|
||||
_, err = dao.Admins.Ctx(ctx).Insert(do.Admins{
|
||||
Username: "admin",
|
||||
PasswordHash: passwordHash,
|
||||
Status: 1,
|
||||
})
|
||||
|
||||
if err := dao.Roles.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// 检查角色是否存在
|
||||
exist, err := tx.Model(dao.Roles.Table()).Where(do.Roles{Code: consts.AdminRoleCode}).Exist()
|
||||
if err != nil {
|
||||
panic("初始化管理员失败")
|
||||
return err
|
||||
}
|
||||
var roleId int64
|
||||
if !exist {
|
||||
roleId, err = tx.Model(dao.Roles.Table()).InsertAndGetId(do.Roles{
|
||||
Name: "系统管理员",
|
||||
Code: consts.AdminRoleCode,
|
||||
Description: "管理员角色",
|
||||
Status: 1,
|
||||
IsDeletable: false,
|
||||
})
|
||||
} else {
|
||||
value, err := tx.Model(dao.Roles.Table()).Where(do.Roles{Code: consts.AdminRoleCode}).Fields(dao.Roles.Columns().Id).Value()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
roleId = value.Int64()
|
||||
}
|
||||
|
||||
// 检查管理员是否存在
|
||||
exist, err = tx.Model(dao.Admins.Table()).Where(do.Admins{Username: "admin"}).Exist()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exist {
|
||||
password, err := utility.EncryptPassword("Aa123456")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Model(dao.Admins.Table()).InsertAndGetId(do.Admins{
|
||||
Username: "admin",
|
||||
PasswordHash: password,
|
||||
RoleId: roleId,
|
||||
Status: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
panic("初始化系统失败")
|
||||
}
|
||||
glog.Infof(ctx, "初始化管理员成功")
|
||||
}
|
||||
@ -60,7 +94,11 @@ func (s *sAdmin) Login(ctx context.Context, in *model.AdminLoginIn) (out *model.
|
||||
if !utility.ComparePassword(admin.PasswordHash, in.Password) {
|
||||
return nil, ecode.Auth
|
||||
}
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{UserId: admin.Id, Permission: consts.AdminPermission})
|
||||
value, err := dao.Roles.Ctx(ctx).WherePri(admin.RoleId).Fields(dao.Roles.Columns().Code).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{UserId: admin.Id, Role: value.String()})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("生成token失败")
|
||||
}
|
||||
@ -84,6 +122,7 @@ func (s *sAdmin) Info(ctx context.Context, in *model.AdminInfoIn) (out *model.Ad
|
||||
return nil, ecode.Fail.Sub("查询管理员失败")
|
||||
}
|
||||
out = &model.AdminInfoOut{
|
||||
Id: admin.Id,
|
||||
Username: admin.Username,
|
||||
}
|
||||
return
|
||||
|
||||
@ -7,6 +7,7 @@ package logic
|
||||
import (
|
||||
_ "server/internal/logic/admin"
|
||||
_ "server/internal/logic/merchantAdmin"
|
||||
_ "server/internal/logic/role"
|
||||
_ "server/internal/logic/storeAdmin"
|
||||
_ "server/internal/logic/user"
|
||||
)
|
||||
|
||||
162
internal/logic/role/role.go
Normal file
162
internal/logic/role/role.go
Normal file
@ -0,0 +1,162 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type sRole struct {
|
||||
}
|
||||
|
||||
func New() service.IRole {
|
||||
return &sRole{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterRole(New())
|
||||
}
|
||||
|
||||
func (s *sRole) Create(ctx context.Context, in *model.RoleCreateInput) (out *model.CreateOut, err error) {
|
||||
// 检查角色编码是否已存在
|
||||
exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: in.Code}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("新增角色查重出现异常")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("角色编码已存在")
|
||||
}
|
||||
|
||||
// 创建角色
|
||||
id, err := dao.Roles.Ctx(ctx).InsertAndGetId(do.Roles{
|
||||
Name: in.Name,
|
||||
Code: in.Code,
|
||||
Description: in.Description,
|
||||
Status: in.Status,
|
||||
IsDeletable: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("创建角色失败")
|
||||
}
|
||||
return &model.CreateOut{Id: id}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) Delete(ctx context.Context, in *model.RoleDeleteInput) (out *model.DeleteOut, err error) {
|
||||
// 检查角色是否存在且可删除
|
||||
role, err := dao.Roles.Ctx(ctx).WherePri(in.Id).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
if role.IsEmpty() {
|
||||
return nil, ecode.Params.Sub("角色不存在")
|
||||
}
|
||||
|
||||
if !role["is_deletable"].Bool() {
|
||||
return nil, ecode.Params.Sub("该角色不可删除")
|
||||
}
|
||||
|
||||
if err = dao.Roles.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
// 删除角色
|
||||
if _, err = dao.Roles.Ctx(ctx).Where(do.Roles{Id: in.Id}).Delete(); err != nil {
|
||||
err = ecode.Fail.Sub("删除角色失败")
|
||||
}
|
||||
// TODO : 删除角色后续需要处理的操作
|
||||
return
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.DeleteOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) BatchDelete(ctx context.Context, in *model.BatchDeleteIn) (out *model.DeleteOut, err error) {
|
||||
|
||||
// 检查角色是否存在且可删除
|
||||
roles, err := dao.Roles.Ctx(ctx).WhereIn(dao.Roles.Columns().Id, in.Ids).All()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
if len(roles) == 0 {
|
||||
return nil, ecode.Params.Sub("角色不存在")
|
||||
}
|
||||
|
||||
// 检查是否有不可删除的角色
|
||||
for _, role := range roles {
|
||||
if !role["is_deletable"].Bool() {
|
||||
return nil, ecode.Params.Sub("存在不可删除的角色")
|
||||
}
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
if err = dao.Roles.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// 删除角色
|
||||
if _, err = dao.Roles.Ctx(ctx).WhereIn(dao.Roles.Columns().Id, in.Ids).Delete(); err != nil {
|
||||
return ecode.Fail.Sub("删除角色失败")
|
||||
}
|
||||
// TODO : 删除角色后续需要处理的操作
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.DeleteOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) Update(ctx context.Context, in *model.RoleUpdateInput) (out *model.UpdateOut, err error) {
|
||||
// 检查角色是否存在
|
||||
exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Id: in.Id}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Params.Sub("角色不存在")
|
||||
}
|
||||
|
||||
// 检查角色编码是否已存在(排除自身)
|
||||
exist, err = dao.Roles.Ctx(ctx).
|
||||
Where(do.Roles{Code: in.Code}).
|
||||
WhereNot(dao.Roles.Columns().Id, in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色编码失败")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("角色编码已存在")
|
||||
}
|
||||
|
||||
// 更新角色
|
||||
if _, err = dao.Roles.Ctx(ctx).
|
||||
Where(do.Roles{Id: in.Id}).
|
||||
Update(do.Roles{
|
||||
Name: in.Name,
|
||||
Code: in.Code,
|
||||
Description: in.Description,
|
||||
Status: in.Status,
|
||||
}); err != nil {
|
||||
return nil, ecode.Fail.Sub("更新角色失败")
|
||||
}
|
||||
|
||||
return &model.UpdateOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) GetRoleList(ctx context.Context, in *model.RoleListInput) (out *model.RoleListOutput, err error) {
|
||||
list := make([]model.Role, 0)
|
||||
var total int
|
||||
// 构建查询条件
|
||||
orm := dao.Roles.Ctx(ctx)
|
||||
if in.Status > 0 {
|
||||
orm = orm.Where(dao.Roles.Columns().Status, in.Status)
|
||||
}
|
||||
|
||||
// 获取分页数据
|
||||
if err = orm.Page(in.Page, in.Size).ScanAndCount(&list, &total, false); err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色列表失败")
|
||||
}
|
||||
return &model.RoleListOutput{
|
||||
List: list,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
@ -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