mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-14 21:23:47 +08:00
tt
This commit is contained in:
358
hotgo-server/app/service/adminService/dept_service.go
Normal file
358
hotgo-server/app/service/adminService/dept_service.go
Normal file
@@ -0,0 +1,358 @@
|
||||
package adminService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/bufanyun/hotgo/app/consts"
|
||||
"github.com/bufanyun/hotgo/app/form/input"
|
||||
"github.com/bufanyun/hotgo/app/model/entity"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dao"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Dept = dept{}
|
||||
|
||||
type dept struct{}
|
||||
|
||||
//
|
||||
// @Title 菜单名称是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *dept) NameUnique(ctx context.Context, in input.AdminDeptNameUniqueInp) (*input.AdminDeptNameUniqueModel, error) {
|
||||
|
||||
var res input.AdminDeptNameUniqueModel
|
||||
isUnique, err := dao.AdminDept.IsUniqueName(ctx, in.Id, in.Name)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.IsUnique = isUnique
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 删除
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *dept) Delete(ctx context.Context, in input.AdminDeptDeleteInp) error {
|
||||
|
||||
exist, err := dao.AdminRoleDept.Ctx(ctx).Where("dept_id", in.Id).One()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !exist.IsEmpty() {
|
||||
return gerror.New("请先解除该部门下所有已关联用户关联关系!")
|
||||
}
|
||||
_, err = dao.AdminDept.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 修改/新增
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *dept) Edit(ctx context.Context, in input.AdminDeptEditInp) (err error) {
|
||||
|
||||
if in.Name == "" {
|
||||
err = gerror.New("名称不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
uniqueName, err := dao.AdminDept.IsUniqueName(ctx, in.Id, in.Name)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueName {
|
||||
err = gerror.New("名称已存在")
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改
|
||||
in.UpdatedAt = gtime.Now()
|
||||
if in.Id > 0 {
|
||||
_, err = dao.AdminDept.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
in.CreatedAt = gtime.Now()
|
||||
_, err = dao.AdminDept.Ctx(ctx).Data(in).Insert()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 最大排序
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictDataMaxSortRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *dept) MaxSort(ctx context.Context, in input.AdminDeptMaxSortInp) (*input.AdminDeptMaxSortModel, error) {
|
||||
var res input.AdminDeptMaxSortModel
|
||||
|
||||
if in.Id > 0 {
|
||||
if err := dao.AdminDept.Ctx(ctx).Where("id", in.Id).Order("sort desc").Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
res.Sort = res.Sort + 10
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定字典类型信息
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeViewRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *dept) View(ctx context.Context, in input.AdminDeptViewInp) (res *input.AdminDeptViewModel, err error) {
|
||||
|
||||
if err = dao.AdminDept.Ctx(ctx).Where("id", in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *dept) List(ctx context.Context, in input.AdminDeptListInp) (list []*input.AdminDeptListModel, err error) {
|
||||
|
||||
mod := dao.AdminDept.Ctx(ctx)
|
||||
|
||||
var (
|
||||
dataList []*entity.AdminDept
|
||||
models []*DeptTree
|
||||
//searchResult []*entity.AdminDept
|
||||
//id int64
|
||||
//ids []int64
|
||||
)
|
||||
|
||||
// 部门名称
|
||||
if in.Name != "" {
|
||||
//err = dao.AdminDept.Ctx(ctx).WhereLike("name", "%"+in.Name+"%").Scan(&searchResult)
|
||||
//if err != nil {
|
||||
// err = gerror.Wrap(err, consts.ErrorORM)
|
||||
// return nil, err
|
||||
//}
|
||||
//for i := 0; i < len(searchResult); i++ {
|
||||
// id, err = dao.AdminDept.TopPid(ctx, searchResult[i])
|
||||
// ids = append(ids, id)
|
||||
//}
|
||||
//
|
||||
//if len(ids) == 0 {
|
||||
// return nil, nil
|
||||
//}
|
||||
//mod = mod.Where("id", ids)
|
||||
}
|
||||
|
||||
err = mod.Order("id desc").Scan(&dataList)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, err
|
||||
}
|
||||
|
||||
_ = gconv.Structs(dataList, &models)
|
||||
|
||||
childIds := service.getDeptChildIds(ctx, models, 0)
|
||||
|
||||
_ = gconv.Structs(childIds, &list)
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
type DeptTree struct {
|
||||
entity.AdminDept
|
||||
Children []*DeptTree `json:"children"`
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 将列表转为父子关系列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param lists
|
||||
// @Param pid
|
||||
// @Return []*RelationTree
|
||||
//
|
||||
func (service *dept) getDeptChildIds(ctx context.Context, lists []*DeptTree, pid int64) []*DeptTree {
|
||||
|
||||
var (
|
||||
count = len(lists)
|
||||
newLists []*DeptTree
|
||||
)
|
||||
|
||||
if count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := 0; i < len(lists); i++ {
|
||||
if lists[i].Id > 0 && lists[i].Pid == pid {
|
||||
var row *DeptTree
|
||||
if err := gconv.Structs(lists[i], &row); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
row.Children = service.getDeptChildIds(ctx, lists, row.Id)
|
||||
newLists = append(newLists, row)
|
||||
}
|
||||
}
|
||||
|
||||
return newLists
|
||||
}
|
||||
|
||||
type DeptListTree struct {
|
||||
Id int64 `json:"id" `
|
||||
Key int64 `json:"key" `
|
||||
Pid int64 `json:"pid" `
|
||||
Label string `json:"label"`
|
||||
Title string `json:"title"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Children []*DeptListTree `json:"children"`
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取列表树
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *dept) ListTree(ctx context.Context, in input.AdminDeptListTreeInp) (list []*input.AdminDeptListTreeModel, err error) {
|
||||
|
||||
mod := dao.AdminDept.Ctx(ctx)
|
||||
|
||||
var (
|
||||
dataList []*entity.AdminDept
|
||||
models []*DeptListTree
|
||||
)
|
||||
|
||||
err = mod.Order("id desc").Scan(&dataList)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, err
|
||||
}
|
||||
|
||||
_ = gconv.Structs(dataList, &models)
|
||||
|
||||
// TODO 重写树入参
|
||||
for i := 0; i < len(models); i++ {
|
||||
models[i].Key = models[i].Id
|
||||
models[i].Title = models[i].Name
|
||||
models[i].Label = models[i].Name
|
||||
}
|
||||
|
||||
childIds := service.getDeptTreeChildIds(ctx, models, 0)
|
||||
|
||||
_ = gconv.Structs(childIds, &list)
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 将列表转为父子关系列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param lists
|
||||
// @Param pid
|
||||
// @Return []*RelationTree
|
||||
//
|
||||
func (service *dept) getDeptTreeChildIds(ctx context.Context, lists []*DeptListTree, pid int64) []*DeptListTree {
|
||||
|
||||
var (
|
||||
count = len(lists)
|
||||
newLists []*DeptListTree
|
||||
)
|
||||
|
||||
if count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := 0; i < len(lists); i++ {
|
||||
if lists[i].Id > 0 && lists[i].Pid == pid {
|
||||
var row *DeptListTree
|
||||
if err := gconv.Structs(lists[i], &row); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
row.Children = service.getDeptTreeChildIds(ctx, lists, row.Id)
|
||||
newLists = append(newLists, row)
|
||||
}
|
||||
}
|
||||
|
||||
return newLists
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取部门名称
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param id
|
||||
// @Return name
|
||||
// @Return err
|
||||
//
|
||||
func (service *dept) GetName(ctx context.Context, id int64) (name string, err error) {
|
||||
|
||||
var data entity.AdminDept
|
||||
|
||||
err = dao.AdminDept.Ctx(ctx).
|
||||
Where("id", id).
|
||||
Fields("name").
|
||||
Scan(&data)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return name, err
|
||||
}
|
||||
|
||||
return data.Name, nil
|
||||
}
|
||||
67
hotgo-server/app/service/adminService/member_post_service.go
Normal file
67
hotgo-server/app/service/adminService/member_post_service.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package adminService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/bufanyun/hotgo/app/consts"
|
||||
"github.com/bufanyun/hotgo/app/model/entity"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dao"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
var MemberPost = new(memberPost)
|
||||
|
||||
type memberPost struct{}
|
||||
|
||||
func (service *memberPost) UpdatePostIds(ctx context.Context, member_id int64, post_ids []int64) (err error) {
|
||||
_, err = dao.AdminMemberPost.Ctx(ctx).
|
||||
Where("member_id", member_id).
|
||||
Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, "删除失败")
|
||||
return err
|
||||
}
|
||||
|
||||
for i := 0; i < len(post_ids); i++ {
|
||||
_, err = dao.AdminMemberPost.Ctx(ctx).
|
||||
Insert(entity.AdminMemberPost{
|
||||
MemberId: member_id,
|
||||
PostId: post_ids[i],
|
||||
})
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, "插入会员岗位失败")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定会员的岗位ids
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param member_id
|
||||
// @Return post_ids
|
||||
// @Return err
|
||||
//
|
||||
func (service *memberPost) GetMemberByIds(ctx context.Context, member_id int64) (post_ids []int64, err error) {
|
||||
|
||||
var list []*entity.AdminMemberPost
|
||||
err = dao.AdminMemberPost.Ctx(ctx).
|
||||
Fields("post_id").
|
||||
Where("member_id", member_id).
|
||||
Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return post_ids, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(list); i++ {
|
||||
post_ids = append(post_ids, list[i].PostId)
|
||||
}
|
||||
|
||||
g.Log().Print(ctx, "post_ids:", post_ids)
|
||||
return post_ids, nil
|
||||
}
|
||||
657
hotgo-server/app/service/adminService/member_service.go
Normal file
657
hotgo-server/app/service/adminService/member_service.go
Normal file
@@ -0,0 +1,657 @@
|
||||
//
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package adminService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/bufanyun/hotgo/app/com"
|
||||
"github.com/bufanyun/hotgo/app/consts"
|
||||
"github.com/bufanyun/hotgo/app/form/adminForm"
|
||||
"github.com/bufanyun/hotgo/app/form/input"
|
||||
"github.com/bufanyun/hotgo/app/model"
|
||||
"github.com/bufanyun/hotgo/app/model/entity"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dao"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dto"
|
||||
"github.com/gogf/gf/v2/crypto/gmd5"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
)
|
||||
|
||||
var Member = new(member)
|
||||
|
||||
type member struct{}
|
||||
|
||||
//
|
||||
// @Title 修改登录密码
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) UpdateProfile(ctx context.Context, in input.AdminMemberUpdateProfileInp) (err error) {
|
||||
|
||||
memberId := com.Context.Get(ctx).User.Id
|
||||
if memberId <= 0 {
|
||||
err := gerror.New("获取用户信息失败!")
|
||||
return err
|
||||
}
|
||||
|
||||
var memberInfo entity.AdminMember
|
||||
if err = dao.AdminMember.Ctx(ctx).Where("id", memberId).Scan(&memberInfo); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dao.AdminMember.Ctx(ctx).
|
||||
Where("id", memberId).
|
||||
Data(g.Map{
|
||||
"mobile": in.Mobile,
|
||||
"email": in.Email,
|
||||
"realname": in.Realname,
|
||||
}).
|
||||
Update()
|
||||
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 修改登录密码
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) UpdatePwd(ctx context.Context, in input.AdminMemberUpdatePwdInp) (err error) {
|
||||
|
||||
var memberInfo entity.AdminMember
|
||||
if err = dao.AdminMember.Ctx(ctx).Where("id", in.Id).Scan(&memberInfo); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
if gmd5.MustEncryptString(in.OldPassword+memberInfo.Salt) != memberInfo.PasswordHash {
|
||||
err = gerror.New("原密码不正确")
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dao.AdminMember.Ctx(ctx).
|
||||
Where("id", in.Id).
|
||||
Data(g.Map{
|
||||
"password_hash": gmd5.MustEncryptString(in.NewPassword + memberInfo.Salt),
|
||||
"updated_at": gtime.Now(),
|
||||
}).
|
||||
Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 重置密码
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) ResetPwd(ctx context.Context, in input.AdminMemberResetPwdInp) (err error) {
|
||||
|
||||
var memberInfo entity.AdminMember
|
||||
if err = dao.AdminMember.Ctx(ctx).Where("id", in.Id).Scan(&memberInfo); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dao.AdminMember.Ctx(ctx).
|
||||
Where("id", in.Id).
|
||||
Data(g.Map{
|
||||
"password_hash": gmd5.MustEncryptString(in.Password + memberInfo.Salt),
|
||||
"updated_at": gtime.Now(),
|
||||
}).
|
||||
Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 菜单名称是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) EmailUnique(ctx context.Context, in input.AdminMemberEmailUniqueInp) (*input.AdminMemberEmailUniqueModel, error) {
|
||||
|
||||
var res input.AdminMemberEmailUniqueModel
|
||||
isUnique, err := dao.AdminMember.IsUniqueEmail(ctx, in.Id, in.Email)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.IsUnique = isUnique
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 手机号是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) MobileUnique(ctx context.Context, in input.AdminMemberMobileUniqueInp) (*input.AdminMemberMobileUniqueModel, error) {
|
||||
|
||||
var res input.AdminMemberMobileUniqueModel
|
||||
isUnique, err := dao.AdminMember.IsUniqueMobile(ctx, in.Id, in.Mobile)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.IsUnique = isUnique
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 菜单名称是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) NameUnique(ctx context.Context, in input.AdminMemberNameUniqueInp) (*input.AdminMemberNameUniqueModel, error) {
|
||||
|
||||
var res input.AdminMemberNameUniqueModel
|
||||
isUnique, err := dao.AdminMember.IsUniqueName(ctx, in.Id, in.Username)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.IsUnique = isUnique
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 删除
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) Delete(ctx context.Context, in input.AdminMemberDeleteInp) error {
|
||||
|
||||
exist, err := dao.AdminMember.Ctx(ctx).Where("member_id", in.Id).One()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !exist.IsEmpty() {
|
||||
return gerror.New("请先解除该部门下所有已关联用户关联关系!")
|
||||
}
|
||||
_, err = dao.AdminMember.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 修改/新增
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) Edit(ctx context.Context, in input.AdminMemberEditInp) (err error) {
|
||||
|
||||
if in.Username == "" {
|
||||
err = gerror.New("帐号不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
uniqueName, err := dao.AdminMember.IsUniqueName(ctx, in.Id, in.Username)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueName {
|
||||
err = gerror.New("帐号已存在")
|
||||
return err
|
||||
}
|
||||
|
||||
if in.Mobile != "" {
|
||||
uniqueMobile, err := dao.AdminMember.IsUniqueMobile(ctx, in.Id, in.Mobile)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueMobile {
|
||||
err = gerror.New("手机号已存在")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if in.Email != "" {
|
||||
uniqueEmail, err := dao.AdminMember.IsUniqueMobile(ctx, in.Id, in.Email)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueEmail {
|
||||
err = gerror.New("邮箱已存在")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 修改
|
||||
in.UpdatedAt = gtime.Now()
|
||||
if in.Id > 0 {
|
||||
_, err = dao.AdminMember.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新岗位
|
||||
err = MemberPost.UpdatePostIds(ctx, in.Id, in.PostIds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
in.CreatedAt = gtime.Now()
|
||||
|
||||
// 新增用户时的额外属性
|
||||
var data input.AdminMemberAddInp
|
||||
data.AdminMemberEditInp = in
|
||||
data.Salt = grand.S(6)
|
||||
data.PasswordHash = gmd5.MustEncryptString(data.Password + data.Salt)
|
||||
|
||||
g.Log().Print(ctx, "data.Salt:", data)
|
||||
insert, err := dao.AdminMember.Ctx(ctx).Data(data).Insert()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新岗位
|
||||
id, err := insert.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = MemberPost.UpdatePostIds(ctx, id, in.PostIds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 最大排序
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictDataMaxSortRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) MaxSort(ctx context.Context, in input.AdminMemberMaxSortInp) (*input.AdminMemberMaxSortModel, error) {
|
||||
var res input.AdminMemberMaxSortModel
|
||||
|
||||
if in.Id > 0 {
|
||||
if err := dao.AdminMember.Ctx(ctx).Where("id", in.Id).Order("sort desc").Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
res.Sort = res.Sort + 10
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定字典类型信息
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeViewRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *member) View(ctx context.Context, in input.AdminMemberViewInp) (res *input.AdminMemberViewModel, err error) {
|
||||
|
||||
if err = dao.AdminMember.Ctx(ctx).Where("id", in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *member) List(ctx context.Context, in input.AdminMemberListInp) (list []*input.AdminMemberListModel, totalCount int, err error) {
|
||||
|
||||
var authorization = com.Jwt.GetAuthorization(com.Context.Get(ctx).Request)
|
||||
// TODO 获取jwtToken
|
||||
jwtToken := consts.RedisJwtToken + gmd5.MustEncryptString(authorization)
|
||||
g.Log().Print(ctx, "jwtToken:", jwtToken)
|
||||
|
||||
mod := dao.AdminMember.Ctx(ctx)
|
||||
|
||||
if in.Realname != "" {
|
||||
mod = mod.WhereLike("realname", "%"+in.Realname+"%")
|
||||
}
|
||||
if in.Username != "" {
|
||||
mod = mod.WhereLike("username", "%"+in.Username+"%")
|
||||
}
|
||||
if in.Mobile > 0 {
|
||||
mod = mod.Where("mobile", in.Mobile)
|
||||
}
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where("status", in.Status)
|
||||
}
|
||||
if in.DeptId > 0 {
|
||||
mod = mod.Where("dept_id", in.DeptId)
|
||||
}
|
||||
|
||||
// 日期范围
|
||||
if in.StartTime != "" {
|
||||
mod = mod.WhereGTE("created_at", in.StartTime)
|
||||
}
|
||||
if in.EndTime != "" {
|
||||
mod = mod.WhereLTE("created_at", in.EndTime)
|
||||
}
|
||||
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
err = mod.Page(in.Page, in.Limit).Order("id desc").Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
// TODO 重写树入参
|
||||
for i := 0; i < len(list); i++ {
|
||||
// TODO 部门
|
||||
deptName, err := dao.AdminDept.Ctx(ctx).
|
||||
Fields("name").
|
||||
Where("id", list[i].DeptId).
|
||||
Value()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
list[i].DeptName = deptName.String()
|
||||
|
||||
// TODO 角色
|
||||
roleName, err := dao.AdminRole.Ctx(ctx).
|
||||
Fields("name").
|
||||
Where("id", list[i].Role).
|
||||
Value()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
list[i].RoleName = roleName.String()
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
// //
|
||||
// @Title 获取登录用户信息
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *member) LoginMemberInfo(ctx context.Context, req *adminForm.MemberInfoReq) (res *adminForm.MemberInfoRes, err error) {
|
||||
|
||||
var (
|
||||
defaultPortalConfig adminForm.PortalConfig
|
||||
defaultPortalConfigs []*adminForm.PortalConfig
|
||||
configContent adminForm.PortalConfigContent
|
||||
configContents []*adminForm.PortalConfigContent
|
||||
configContentOptions []*adminForm.PortalConfigContentOptions
|
||||
Options adminForm.PortalConfigContentOptions
|
||||
)
|
||||
|
||||
g.Log().Print(ctx, "测试")
|
||||
|
||||
// TODO 配置内容选项
|
||||
Options.TitleRequired = true
|
||||
Options.Refresh = 1
|
||||
configContentOptions = append(configContentOptions, &Options)
|
||||
|
||||
// TODO 配置内容
|
||||
configContent.Options = configContentOptions
|
||||
configContent.Id = 1
|
||||
configContent.X = 0
|
||||
configContent.Y = 0
|
||||
configContent.W = 3
|
||||
configContent.H = 262
|
||||
configContent.I = 1
|
||||
configContent.Key = "kuaijierukou"
|
||||
configContent.IsShowTitle = "N"
|
||||
configContent.IsAllowDrag = false
|
||||
configContent.Name = "快捷入口"
|
||||
configContent.Type = "smallPage"
|
||||
configContent.Url = "dashboard/portal/CommonUse"
|
||||
configContent.Moved = true
|
||||
|
||||
configContents = append(configContents, &configContent)
|
||||
|
||||
// TODO 默认配置
|
||||
defaultPortalConfig.Id = "4ae60dd1debe462096698e1da993317a"
|
||||
defaultPortalConfig.Name = "首页"
|
||||
defaultPortalConfig.Code = "6c297eb4651940edbb45c87c75be00d7"
|
||||
defaultPortalConfig.ApplicationRange = "U"
|
||||
defaultPortalConfig.IsDefault = "Y"
|
||||
defaultPortalConfig.ResourceId = "1"
|
||||
defaultPortalConfig.SystemDefinedId = "app1"
|
||||
defaultPortalConfig.PortalConfigContent = gconv.String(configContents)
|
||||
|
||||
defaultPortalConfigs = append(defaultPortalConfigs, &defaultPortalConfig)
|
||||
|
||||
member := com.Context.Get(ctx).User
|
||||
|
||||
noticeList, err := Notice.WhereAll(ctx, dto.AdminNotice{
|
||||
Status: consts.StatusEnabled,
|
||||
})
|
||||
if err != nil {
|
||||
noticeList = nil
|
||||
}
|
||||
|
||||
res = &adminForm.MemberInfoRes{
|
||||
LincenseInfo: consts.VersionApp,
|
||||
Permissions: []string{"*:*:*"},
|
||||
Roles: []string{"admin"},
|
||||
User: *member,
|
||||
DefaultPortalConfig: defaultPortalConfigs,
|
||||
UserPortalConfig: defaultPortalConfigs,
|
||||
SysNoticeList: noticeList,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 提交登录
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *member) Login(ctx context.Context, in input.AdminMemberLoginSignInp) (res *input.AdminMemberLoginSignModel, err error) {
|
||||
|
||||
var member *entity.AdminMember
|
||||
err = dao.AdminMember.Ctx(ctx).Where("username", in.Username).Scan(&member)
|
||||
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return
|
||||
}
|
||||
if member == nil {
|
||||
err = gerror.New(consts.ErrorNotData)
|
||||
return
|
||||
}
|
||||
|
||||
if member.Salt == "" {
|
||||
err = gerror.New("用户信息错误")
|
||||
return
|
||||
}
|
||||
|
||||
if member.PasswordHash != gmd5.MustEncryptString(in.Password+member.Salt) {
|
||||
err = gerror.New("用户密码不正确")
|
||||
return
|
||||
}
|
||||
|
||||
// 默认设备
|
||||
if in.Device != consts.AppAdmin && in.Device != consts.AppApi {
|
||||
in.Device = consts.AppAdmin
|
||||
}
|
||||
|
||||
// TODO 生成token
|
||||
jwtExpires, err := g.Cfg().Get(ctx, "jwt.expires", 1)
|
||||
if err != nil {
|
||||
err := gerror.New(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
// TODO 有效期
|
||||
expires := jwtExpires.Int64()
|
||||
|
||||
// TODO 过期时间戳
|
||||
exp := gconv.Int64(gtime.Timestamp()) + expires
|
||||
|
||||
var identity *model.Identity
|
||||
identity = &model.Identity{
|
||||
Id: member.Id,
|
||||
Username: member.Username,
|
||||
Realname: member.Realname,
|
||||
Avatar: member.Avatar,
|
||||
Email: member.Email,
|
||||
Mobile: member.Mobile,
|
||||
VisitCount: member.VisitCount,
|
||||
LastTime: member.LastTime,
|
||||
LastIp: member.LastIp,
|
||||
Role: member.Role,
|
||||
Exp: exp,
|
||||
Expires: expires,
|
||||
App: consts.AppAdmin,
|
||||
}
|
||||
token, err := com.Jwt.GenerateLoginToken(ctx, identity, false)
|
||||
if err != nil {
|
||||
err = gerror.New(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// TODO 更新登录信息
|
||||
authKey := gmd5.MustEncryptString(gconv.String(token))
|
||||
|
||||
_, err = dao.AdminMember.Ctx(ctx).Data(dto.AdminMember{
|
||||
AuthKey: gmd5.MustEncryptString(authKey),
|
||||
VisitCount: member.VisitCount + 1,
|
||||
LastTime: gtime.Timestamp(),
|
||||
LastIp: com.Context.Get(ctx).Request.GetClientIp(),
|
||||
}).Where(dto.AdminMember{
|
||||
Id: member.Id,
|
||||
}).Update()
|
||||
|
||||
if err != nil {
|
||||
err = gerror.New(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
res = &input.AdminMemberLoginSignModel{
|
||||
Identity: *identity,
|
||||
Token: gconv.String(token),
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取角色下的会员列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *member) RoleMemberList(ctx context.Context, in input.AdminRoleMemberListInp) (list []*input.AdminMemberListModel, totalCount int, err error) {
|
||||
|
||||
mod := dao.AdminMember.Ctx(ctx)
|
||||
if in.Role > 0 {
|
||||
mod = mod.Where("role", in.Role)
|
||||
}
|
||||
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
err = mod.Page(in.Page, in.Limit).Order("id desc").Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
533
hotgo-server/app/service/adminService/menu_service.go
Normal file
533
hotgo-server/app/service/adminService/menu_service.go
Normal file
@@ -0,0 +1,533 @@
|
||||
//
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package adminService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/bufanyun/hotgo/app/consts"
|
||||
"github.com/bufanyun/hotgo/app/form/adminForm"
|
||||
"github.com/bufanyun/hotgo/app/form/input"
|
||||
"github.com/bufanyun/hotgo/app/model"
|
||||
"github.com/bufanyun/hotgo/app/model/entity"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dao"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dto"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Menu = new(menu)
|
||||
|
||||
type menu struct{}
|
||||
|
||||
//
|
||||
// @Title 查询角色菜单列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.MenuSearchListRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) RoleList(ctx context.Context, in input.MenuRoleListInp) (*input.MenuRoleListModel, error) {
|
||||
|
||||
var (
|
||||
mod = dao.AdminRoleMenu.Ctx(ctx)
|
||||
roleMenu []*entity.AdminRoleMenu
|
||||
lst []*model.LabelTreeMenu
|
||||
res input.MenuRoleListModel
|
||||
err error
|
||||
checkedKeys []int64
|
||||
)
|
||||
|
||||
// TODO 获取选中菜单ID
|
||||
if in.RoleId > 0 {
|
||||
mod = mod.Where("role_id", in.RoleId)
|
||||
}
|
||||
err = mod.Fields().Scan(&roleMenu)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; i < len(roleMenu); i++ {
|
||||
checkedKeys = append(checkedKeys, roleMenu[i].MenuId)
|
||||
}
|
||||
res.CheckedKeys = checkedKeys
|
||||
|
||||
// TODO 获取菜单树
|
||||
lst, err = dao.AdminMenu.GenLabelTreeList(ctx, 0)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = gconv.Structs(lst, &res.Menus)
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 查询菜单列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.MenuSearchListRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) SearchList(ctx context.Context, req *adminForm.MenuSearchListReq) (*adminForm.MenuSearchListRes, error) {
|
||||
|
||||
var (
|
||||
mod = dao.AdminMenu.Ctx(ctx)
|
||||
lst []*model.TreeMenu
|
||||
res adminForm.MenuSearchListRes
|
||||
searchResult []*entity.AdminMenu
|
||||
id int64
|
||||
ids []int64
|
||||
err error
|
||||
)
|
||||
|
||||
if req.Name != "" {
|
||||
mod = mod.WhereLike("name", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
if req.Status > 0 {
|
||||
mod = mod.Where("status", req.Status)
|
||||
}
|
||||
|
||||
if req.Name != "" || req.Status > 0 {
|
||||
err = mod.Scan(&searchResult)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; i < len(searchResult); i++ {
|
||||
id, err = dao.AdminMenu.TopPid(ctx, searchResult[i])
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
|
||||
lst, err = dao.AdminMenu.GenTreeList(ctx, 0, ids)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = gconv.Structs(lst, &res)
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 最大排序
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictDataMaxSortRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) MaxSort(ctx context.Context, req *adminForm.MenuMaxSortReq) (*adminForm.MenuMaxSortRes, error) {
|
||||
var (
|
||||
res adminForm.MenuMaxSortRes
|
||||
err error
|
||||
)
|
||||
|
||||
if req.Id > 0 {
|
||||
if err = dao.AdminMenu.Ctx(ctx).Where("id", req.Id).Order("sort desc").Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
res.Sort = res.Sort + 10
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 菜单名称是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) NameUnique(ctx context.Context, req *adminForm.MenuNameUniqueReq) (*adminForm.MenuNameUniqueRes, error) {
|
||||
var (
|
||||
res adminForm.MenuNameUniqueRes
|
||||
err error
|
||||
)
|
||||
|
||||
res.IsUnique, err = dao.AdminMenu.IsUniqueName(ctx, req.Id, req.Name)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 菜单编码是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) CodeUnique(ctx context.Context, req *adminForm.MenuCodeUniqueReq) (*adminForm.MenuCodeUniqueRes, error) {
|
||||
var (
|
||||
res adminForm.MenuCodeUniqueRes
|
||||
err error
|
||||
)
|
||||
|
||||
res.IsUnique, err = dao.AdminMenu.IsUniqueCode(ctx, req.Id, req.Code)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 删除
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) Delete(ctx context.Context, req *adminForm.MenuDeleteReq) error {
|
||||
|
||||
exist, err := dao.AdminMenu.Ctx(ctx).Where("pid", req.Id).One()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !exist.IsEmpty() {
|
||||
return gerror.New("请先删除该菜单下的所有菜单!")
|
||||
}
|
||||
_, err = dao.AdminMenu.Ctx(ctx).Where("id", req.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 修改/新增
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) Edit(ctx context.Context, req *adminForm.MenuEditReq) (err error) {
|
||||
var (
|
||||
pidData *dto.AdminMenu
|
||||
uniqueName bool
|
||||
uniqueCode bool
|
||||
)
|
||||
|
||||
if req.Name == "" {
|
||||
err = gerror.New("菜单名称不能为空")
|
||||
return err
|
||||
}
|
||||
if req.Path == "" {
|
||||
err = gerror.New("菜单路径不能为空")
|
||||
return err
|
||||
}
|
||||
if req.Code == "" {
|
||||
err = gerror.New("菜单编码不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
uniqueName, err = dao.AdminMenu.IsUniqueName(ctx, req.Id, req.Name)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueName {
|
||||
err = gerror.New("菜单名称已存在")
|
||||
return err
|
||||
}
|
||||
|
||||
uniqueCode, err = dao.AdminMenu.IsUniqueCode(ctx, req.Id, req.Code)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueCode {
|
||||
err = gerror.New("菜单编码已存在")
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO 维护菜单等级
|
||||
if req.Pid == 0 {
|
||||
req.Level = 1
|
||||
} else {
|
||||
if err = dao.AdminMenu.Ctx(ctx).Where("id", req.Pid).Scan(&pidData); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if pidData == nil {
|
||||
return gerror.New("上级菜单信息错误")
|
||||
}
|
||||
req.Level = gconv.Int(pidData.Level) + 1
|
||||
}
|
||||
|
||||
// 修改
|
||||
req.UpdatedAt = gtime.Now()
|
||||
if req.Id > 0 {
|
||||
if req.Pid == req.Id {
|
||||
return gerror.New("上级菜单不能是当前菜单")
|
||||
}
|
||||
_, err = dao.AdminMenu.Ctx(ctx).Where("id", req.Id).Data(req).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
req.CreatedAt = gtime.Now()
|
||||
_, err = dao.AdminMenu.Ctx(ctx).Data(req).Insert()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取信息
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeViewRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *menu) View(ctx context.Context, req *adminForm.MenuViewReq) (res *adminForm.MenuViewRes, err error) {
|
||||
//var (
|
||||
// res adminForm.MenuViewRes
|
||||
//)
|
||||
|
||||
if err = dao.AdminMenu.Ctx(ctx).Where("id", req.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取菜单列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *menu) List(ctx context.Context, req *adminForm.MenuListReq) (*adminForm.MenuListRes, error) {
|
||||
var (
|
||||
m = dao.AdminMenu.Ctx(ctx)
|
||||
list []*entity.AdminMenu
|
||||
res adminForm.MenuListRes
|
||||
totalCount int
|
||||
err error
|
||||
)
|
||||
|
||||
if req.Pid == 0 {
|
||||
m = m.Where("level", 1)
|
||||
} else {
|
||||
m = m.Where("pid", req.Pid)
|
||||
}
|
||||
|
||||
totalCount, err = m.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = m.Page(req.Page, req.Limit).Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.List = list
|
||||
res.Page = req.Page
|
||||
res.Limit = req.Limit
|
||||
res.TotalCount = totalCount
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
type RelationTree struct {
|
||||
adminForm.RoleDynamicBase
|
||||
Children []*RelationTree `json:"children"`
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取菜单列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param member_id
|
||||
//
|
||||
func (service *menu) GetMenuList(ctx context.Context, member_id int64) (lists *adminForm.RoleDynamicRes, err error) {
|
||||
|
||||
var (
|
||||
results []*entity.AdminMenu
|
||||
models []*RelationTree
|
||||
recursion []*adminForm.RoleDynamicBase
|
||||
finalResponse adminForm.RoleDynamicRes
|
||||
)
|
||||
|
||||
err = dao.AdminMenu.Ctx(ctx).Order("sort asc,id desc").Scan(&results)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(results); i++ {
|
||||
|
||||
// 元数据
|
||||
var (
|
||||
meta adminForm.RoleDynamicMeta
|
||||
rec adminForm.RoleDynamicBase
|
||||
)
|
||||
|
||||
meta.Title = results[i].Name
|
||||
meta.Icon = results[i].Icon
|
||||
meta.NoCache = gconv.Bool(results[i].IsCache)
|
||||
meta.Remark = results[i].Remark
|
||||
|
||||
rec.Id = results[i].Id
|
||||
rec.Pid = results[i].Pid
|
||||
rec.IsFrame = results[i].IsFrame
|
||||
rec.Name = results[i].Name
|
||||
rec.Code = results[i].Code
|
||||
rec.Path = results[i].Path
|
||||
rec.Hidden = results[i].IsVisible == "1"
|
||||
rec.Redirect = service.getRedirect(results[i])
|
||||
rec.Component = service.getComponent(results[i])
|
||||
rec.AlwaysShow = true
|
||||
rec.Meta = &meta
|
||||
|
||||
recursion = append(recursion, &rec)
|
||||
}
|
||||
|
||||
_ = gconv.Structs(recursion, &models)
|
||||
|
||||
childIds := service.getChildIds(ctx, models, 0)
|
||||
|
||||
_ = gconv.Structs(childIds, &finalResponse)
|
||||
|
||||
return &finalResponse, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取菜单的组件配置
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param model
|
||||
// @Return string
|
||||
//
|
||||
func (service *menu) getComponent(mod *entity.AdminMenu) string {
|
||||
|
||||
if mod.Type == "M" {
|
||||
return "Layout"
|
||||
}
|
||||
|
||||
if mod.Type == "C" {
|
||||
return mod.Component
|
||||
}
|
||||
|
||||
return mod.Component
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取菜单是否重定向
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param model
|
||||
// @Return string
|
||||
//
|
||||
func (service *menu) getRedirect(model *entity.AdminMenu) string {
|
||||
if model.Type == "M" {
|
||||
return "noRedirect"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 将菜单转为父子关系菜单
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param lists
|
||||
// @Param pid
|
||||
// @Return []*RelationTree
|
||||
//
|
||||
func (service *menu) getChildIds(ctx context.Context, lists []*RelationTree, pid int64) []*RelationTree {
|
||||
|
||||
var (
|
||||
count = len(lists)
|
||||
newLists []*RelationTree
|
||||
)
|
||||
|
||||
if count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := 0; i < len(lists); i++ {
|
||||
if lists[i].Id > 0 && lists[i].Pid == pid {
|
||||
var row *RelationTree
|
||||
if err := gconv.Structs(lists[i], &row); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
row.Children = service.getChildIds(ctx, lists, row.Id)
|
||||
newLists = append(newLists, row)
|
||||
}
|
||||
}
|
||||
|
||||
return newLists
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 根据条件查询一行的数据
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param where
|
||||
// @Return *entity.AdminMenu
|
||||
//
|
||||
func (service *menu) WhereScan(ctx context.Context, where dto.AdminMenu) *entity.AdminMenu {
|
||||
var (
|
||||
mod *entity.AdminMenu
|
||||
err error
|
||||
)
|
||||
|
||||
if err = dao.AdminMenu.Ctx(ctx).Where(where).Scan(&mod); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil
|
||||
}
|
||||
|
||||
return mod
|
||||
}
|
||||
263
hotgo-server/app/service/adminService/notice_service.go
Normal file
263
hotgo-server/app/service/adminService/notice_service.go
Normal file
@@ -0,0 +1,263 @@
|
||||
//
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package adminService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/bufanyun/hotgo/app/consts"
|
||||
"github.com/bufanyun/hotgo/app/form/input"
|
||||
"github.com/bufanyun/hotgo/app/model/entity"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dao"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dto"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Notice = new(notice)
|
||||
|
||||
type notice struct{}
|
||||
|
||||
//
|
||||
// @Title 菜单名称是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *notice) NameUnique(ctx context.Context, in input.AdminNoticeNameUniqueInp) (*input.AdminNoticeNameUniqueModel, error) {
|
||||
|
||||
var res input.AdminNoticeNameUniqueModel
|
||||
isUnique, err := dao.AdminNotice.IsUniqueTitle(ctx, in.Id, in.Title)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.IsUnique = isUnique
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 删除
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *notice) Delete(ctx context.Context, in input.AdminNoticeDeleteInp) error {
|
||||
|
||||
_, err := dao.AdminNotice.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 修改/新增
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *notice) Edit(ctx context.Context, in input.AdminNoticeEditInp) (err error) {
|
||||
|
||||
if in.Title == "" {
|
||||
err = gerror.New("名称不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
uniqueName, err := dao.AdminNotice.IsUniqueTitle(ctx, in.Id, in.Title)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueName {
|
||||
err = gerror.New("名称已存在")
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改
|
||||
in.UpdatedAt = gtime.Now()
|
||||
if in.Id > 0 {
|
||||
_, err = dao.AdminNotice.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
in.CreatedAt = gtime.Now()
|
||||
_, err = dao.AdminNotice.Ctx(ctx).Data(in).Insert()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 最大排序
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictDataMaxSortRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *notice) MaxSort(ctx context.Context, in input.AdminNoticeMaxSortInp) (*input.AdminNoticeMaxSortModel, error) {
|
||||
var res input.AdminNoticeMaxSortModel
|
||||
|
||||
if in.Id > 0 {
|
||||
if err := dao.AdminNotice.Ctx(ctx).Where("id", in.Id).Order("sort desc").Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
res.Sort = res.Sort + 10
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定字典类型信息
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeViewRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *notice) View(ctx context.Context, in input.AdminNoticeViewInp) (res *input.AdminNoticeViewModel, err error) {
|
||||
|
||||
if err = dao.AdminNotice.Ctx(ctx).Where("id", in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *notice) List(ctx context.Context, in input.AdminNoticeListInp) (list []*input.AdminNoticeListModel, totalCount int, err error) {
|
||||
|
||||
mod := dao.AdminNotice.Ctx(ctx)
|
||||
|
||||
if in.Realname != "" {
|
||||
mod = mod.WhereLike("realname", "%"+in.Realname+"%")
|
||||
}
|
||||
if in.Username != "" {
|
||||
mod = mod.WhereLike("username", "%"+in.Username+"%")
|
||||
}
|
||||
if in.Mobile > 0 {
|
||||
mod = mod.Where("mobile", in.Mobile)
|
||||
}
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where("status", in.Status)
|
||||
}
|
||||
if in.DeptId > 0 {
|
||||
mod = mod.Where("dept_id", in.DeptId)
|
||||
}
|
||||
|
||||
// 日期范围
|
||||
if in.StartTime != "" {
|
||||
mod = mod.WhereGTE("created_at", in.StartTime)
|
||||
}
|
||||
if in.EndTime != "" {
|
||||
mod = mod.WhereLTE("created_at", in.EndTime)
|
||||
}
|
||||
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
err = mod.Page(in.Page, in.Limit).Order("id desc").Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
//// TODO 重写树入参
|
||||
//for i := 0; i < len(list); i++ {
|
||||
//}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 根据条件查询所有数据
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param where
|
||||
// @Return []*entity.AdminNotice
|
||||
// @Return error
|
||||
//
|
||||
func (service *notice) WhereAll(ctx context.Context, where dto.AdminNotice) ([]*entity.AdminNotice, error) {
|
||||
var (
|
||||
model []*entity.AdminNotice
|
||||
err error
|
||||
result gdb.Result
|
||||
)
|
||||
result, err = dao.AdminNotice.Ctx(ctx).Where(where).All()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = gconv.Scan(result, &model)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorRotaPointer)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return model, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 根据条件查询一行的数据
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param where
|
||||
// @Return *entity.AdminMenu
|
||||
//
|
||||
func (service *notice) WhereScan(ctx context.Context, where dto.AdminNotice) *entity.AdminNotice {
|
||||
var (
|
||||
model *entity.AdminNotice
|
||||
err error
|
||||
)
|
||||
|
||||
if err = dao.AdminMenu.Ctx(ctx).Where(where).Scan(&model); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil
|
||||
}
|
||||
|
||||
return model
|
||||
}
|
||||
274
hotgo-server/app/service/adminService/post_service.go
Normal file
274
hotgo-server/app/service/adminService/post_service.go
Normal file
@@ -0,0 +1,274 @@
|
||||
//
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package adminService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/bufanyun/hotgo/app/consts"
|
||||
"github.com/bufanyun/hotgo/app/form/input"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dao"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
var Post = new(post)
|
||||
|
||||
type post struct{}
|
||||
|
||||
//
|
||||
// @Title 删除
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *post) Delete(ctx context.Context, in input.AdminPostDeleteInp) error {
|
||||
|
||||
exist, err := dao.AdminMemberPost.Ctx(ctx).Where("post_id", in.Id).One()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !exist.IsEmpty() {
|
||||
return gerror.New("请先解除该岗位下所有已关联用户关联关系!")
|
||||
}
|
||||
_, err = dao.AdminPost.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 修改/新增
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return error
|
||||
//
|
||||
func (service *post) Edit(ctx context.Context, in input.AdminPostEditInp) (err error) {
|
||||
|
||||
if in.Name == "" {
|
||||
err = gerror.New("名称不能为空")
|
||||
return err
|
||||
}
|
||||
if in.Code == "" {
|
||||
err = gerror.New("编码不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
uniqueName, err := dao.AdminPost.IsUniqueName(ctx, in.Id, in.Name)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueName {
|
||||
err = gerror.New("名称已存在")
|
||||
return err
|
||||
}
|
||||
|
||||
uniqueCode, err := dao.AdminPost.IsUniqueCode(ctx, in.Id, in.Code)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !uniqueCode {
|
||||
err = gerror.New("编码已存在")
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改
|
||||
in.UpdatedAt = gtime.Now()
|
||||
if in.Id > 0 {
|
||||
_, err = dao.AdminPost.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
in.CreatedAt = gtime.Now()
|
||||
_, err = dao.AdminPost.Ctx(ctx).Data(in).Insert()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 最大排序
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictDataMaxSortRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *post) MaxSort(ctx context.Context, in input.AdminPostMaxSortInp) (*input.AdminPostMaxSortModel, error) {
|
||||
var res input.AdminPostMaxSortModel
|
||||
|
||||
if in.Id > 0 {
|
||||
if err := dao.AdminMenu.Ctx(ctx).Where("id", in.Id).Order("sort desc").Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
res.Sort = res.Sort + 10
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 菜单名称是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *post) NameUnique(ctx context.Context, in input.AdminPostNameUniqueInp) (*input.AdminPostNameUniqueModel, error) {
|
||||
|
||||
var res input.AdminPostNameUniqueModel
|
||||
isUnique, err := dao.AdminPost.IsUniqueName(ctx, in.Id, in.Name)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.IsUnique = isUnique
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 编码是否唯一
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeUniqueRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *post) CodeUnique(ctx context.Context, in input.AdminPostCodeUniqueInp) (*input.AdminPostCodeUniqueModel, error) {
|
||||
|
||||
var res input.AdminPostCodeUniqueModel
|
||||
isUnique, err := dao.AdminPost.IsUniqueCode(ctx, in.Id, in.Code)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.IsUnique = isUnique
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定字典类型信息
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return *adminForm.DictTypeViewRes
|
||||
// @Return error
|
||||
//
|
||||
func (service *post) View(ctx context.Context, in input.AdminPostViewInp) (res *input.AdminPostViewModel, err error) {
|
||||
|
||||
if err = dao.AdminPost.Ctx(ctx).Where("id", in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *post) List(ctx context.Context, in input.AdminPostListInp) (list []*input.AdminPostListModel, totalCount int, err error) {
|
||||
|
||||
mod := dao.AdminPost.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
if in.Name != "" {
|
||||
mod = mod.WhereLike("name", "%"+in.Name+"%")
|
||||
}
|
||||
|
||||
// 模块
|
||||
if in.Code != "" {
|
||||
mod = mod.Where("code", in.Code)
|
||||
}
|
||||
|
||||
// 请求方式
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where("status", in.Status)
|
||||
}
|
||||
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
err = mod.Page(in.Page, in.Limit).Order("id desc").Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定用户的第一岗位
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param RoleId
|
||||
// @Return name
|
||||
// @Return err
|
||||
//
|
||||
func (service *post) GetMemberByStartName(ctx context.Context, memberId int64) (name string, err error) {
|
||||
|
||||
// TODO 默认取第一岗位
|
||||
postId, err := dao.AdminMemberPost.Ctx(ctx).
|
||||
Fields("post_id").
|
||||
Where("member_id", memberId).
|
||||
Limit(1).
|
||||
Value()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return name, err
|
||||
}
|
||||
|
||||
val, err := dao.AdminPost.Ctx(ctx).
|
||||
Fields("name").
|
||||
Where("id", postId.Int()).
|
||||
Order("id desc").
|
||||
Value()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return name, err
|
||||
}
|
||||
|
||||
return val.String(), nil
|
||||
}
|
||||
130
hotgo-server/app/service/adminService/role_service.go
Normal file
130
hotgo-server/app/service/adminService/role_service.go
Normal file
@@ -0,0 +1,130 @@
|
||||
//
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package adminService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/bufanyun/hotgo/app/consts"
|
||||
"github.com/bufanyun/hotgo/app/form/input"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dao"
|
||||
"github.com/bufanyun/hotgo/app/service/internal/dto"
|
||||
"github.com/bufanyun/hotgo/app/utils"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
var Role = new(role)
|
||||
|
||||
type role struct{}
|
||||
|
||||
//
|
||||
// @Title 验证权限
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param member_id
|
||||
// @Param path
|
||||
// @Return bool
|
||||
//
|
||||
func (service *role) Verify(ctx context.Context, member_id int, path string) bool {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
||||
if utils.Auth.IsExceptAuth(ctx, path) {
|
||||
return true
|
||||
}
|
||||
|
||||
menu := Menu.WhereScan(ctx, dto.AdminMenu{
|
||||
Path: path,
|
||||
Status: consts.StatusEnabled,
|
||||
})
|
||||
|
||||
if menu == nil {
|
||||
err = gerror.New(consts.ErrorNotData)
|
||||
return false
|
||||
}
|
||||
|
||||
g.Log().Print(ctx, "menu:", menu)
|
||||
g.Log().Print(ctx, "err:", err)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *role) List(ctx context.Context, in input.AdminRoleListInp) (list []*input.AdminRoleListModel, totalCount int, err error) {
|
||||
|
||||
mod := dao.AdminRole.Ctx(ctx)
|
||||
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
err = mod.Page(in.Page, in.Limit).Order("id asc").Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定角色的名称
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param RoleId
|
||||
// @Return name
|
||||
// @Return err
|
||||
//
|
||||
func (service *role) GetName(ctx context.Context, RoleId int64) (name string, err error) {
|
||||
roleName, err := dao.AdminRole.Ctx(ctx).
|
||||
Fields("name").
|
||||
Where("id", RoleId).
|
||||
Order("id desc").
|
||||
Value()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return name, err
|
||||
}
|
||||
|
||||
return roleName.String(), nil
|
||||
}
|
||||
|
||||
//
|
||||
// @Title 获取指定会员的岗位列表
|
||||
// @Description
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @Param ctx
|
||||
// @Param req
|
||||
// @Return res
|
||||
// @Return err
|
||||
//
|
||||
func (service *role) GetMemberList(ctx context.Context, RoleId int64) (list []*input.AdminRoleListModel, err error) {
|
||||
|
||||
err = dao.AdminRole.Ctx(ctx).
|
||||
Where("id", RoleId).
|
||||
Order("id desc").
|
||||
Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, err
|
||||
}
|
||||
|
||||
return list, err
|
||||
}
|
||||
Reference in New Issue
Block a user