新增
This commit is contained in:
parent
a7ca868a5f
commit
c0c69eb602
170
api/controller/api.go
Normal file
170
api/controller/api.go
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"hospital-admin-api/api/dao"
|
||||||
|
"hospital-admin-api/api/requests"
|
||||||
|
"hospital-admin-api/api/responses"
|
||||||
|
"hospital-admin-api/api/responses/apiResponse"
|
||||||
|
"hospital-admin-api/api/service"
|
||||||
|
"hospital-admin-api/global"
|
||||||
|
"hospital-admin-api/utils"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AdminApi struct{}
|
||||||
|
|
||||||
|
// GetApiPage 获取api列表-分页
|
||||||
|
func (r *AdminApi) GetApiPage(c *gin.Context) {
|
||||||
|
apiRequest := requests.ApiRequest{}
|
||||||
|
if err := c.ShouldBind(&apiRequest.GetApiPage); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(apiRequest.GetApiPage); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if apiRequest.GetApiPage.Page == 0 {
|
||||||
|
apiRequest.GetApiPage.Page = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if apiRequest.GetApiPage.PageSize == 0 {
|
||||||
|
apiRequest.GetApiPage.PageSize = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
adminApiDao := dao.AdminApiDao{}
|
||||||
|
adminApi, total, err := adminApiDao.GetAdminApiPageSearch(apiRequest.GetApiPage, apiRequest.GetApiPage.Page, apiRequest.GetApiPage.PageSize)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理返回值
|
||||||
|
getApiPageResponses := apiResponse.GetApiPageResponse(adminApi)
|
||||||
|
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
result["page"] = apiRequest.GetApiPage.Page
|
||||||
|
result["page_size"] = apiRequest.GetApiPage.PageSize
|
||||||
|
result["total"] = total
|
||||||
|
result["data"] = getApiPageResponses
|
||||||
|
responses.OkWithData(result, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddApi 新增接口
|
||||||
|
func (r *AdminApi) AddApi(c *gin.Context) {
|
||||||
|
apiRequest := requests.ApiRequest{}
|
||||||
|
if err := c.ShouldBind(&apiRequest.AddApi); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(apiRequest.AddApi); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
ApiService := service.ApiService{}
|
||||||
|
_, err := ApiService.AddApi(c, apiRequest.AddApi)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetApi 接口详情
|
||||||
|
func (r *AdminApi) GetApi(c *gin.Context) {
|
||||||
|
id := c.Param("api_id")
|
||||||
|
if id == "" {
|
||||||
|
responses.FailWithMessage("缺少参数", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 id 转换为 int64 类型
|
||||||
|
apiId, err := strconv.ParseInt(id, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
responses.Fail(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取接口详情
|
||||||
|
adminApiDao := dao.AdminApiDao{}
|
||||||
|
adminApi, err := adminApiDao.GetAdminApiById(apiId)
|
||||||
|
if err != nil || adminApi == nil {
|
||||||
|
responses.FailWithMessage("接口数据错误", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理返回值
|
||||||
|
getApiResponses := apiResponse.GetApiResponse(adminApi)
|
||||||
|
|
||||||
|
responses.OkWithData(getApiResponses, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteApi 删除接口-批量
|
||||||
|
func (r *AdminApi) DeleteApi(c *gin.Context) {
|
||||||
|
apiRequest := requests.ApiRequest{}
|
||||||
|
if err := c.ShouldBindJSON(&apiRequest.DeleteApi); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(apiRequest.DeleteApi); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
apiService := service.ApiService{}
|
||||||
|
_, err := apiService.DeleteApi(c, apiRequest.DeleteApi)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutApi 修改接口
|
||||||
|
func (r *AdminApi) PutApi(c *gin.Context) {
|
||||||
|
apiRequest := requests.ApiRequest{}
|
||||||
|
if err := c.ShouldBind(&apiRequest.PutApi); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(apiRequest.PutApi); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := c.Param("api_id")
|
||||||
|
if id == "" {
|
||||||
|
responses.FailWithMessage("缺少参数", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 id 转换为 int64 类型
|
||||||
|
ApiId, err := strconv.ParseInt(id, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
responses.Fail(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
apiService := service.ApiService{}
|
||||||
|
_, err = apiService.PutApi(c, ApiId, apiRequest.PutApi)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
@ -2,8 +2,10 @@ package controller
|
|||||||
|
|
||||||
// Api api接口
|
// Api api接口
|
||||||
type Api struct {
|
type Api struct {
|
||||||
Basic // 基础数据
|
Basic // 基础数据
|
||||||
Role // 角色数据
|
Role // 角色数据
|
||||||
Menu // 菜单数据
|
Menu // 菜单数据
|
||||||
User // 用户数据
|
User // 用户数据
|
||||||
|
AdminApi // 接口数据
|
||||||
|
Dept // 部门数据
|
||||||
}
|
}
|
||||||
|
|||||||
142
api/controller/dept.go
Normal file
142
api/controller/dept.go
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"hospital-admin-api/api/responses"
|
||||||
|
"hospital-admin-api/api/service"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Dept struct{}
|
||||||
|
|
||||||
|
// GetDeptList 获取部门列表
|
||||||
|
func (r *Dept) GetDeptList(c *gin.Context) {
|
||||||
|
deptRequest := service.DeptService{}
|
||||||
|
deptList, err := deptRequest.GetDeptList()
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if deptList == nil {
|
||||||
|
responses.Ok(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.OkWithData(deptList, c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// // AddDept 新增部门
|
||||||
|
// func (r *Dept) AddDept(c *gin.Context) {
|
||||||
|
// deptRequest := requests.DeptRequest{}
|
||||||
|
// if err := c.ShouldBind(&deptRequest.AddDept); err != nil {
|
||||||
|
// responses.FailWithMessage(err.Error(), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 参数验证
|
||||||
|
// if err := global.Validate.Struct(deptRequest.AddDept); err != nil {
|
||||||
|
// responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 业务处理
|
||||||
|
// deptService := service.DeptService{}
|
||||||
|
// _, err := deptService.AddDept(c, deptRequest.AddDept)
|
||||||
|
// if err != nil {
|
||||||
|
// responses.FailWithMessage(err.Error(), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// responses.Ok(c)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // GetDept 部门详情
|
||||||
|
// func (r *Dept) GetDept(c *gin.Context) {
|
||||||
|
// id := c.Param("dept_id")
|
||||||
|
// if id == "" {
|
||||||
|
// responses.FailWithMessage("缺少参数", c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 将 id 转换为 int64 类型
|
||||||
|
// deptId, err := strconv.ParseInt(id, 10, 64)
|
||||||
|
// if err != nil {
|
||||||
|
// responses.Fail(c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 获取部门详情
|
||||||
|
// adminDeptDao := dao.AdminDeptDao{}
|
||||||
|
// adminDept, err := adminDeptDao.GetAdminDeptById(deptId)
|
||||||
|
// if err != nil || adminDept == nil {
|
||||||
|
// responses.FailWithMessage("部门数据错误", c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 处理返回值
|
||||||
|
// getDeptResponses := apiResponse.GetDeptResponse(adminDept)
|
||||||
|
//
|
||||||
|
// responses.OkWithData(getDeptResponses, c)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // DeleteDept 删除部门-批量
|
||||||
|
// func (r *Dept) DeleteDept(c *gin.Context) {
|
||||||
|
// deptRequest := requests.DeptRequest{}
|
||||||
|
// if err := c.ShouldBindJSON(&deptRequest.DeleteDept); err != nil {
|
||||||
|
// responses.FailWithMessage(err.Error(), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 参数验证
|
||||||
|
// if err := global.Validate.Struct(deptRequest.DeleteDept); err != nil {
|
||||||
|
// responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 业务处理
|
||||||
|
// apiService := service.DeptService{}
|
||||||
|
// _, err := apiService.DeleteDept(c, deptRequest.DeleteDept)
|
||||||
|
// if err != nil {
|
||||||
|
// responses.FailWithMessage(err.Error(), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// responses.Ok(c)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // PutDept 修改部门
|
||||||
|
// func (r *Dept) PutDept(c *gin.Context) {
|
||||||
|
// deptRequest := requests.DeptRequest{}
|
||||||
|
// if err := c.ShouldBind(&deptRequest.PutDept); err != nil {
|
||||||
|
// responses.FailWithMessage(err.Error(), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 参数验证
|
||||||
|
// if err := global.Validate.Struct(deptRequest.PutDept); err != nil {
|
||||||
|
// responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// id := c.Param("dept_id")
|
||||||
|
// if id == "" {
|
||||||
|
// responses.FailWithMessage("缺少参数", c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 将 id 转换为 int64 类型
|
||||||
|
// deptId, err := strconv.ParseInt(id, 10, 64)
|
||||||
|
// if err != nil {
|
||||||
|
// responses.Fail(c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 业务处理
|
||||||
|
// apiService := service.DeptService{}
|
||||||
|
// _, err = apiService.PutDept(c, deptId, deptRequest.PutDept)
|
||||||
|
// if err != nil {
|
||||||
|
// responses.FailWithMessage(err.Error(), c)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// responses.Ok(c)
|
||||||
|
// }
|
||||||
@ -1,14 +1,16 @@
|
|||||||
package dao
|
package dao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/api/model"
|
"hospital-admin-api/api/model"
|
||||||
|
"hospital-admin-api/api/requests"
|
||||||
"hospital-admin-api/global"
|
"hospital-admin-api/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AdminApiDao struct {
|
type AdminApiDao struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAdminApiById 获取菜单数据-菜单id
|
// GetAdminApiById 获取接口数据-接口id
|
||||||
func (r *AdminApiDao) GetAdminApiById(apiId int64) (m *model.AdminAPI, err error) {
|
func (r *AdminApiDao) GetAdminApiById(apiId int64) (m *model.AdminAPI, err error) {
|
||||||
err = global.Db.First(&m, apiId).Error
|
err = global.Db.First(&m, apiId).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -16,3 +18,68 @@ func (r *AdminApiDao) GetAdminApiById(apiId int64) (m *model.AdminAPI, err error
|
|||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAdminApiPageSearch 获取接口列表-分页
|
||||||
|
func (r *AdminApiDao) GetAdminApiPageSearch(getApiPage requests.GetApiPage, page, pageSize int) (m []*model.AdminAPI, total int64, err error) {
|
||||||
|
var totalRecords int64
|
||||||
|
|
||||||
|
// 构建查询条件
|
||||||
|
query := global.Db.Model(&model.AdminAPI{})
|
||||||
|
|
||||||
|
if getApiPage.ApiName != "" {
|
||||||
|
query = query.Where("api_name like ?", "%"+getApiPage.ApiName+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
if getApiPage.ApiMethod != "" {
|
||||||
|
query = query.Where("api_method like ?", "%"+getApiPage.ApiMethod+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
if getApiPage.ApiPath != "" {
|
||||||
|
query = query.Where("api_path like ?", "%"+getApiPage.ApiPath+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询总数量
|
||||||
|
if err := query.Count(&totalRecords).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
return m, totalRecords, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddAdminApi 新增接口
|
||||||
|
func (r *AdminApiDao) AddAdminApi(tx *gorm.DB, model *model.AdminAPI) (*model.AdminAPI, error) {
|
||||||
|
if err := tx.Create(model).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return model, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAdminApiList 获取接口列表
|
||||||
|
func (r *AdminApiDao) GetAdminApiList(maps interface{}) (m []*model.AdminAPI, err error) {
|
||||||
|
err = global.Db.Where(maps).Find(&m).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteAdminApiById 删除接口-接口id
|
||||||
|
func (r *AdminApiDao) DeleteAdminApiById(tx *gorm.DB, apiId int64) error {
|
||||||
|
if err := tx.Delete(&model.AdminAPI{}, apiId).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EditAdminApiById 修改接口-接口id
|
||||||
|
func (r *AdminApiDao) EditAdminApiById(tx *gorm.DB, apiId int64, data interface{}) error {
|
||||||
|
err := tx.Model(&model.AdminAPI{}).Where("api_id = ?", apiId).Updates(data).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@ -16,3 +16,12 @@ func (r *AdminDeptDao) GetAdminDeptFirstById(deptId int64) (m *model.AdminDept,
|
|||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAdminDeptList 获取部门列表
|
||||||
|
func (r *AdminDeptDao) GetAdminDeptList() (m []*model.AdminDept, err error) {
|
||||||
|
err = global.Db.Find(&m).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|||||||
@ -1,15 +1,12 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
// AdminDept 后台-部门表
|
// AdminDept 后台-部门表
|
||||||
type AdminDept struct {
|
type AdminDept struct {
|
||||||
DeptId int64 `gorm:"column:dept_id;type:bigint(19);primary_key;comment:主键id" json:"dept_id"`
|
DeptId int64 `gorm:"column:dept_id;type:bigint(19);primary_key;comment:主键id" json:"dept_id"`
|
||||||
ParentId int64 `gorm:"column:parent_id;type:bigint(19);comment:本表父级id" json:"parent_id"`
|
ParentId int64 `gorm:"column:parent_id;type:bigint(19);comment:本表父级id" json:"parent_id"`
|
||||||
DeptName string `gorm:"column:dept_name;type:varchar(255);comment:部门名称" json:"dept_name"`
|
DeptName string `gorm:"column:dept_name;type:varchar(255);comment:部门名称" json:"dept_name"`
|
||||||
DeptStatus int `gorm:"column:dept_status;type:tinyint(1);default:1;comment:部门状态(1:正常 2:删除)" json:"dept_status"`
|
DeptStatus int `gorm:"column:dept_status;type:tinyint(1);default:1;comment:部门状态(1:正常 2:删除)" json:"dept_status"`
|
||||||
CreatedAt time.Time `gorm:"column:created_at;type:datetime;comment:创建时间" json:"created_at"`
|
Model
|
||||||
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;comment:修改时间" json:"updated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AdminDept) TableName() string {
|
func (m *AdminDept) TableName() string {
|
||||||
|
|||||||
36
api/requests/api.go
Normal file
36
api/requests/api.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package requests
|
||||||
|
|
||||||
|
type ApiRequest struct {
|
||||||
|
GetApiPage // 获取接口列表-分页
|
||||||
|
AddApi // 新增接口
|
||||||
|
DeleteApi // 删除接口-批量
|
||||||
|
PutApi // 修改接口
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetApiPage 获取接口列表-分页
|
||||||
|
type GetApiPage struct {
|
||||||
|
ApiName string `json:"api_name" form:"api_name" label:"api名称"`
|
||||||
|
ApiPath string `json:"api_path" form:"api_path" label:"接口路径"`
|
||||||
|
ApiMethod string `json:"api_method" form:"api_method" label:"请求类型"` // (put:修改 post:新增 get:获取 delete:删除)
|
||||||
|
Page int `json:"page" form:"page" label:"页码"`
|
||||||
|
PageSize int `json:"page_size" form:"page_size" label:"每页个数"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddApi 新增接口
|
||||||
|
type AddApi struct {
|
||||||
|
ApiName string `json:"api_name" form:"api_name" validate:"required" label:"接口名称"`
|
||||||
|
ApiPath string `json:"api_path" form:"api_path" validate:"required" label:"接口路径"`
|
||||||
|
ApiMethod string `json:"api_method" form:"api_method" validate:"required,oneof=GET POST PUT DELETE" label:"请求类型"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteApi 删除接口-批量
|
||||||
|
type DeleteApi struct {
|
||||||
|
ApiIds []string `json:"api_ids" form:"api_ids" validate:"required" label:"接口id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutApi 修改接口
|
||||||
|
type PutApi struct {
|
||||||
|
ApiName string `json:"api_name" form:"api_name" label:"api名称"`
|
||||||
|
ApiMethod string `json:"api_method" form:"api_method" label:"请求类型"` // (put:修改 post:新增 get:获取 delete:删除)
|
||||||
|
ApiPath string `json:"api_path" form:"api_path" validate:"required" label:"接口路径"`
|
||||||
|
}
|
||||||
26
api/requests/dept.go
Normal file
26
api/requests/dept.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package requests
|
||||||
|
|
||||||
|
type DeptRequest struct {
|
||||||
|
AddDept // 新增部门
|
||||||
|
DeleteDept // 删除部门-批量
|
||||||
|
PutDept // 修改部门
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddDept 新增部门
|
||||||
|
type AddDept struct {
|
||||||
|
ParentId int64 `json:"parent_id" form:"parent_id" validate:"required" label:"上级部门"` // 本表父级id
|
||||||
|
DeptName string `json:"dept_name" form:"dept_name" validate:"required" label:"部门名称"`
|
||||||
|
DeptStatus int `json:"dept_status" form:"dept_status" validate:"required,oneof=1 2" label:"部门状态"` // (1:正常 2:删除)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteDept 删除部门-批量
|
||||||
|
type DeleteDept struct {
|
||||||
|
DeptIds []string `json:"dept_ids" form:"dept_ids" validate:"required" label:"部门id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutDept 修改部门
|
||||||
|
type PutDept struct {
|
||||||
|
ParentId int64 `json:"parent_id" form:"parent_id" validate:"required" label:"上级部门"` // 本表父级id
|
||||||
|
DeptName string `json:"dept_name" form:"dept_name" validate:"required" label:"部门名称"`
|
||||||
|
DeptStatus int `json:"dept_status" form:"dept_status" validate:"required,oneof=1 2" label:"部门状态"` // (1:正常 2:删除)
|
||||||
|
}
|
||||||
67
api/responses/apiResponse/api.go
Normal file
67
api/responses/apiResponse/api.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package apiResponse
|
||||||
|
|
||||||
|
import (
|
||||||
|
"hospital-admin-api/api/model"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getApiPage 获取接口列表-分页
|
||||||
|
type getApiPage struct {
|
||||||
|
APIID string `json:"api_id"` // 主键id
|
||||||
|
APIName string `json:"api_name"` // api名称
|
||||||
|
APIPath string `json:"api_path"` // 接口路径(全路径 id为:id)
|
||||||
|
APIMethod string `json:"api_method"` // 请求类型
|
||||||
|
IsAuth int `json:"is_auth"` // 是否验证权限(0:否 1:是)
|
||||||
|
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||||
|
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接口详情
|
||||||
|
type getApi struct {
|
||||||
|
APIID string `json:"api_id"` // 主键id
|
||||||
|
APIName string `json:"api_name"` // api名称
|
||||||
|
APIPath string `json:"api_path"` // 接口路径(全路径 id为:id)
|
||||||
|
APIMethod string `json:"api_method"` // 请求类型
|
||||||
|
IsAuth int `json:"is_auth"` // 是否验证权限(0:否 1:是)
|
||||||
|
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||||
|
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetApiPageResponse 获取接口列表-分页
|
||||||
|
func GetApiPageResponse(adminApi []*model.AdminAPI) []getApiPage {
|
||||||
|
// 处理返回值
|
||||||
|
getApiPageResponses := make([]getApiPage, len(adminApi))
|
||||||
|
|
||||||
|
if len(adminApi) > 0 {
|
||||||
|
for i, v := range adminApi {
|
||||||
|
// 将原始结构体转换为新结构体
|
||||||
|
getApiPageResponse := getApiPage{
|
||||||
|
APIID: strconv.Itoa(int(v.APIID)),
|
||||||
|
APIName: v.APIName,
|
||||||
|
APIPath: v.APIPath,
|
||||||
|
APIMethod: v.APIMethod,
|
||||||
|
IsAuth: v.IsAuth,
|
||||||
|
CreatedAt: v.CreatedAt,
|
||||||
|
UpdatedAt: v.UpdatedAt,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将转换后的结构体添加到新切片中
|
||||||
|
getApiPageResponses[i] = getApiPageResponse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getApiPageResponses
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetApiResponse 接口详情
|
||||||
|
func GetApiResponse(adminApi *model.AdminAPI) *getApi {
|
||||||
|
return &getApi{
|
||||||
|
APIID: strconv.Itoa(int(adminApi.APIID)),
|
||||||
|
APIName: adminApi.APIName,
|
||||||
|
APIPath: adminApi.APIPath,
|
||||||
|
APIMethod: adminApi.APIMethod,
|
||||||
|
IsAuth: adminApi.IsAuth,
|
||||||
|
CreatedAt: adminApi.CreatedAt,
|
||||||
|
UpdatedAt: adminApi.UpdatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
14
api/responses/deptResponse/dept.go
Normal file
14
api/responses/deptResponse/dept.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package deptResponse
|
||||||
|
|
||||||
|
import "hospital-admin-api/api/model"
|
||||||
|
|
||||||
|
// GetDeptList 获取部门列表
|
||||||
|
type GetDeptList struct {
|
||||||
|
DeptId string `json:"dept_id"`
|
||||||
|
ParentId string `json:"parent_id"` // 父菜单ID(0表示一级)
|
||||||
|
DeptName string `json:"dept_name"` // 部门名称
|
||||||
|
DeptStatus int `json:"dept_status"` // 部门状态(1:正常 2:删除)
|
||||||
|
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||||
|
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||||
|
Children []*GetDeptList `json:"children"` // 下级页面
|
||||||
|
}
|
||||||
@ -131,4 +131,42 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
|||||||
// 修改用户
|
// 修改用户
|
||||||
userGroup.PUT("/:user_id", api.User.PutUser)
|
userGroup.PUT("/:user_id", api.User.PutUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 接口
|
||||||
|
apiGroup := adminGroup.Group("/api")
|
||||||
|
{
|
||||||
|
// 获取接口列表-分页
|
||||||
|
apiGroup.GET("", api.AdminApi.GetApiPage)
|
||||||
|
|
||||||
|
// 新增接口
|
||||||
|
apiGroup.POST("", api.AdminApi.AddApi)
|
||||||
|
|
||||||
|
// 接口详情
|
||||||
|
apiGroup.GET("/:api_id", api.AdminApi.GetApi)
|
||||||
|
|
||||||
|
// 删除接口-批量
|
||||||
|
apiGroup.DELETE("", api.AdminApi.DeleteApi)
|
||||||
|
|
||||||
|
// 修改接口
|
||||||
|
apiGroup.PUT("/:api_id", api.AdminApi.PutApi)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 部门
|
||||||
|
deptGroup := adminGroup.Group("/dept")
|
||||||
|
{
|
||||||
|
// 获取部门列表
|
||||||
|
deptGroup.GET("", api.Dept.GetDeptList)
|
||||||
|
|
||||||
|
// 新增部门
|
||||||
|
deptGroup.POST("", api.Dept.GetDeptList)
|
||||||
|
|
||||||
|
// 部门详情
|
||||||
|
deptGroup.GET("/:api_id", api.Dept.GetDeptList)
|
||||||
|
|
||||||
|
// 删除部门-批量
|
||||||
|
deptGroup.DELETE("", api.Dept.GetDeptList)
|
||||||
|
|
||||||
|
// 修改部门
|
||||||
|
deptGroup.PUT("/:api_id", api.Dept.GetDeptList)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
206
api/service/api.go
Normal file
206
api/service/api.go
Normal file
@ -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
|
||||||
|
}
|
||||||
50
api/service/dept.go
Normal file
50
api/service/dept.go
Normal file
@ -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 {
|
for _, menu := range menuData {
|
||||||
|
if menuMap[menu.MenuId] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if menu.ParentId == 0 {
|
if menu.ParentId == 0 {
|
||||||
if menuMap[menu.MenuId] == nil {
|
if menuMap[menu.MenuId] == nil {
|
||||||
continue
|
continue
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user