新增
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/api/requests"
|
||||
"hospital-admin-api/global"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ApiService struct {
|
||||
}
|
||||
|
||||
// AddApi 新增接口
|
||||
func (a *ApiService) AddApi(c *gin.Context, addApiRequest requests.AddApi) (bool, error) {
|
||||
loginRoleId := c.GetInt64("RoleId")
|
||||
if loginRoleId == 0 {
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
// 获取当前登陆接口角色数据
|
||||
adminRoleDao := dao.AdminRoleDao{}
|
||||
adminRole, err := adminRoleDao.GetAdminRoleFirstById(loginRoleId)
|
||||
if err != nil || adminRole == nil {
|
||||
return false, errors.New("非法操作")
|
||||
}
|
||||
|
||||
if adminRole.IsAdmin == 0 {
|
||||
return false, errors.New("您当前为普通用户,无法添加接口")
|
||||
}
|
||||
|
||||
adminApiDao := dao.AdminApiDao{}
|
||||
|
||||
// 检测接口名称+请求方式
|
||||
maps := make(map[string]interface{})
|
||||
maps["api_path"] = addApiRequest.ApiPath
|
||||
maps["api_method"] = addApiRequest.ApiMethod
|
||||
adminApis, err := adminApiDao.GetAdminApiList(maps)
|
||||
if len(adminApis) > 0 || err != nil {
|
||||
return false, errors.New("接口重复")
|
||||
}
|
||||
|
||||
// 检测接口路径
|
||||
pattern := `^/[\w/]+(\:\w+)?$`
|
||||
r := regexp.MustCompile(pattern)
|
||||
|
||||
if !r.MatchString(addApiRequest.ApiPath) {
|
||||
return false, errors.New("接口路径错误")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 新增接口
|
||||
AdminApiModel := &model.AdminAPI{
|
||||
APIName: addApiRequest.ApiName,
|
||||
APIPath: addApiRequest.ApiPath,
|
||||
APIMethod: addApiRequest.ApiMethod,
|
||||
IsAuth: 1,
|
||||
}
|
||||
|
||||
adminApi, _ := adminApiDao.AddAdminApi(tx, AdminApiModel)
|
||||
if adminApi == nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// DeleteApi 删除接口-批量
|
||||
func (r *ApiService) DeleteApi(c *gin.Context, deleteApiRequest requests.DeleteApi) (bool, error) {
|
||||
adminApiDao := dao.AdminApiDao{}
|
||||
|
||||
// 获取当前登陆接口角色数据
|
||||
roleId := c.GetInt64("RoleId")
|
||||
if roleId == 0 {
|
||||
return false, errors.New("数据错误")
|
||||
}
|
||||
|
||||
AdminRoleDao := dao.AdminRoleDao{}
|
||||
adminRole, err := AdminRoleDao.GetAdminRoleFirstById(roleId)
|
||||
if err != nil || adminRole == nil {
|
||||
return false, errors.New("非法操作")
|
||||
}
|
||||
|
||||
if adminRole.IsAdmin == 0 {
|
||||
return false, errors.New("暂无权限,请联系管理员删除")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
for _, v := range deleteApiRequest.ApiIds {
|
||||
// 将字符串转换为int64类型
|
||||
v, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return false, errors.New("菜单错误")
|
||||
}
|
||||
|
||||
// 检测删除接口
|
||||
adminApi, err := adminApiDao.GetAdminApiById(v)
|
||||
if err != nil || adminApi == nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("删除失败")
|
||||
}
|
||||
|
||||
// 删除接口
|
||||
err = adminApiDao.DeleteAdminApiById(tx, v)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("删除失败")
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutApi 修改接口
|
||||
func (r *ApiService) PutApi(c *gin.Context, requestApiId int64, putApiRequest requests.PutApi) (bool, error) {
|
||||
adminApiDao := dao.AdminApiDao{}
|
||||
|
||||
// 获取需修改接口数据
|
||||
adminApi, err := adminApiDao.GetAdminApiById(requestApiId)
|
||||
if err != nil || adminApi == nil {
|
||||
return false, errors.New("接口数据错误")
|
||||
}
|
||||
|
||||
// 获取当前登陆接口角色数据
|
||||
loginRoleId := c.GetInt64("RoleId")
|
||||
if loginRoleId == 0 {
|
||||
return false, errors.New("数据错误")
|
||||
}
|
||||
|
||||
// 获取当前登陆接口角色数据
|
||||
adminRoleDao := dao.AdminRoleDao{}
|
||||
adminRole, err := adminRoleDao.GetAdminRoleFirstById(loginRoleId)
|
||||
if err != nil || adminRole == nil {
|
||||
return false, errors.New("非法操作")
|
||||
}
|
||||
|
||||
if adminRole.IsAdmin == 0 {
|
||||
return false, errors.New("您当前为普通用户,无法修改接口")
|
||||
}
|
||||
|
||||
if putApiRequest.ApiPath != adminApi.APIPath && putApiRequest.ApiMethod != adminApi.APIMethod {
|
||||
// 检测接口名称+请求方式
|
||||
maps := make(map[string]interface{})
|
||||
maps["api_path"] = putApiRequest.ApiPath
|
||||
maps["api_method"] = putApiRequest.ApiMethod
|
||||
adminApis, err := adminApiDao.GetAdminApiList(maps)
|
||||
if len(adminApis) > 0 || err != nil {
|
||||
return false, errors.New("接口重复")
|
||||
}
|
||||
}
|
||||
|
||||
if putApiRequest.ApiPath != adminApi.APIPath {
|
||||
// 检测接口路径
|
||||
pattern := `^/[\w/]+(\:\w+)?$`
|
||||
r := regexp.MustCompile(pattern)
|
||||
|
||||
if !r.MatchString(putApiRequest.ApiPath) {
|
||||
return false, errors.New("接口路径错误")
|
||||
}
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 修改角色
|
||||
data := make(map[string]interface{})
|
||||
data["api_name"] = putApiRequest.ApiName
|
||||
data["api_path"] = putApiRequest.ApiPath
|
||||
data["api_method"] = putApiRequest.ApiMethod
|
||||
err = adminApiDao.EditAdminApiById(tx, requestApiId, data)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("修改失败")
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return true, nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/responses/deptResponse"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DeptService struct {
|
||||
}
|
||||
|
||||
// GetDeptList 获取部门列表
|
||||
func (r *DeptService) GetDeptList() ([]*deptResponse.GetDeptList, error) {
|
||||
// 获取全部部门
|
||||
adminDeptDao := dao.AdminDeptDao{}
|
||||
adminDept, _ := adminDeptDao.GetAdminDeptList()
|
||||
if adminDept == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
deptMap := make(map[int64]*deptResponse.GetDeptList)
|
||||
var deptTree []*deptResponse.GetDeptList
|
||||
|
||||
for _, dept := range adminDept {
|
||||
node := &deptResponse.GetDeptList{
|
||||
DeptId: strconv.FormatInt(dept.DeptId, 10),
|
||||
ParentId: strconv.FormatInt(dept.ParentId, 10),
|
||||
DeptName: dept.DeptName,
|
||||
DeptStatus: dept.DeptStatus,
|
||||
CreatedAt: dept.CreatedAt,
|
||||
UpdatedAt: dept.UpdatedAt,
|
||||
Children: nil,
|
||||
}
|
||||
deptMap[dept.DeptId] = node
|
||||
}
|
||||
|
||||
// 构建菜单树
|
||||
for _, dept := range adminDept {
|
||||
if dept.ParentId == 0 {
|
||||
if deptMap[dept.DeptId] == nil {
|
||||
continue
|
||||
}
|
||||
deptTree = append(deptTree, deptMap[dept.DeptId])
|
||||
} else if parent, ok := deptMap[dept.ParentId]; ok {
|
||||
parent.Children = append(parent.Children, deptMap[dept.DeptId])
|
||||
}
|
||||
}
|
||||
|
||||
return deptTree, nil
|
||||
}
|
||||
@@ -80,6 +80,9 @@ func buildMenuTree(menuIds []int64, menuData []*model.AdminMenu) []*roleResponse
|
||||
|
||||
// 构建菜单树
|
||||
for _, menu := range menuData {
|
||||
if menuMap[menu.MenuId] == nil {
|
||||
continue
|
||||
}
|
||||
if menu.ParentId == 0 {
|
||||
if menuMap[menu.MenuId] == nil {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user