改变路由层级,模块化
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
cache2 "cutego/modules/core/cache"
|
||||
"cutego/modules/core/entity"
|
||||
service2 "cutego/modules/core/service"
|
||||
"cutego/pkg/cache"
|
||||
"cutego/pkg/excels"
|
||||
"cutego/pkg/file"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ConfigApi struct {
|
||||
configService service2.ConfigService
|
||||
logService service2.LogService
|
||||
}
|
||||
|
||||
// GetConfigValue 根据参数键名查询参数值
|
||||
func (a ConfigApi) GetConfigValue(c *gin.Context) {
|
||||
param := c.Query("key")
|
||||
if !gotool.StrUtils.HasEmpty(param) {
|
||||
key := a.configService.GetConfigKey(param)
|
||||
c.JSON(http.StatusOK, resp.Success(key.ConfigValue, ""))
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("参数不合法"))
|
||||
}
|
||||
}
|
||||
|
||||
// List 查询设置列表
|
||||
func (a ConfigApi) List(c *gin.Context) {
|
||||
a.logService.LogStart(c)
|
||||
a.logService.LogToDB(c, "日志测试 start")
|
||||
|
||||
query := request.ConfigQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
find, i := a.configService.FindPage(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: find,
|
||||
Total: i,
|
||||
Size: query.PageSize,
|
||||
})
|
||||
a.logService.LogToDB(c, "日志测试 end")
|
||||
}
|
||||
|
||||
// Add 添加数据
|
||||
func (a ConfigApi) Add(c *gin.Context) {
|
||||
config := entity.SysConfig{}
|
||||
if c.Bind(&config) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
// 检验key是否存在
|
||||
if a.configService.CheckConfigKeyUnique(config) {
|
||||
resp.Error(c, "新增参数'"+config.ConfigName+"'失败, 参数键名已存在")
|
||||
}
|
||||
config.CreateBy = util.GetUserInfo(c).UserName
|
||||
if a.configService.Save(config) > 0 {
|
||||
// 进行缓存数据添加
|
||||
cache2.SetRedisConfig(config)
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Get 查询数据
|
||||
func (a ConfigApi) Get(c *gin.Context) {
|
||||
param := c.Param("configId")
|
||||
configId, _ := strconv.ParseInt(param, 10, 64)
|
||||
resp.OK(c, a.configService.GetInfo(configId))
|
||||
}
|
||||
|
||||
// Edit 修改数据
|
||||
func (a ConfigApi) Edit(c *gin.Context) {
|
||||
config := entity.SysConfig{}
|
||||
if c.Bind(&config) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
// 检验key是否存在
|
||||
if a.configService.CheckConfigKeyUnique(config) {
|
||||
resp.Error(c, "修改参数'"+config.ConfigName+"'失败, 参数键名已存在")
|
||||
}
|
||||
config.UpdateBy = util.GetUserInfo(c).UserName
|
||||
if a.configService.Edit(config) > 0 {
|
||||
cache2.SetRedisConfig(config)
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func (a ConfigApi) Delete(c *gin.Context) {
|
||||
ids := strings.Split(c.Param("ids"), ",")
|
||||
list := make([]int64, 0)
|
||||
for _, id := range ids {
|
||||
parseInt, _ := strconv.ParseInt(id, 10, 64)
|
||||
list = append(list, parseInt)
|
||||
}
|
||||
// 进行校验, 查看是否可以删除
|
||||
byIds := a.configService.CheckConfigByIds(list)
|
||||
for _, config := range *byIds {
|
||||
if config.ConfigType == "Y" {
|
||||
resp.Error(c, "内置参数"+config.ConfigName+"不能删除")
|
||||
return
|
||||
}
|
||||
}
|
||||
// 进行删除
|
||||
if a.configService.Remove(list) {
|
||||
// 刷新缓存
|
||||
strs := make([]string, 0)
|
||||
for _, config := range *byIds {
|
||||
strs = append(strs, config.ConfigKey)
|
||||
}
|
||||
cache.RemoveList(strs)
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// RefreshCache 刷新缓存
|
||||
func (a ConfigApi) RefreshCache(c *gin.Context) {
|
||||
all := a.configService.FindAll()
|
||||
for _, sysConfig := range *all {
|
||||
cache2.RemoveRedisConfig(sysConfig.ConfigKey)
|
||||
cache2.SetRedisConfig(sysConfig)
|
||||
}
|
||||
}
|
||||
|
||||
// Export 导出数据
|
||||
func (a ConfigApi) Export(c *gin.Context) {
|
||||
query := request.ConfigQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
find, _ := a.configService.FindPage(query)
|
||||
items := make([]interface{}, 0)
|
||||
for _, config := range *find {
|
||||
items = append(items, config)
|
||||
}
|
||||
_, files := excels.ExportExcel(items, "配置表")
|
||||
file.DownloadExcel(c, files)
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/api/v1/response"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/cronjob"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CronJobApi struct {
|
||||
cronJobService service.CronJobService
|
||||
}
|
||||
|
||||
// List 获取定时任务数据
|
||||
func (a CronJobApi) List(c *gin.Context) {
|
||||
query := request.CronJobQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
find, i := a.cronJobService.FindPage(query)
|
||||
|
||||
var result []interface{}
|
||||
for _, item := range find {
|
||||
// 领域对象转换
|
||||
result = append(result, util.DeepCopy(item, &response.CronJobResponse{}))
|
||||
}
|
||||
resp.OK(c, util.NewPage(result, i, query.PageSize))
|
||||
}
|
||||
|
||||
// GetById 根据id获取任务详情
|
||||
func (a CronJobApi) GetById(c *gin.Context) {
|
||||
jobId := c.Param("jobId")
|
||||
info := a.cronJobService.GetInfo(common.StringToInt64(jobId))
|
||||
resp.OK(c, util.DeepCopy(info, &response.CronJobResponse{}))
|
||||
}
|
||||
|
||||
// Edit 修改定时任务
|
||||
func (a CronJobApi) Edit(c *gin.Context) {
|
||||
record := entity.SysCronJob{}
|
||||
if c.Bind(&record) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
if a.cronJobService.Edit(record) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Add 新增定时任务
|
||||
func (a CronJobApi) Add(c *gin.Context) {
|
||||
dto := entity.SysCronJob{}
|
||||
if c.Bind(&dto) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
record := a.cronJobService.GetInfoByAlias(dto.FuncAlias)
|
||||
if record.JobId > 0 {
|
||||
resp.Error(c, "任务已存在!")
|
||||
} else {
|
||||
a.cronJobService.Save(dto)
|
||||
cronjob.AppendCronFunc(dto.JobCron, dto.FuncAlias, dto.Status)
|
||||
resp.OK(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 删除定时任务
|
||||
func (a CronJobApi) Remove(c *gin.Context) {
|
||||
jobId := common.StringToInt64(c.Param("jobId"))
|
||||
funcAlias := c.Param("funcAlias")
|
||||
|
||||
if a.cronJobService.GetInfo(jobId).Status == "1" {
|
||||
resp.Error(c, "任务运行中, 无法删除!")
|
||||
return
|
||||
}
|
||||
|
||||
cronjob.RemoveCronFunc(funcAlias)
|
||||
a.cronJobService.Remove([]int64{jobId})
|
||||
resp.OK(c)
|
||||
}
|
||||
|
||||
// ChangeStatus 改变任务状态
|
||||
func (a CronJobApi) ChangeStatus(c *gin.Context) {
|
||||
record := entity.SysCronJob{}
|
||||
if c.Bind(&record) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
if a.cronJobService.Edit(record) > 0 {
|
||||
if record.Status == "1" {
|
||||
cronjob.StartCronFunc(record.FuncAlias)
|
||||
} else {
|
||||
cronjob.StopCronFunc(record.FuncAlias)
|
||||
}
|
||||
}
|
||||
resp.OK(c)
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/tree/tree_dept"
|
||||
"cutego/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DeptApi struct {
|
||||
deptService service.DeptService
|
||||
}
|
||||
|
||||
// TreeSelect 查询部门菜单树
|
||||
func (a DeptApi) DeptTreeSelect(c *gin.Context) {
|
||||
query := request.DeptQuery{}
|
||||
if c.BindQuery(&query) == nil {
|
||||
treeSelect := a.deptService.FindTreeSelect(query)
|
||||
list := tree_dept.DeptList{}
|
||||
c.JSON(http.StatusOK, resp.Success(list.GetTree(treeSelect)))
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("参数绑定错误"))
|
||||
}
|
||||
}
|
||||
|
||||
// RoleDeptTreeSelect 加载对应角色部门列表树
|
||||
func (a DeptApi) RoleDeptTreeSelect(c *gin.Context) {
|
||||
m := make(map[string]interface{})
|
||||
param := c.Param("roleId")
|
||||
roleId, _ := strconv.ParseInt(param, 10, 64)
|
||||
checkedKeys := a.deptService.FindDeptListByRoleId(roleId)
|
||||
m["checkedKeys"] = checkedKeys
|
||||
treeSelect := a.deptService.FindTreeSelect(request.DeptQuery{})
|
||||
list := tree_dept.DeptList{}
|
||||
tree := list.GetTree(treeSelect)
|
||||
m["depts"] = tree
|
||||
resp.OK(c, m)
|
||||
}
|
||||
|
||||
// Find 查询部门列表
|
||||
func (a DeptApi) Find(c *gin.Context) {
|
||||
query := request.DeptQuery{}
|
||||
if c.BindQuery(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
resp.OK(c, a.deptService.FindDeptList(query))
|
||||
}
|
||||
|
||||
// ExcludeChild 查询部门列表(排除节点)
|
||||
func (a DeptApi) ExcludeChild(c *gin.Context) {
|
||||
param := c.Param("deptId")
|
||||
deptId, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
list := a.deptService.FindDeptList(request.DeptQuery{})
|
||||
var depts = *list
|
||||
deptList := make([]entity.SysDept, 0)
|
||||
for _, dept := range depts {
|
||||
if dept.DeptId == deptId || strings.Contains(dept.Ancestors, strconv.Itoa(deptId)) {
|
||||
continue
|
||||
}
|
||||
deptList = append(deptList, dept)
|
||||
}
|
||||
resp.OK(c, deptList)
|
||||
}
|
||||
|
||||
// GetInfo 根据部门编号获取详细信息
|
||||
func (a DeptApi) GetInfo(c *gin.Context) {
|
||||
param := c.Param("deptId")
|
||||
deptId, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
resp.OK(c, a.deptService.GetDeptById(deptId))
|
||||
}
|
||||
|
||||
// Add 添加部门
|
||||
func (a DeptApi) Add(c *gin.Context) {
|
||||
dept := entity.SysDept{}
|
||||
if c.Bind(&dept) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
// 校验部门名称是否唯一
|
||||
unique := a.deptService.CheckDeptNameUnique(dept)
|
||||
if unique {
|
||||
resp.Error(c, "新增部门'"+dept.DeptName+"'失败, 部门名称已存在")
|
||||
return
|
||||
}
|
||||
info := a.deptService.GetDeptById(dept.ParentId)
|
||||
if info.Status == "1" {
|
||||
resp.Error(c, "部门停用, 不允许新增")
|
||||
return
|
||||
}
|
||||
dept.Ancestors = info.Ancestors + "," + strconv.Itoa(dept.ParentId)
|
||||
dept.CreateBy = util.GetUserInfo(c).UserName
|
||||
if a.deptService.Save(dept) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 删除部门
|
||||
func (a DeptApi) Delete(c *gin.Context) {
|
||||
param := c.Param("deptId")
|
||||
deptId, _ := strconv.Atoi(param)
|
||||
// 是否存在部门子节点
|
||||
if a.deptService.HasChildByDeptId(deptId) > 0 {
|
||||
resp.Error(c, "存在下级部门,不允许删除")
|
||||
return
|
||||
}
|
||||
if a.deptService.CheckDeptExistUser(deptId) > 0 {
|
||||
resp.Error(c, "部门存在用户,不允许删除")
|
||||
return
|
||||
}
|
||||
if a.deptService.Remove(deptId) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Edit 修改部门数据接口
|
||||
func (a DeptApi) Edit(c *gin.Context) {
|
||||
dept := entity.SysDept{}
|
||||
if c.Bind(&dept) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
dept.UpdateTime = time.Now()
|
||||
dept.UpdateBy = util.GetUserInfo(c).UserName
|
||||
if a.deptService.Edit(dept) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/api/v1/response"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/excels"
|
||||
"cutego/pkg/file"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DictDataApi struct {
|
||||
dictDataService service.DictDataService
|
||||
}
|
||||
|
||||
// GetByType 根据字典类型查询字典数据d
|
||||
func (a DictDataApi) GetByType(c *gin.Context) {
|
||||
param := c.Param("dictType")
|
||||
if !gotool.StrUtils.HasEmpty(param) {
|
||||
list := a.dictDataService.FindByDictType(param)
|
||||
var result []response.DictDataResponse
|
||||
for _, data := range list {
|
||||
result = append(result, response.DictDataResponse{
|
||||
DictCode: data.DictCode,
|
||||
DictLabel: data.DictLabel,
|
||||
DictSort: data.DictSort,
|
||||
DictValue: data.DictValue,
|
||||
DictType: data.DictType,
|
||||
IsDefault: data.IsDefault,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, resp.Success(result))
|
||||
}
|
||||
}
|
||||
|
||||
// List 查询字典数据集合
|
||||
func (a DictDataApi) List(c *gin.Context) {
|
||||
query := request.DiceDataQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
list, i := a.dictDataService.FindPage(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: list,
|
||||
Total: i,
|
||||
Size: query.PageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// Get 根据id查询字典数据
|
||||
func (a DictDataApi) Get(c *gin.Context) {
|
||||
param := c.Param("dictCode")
|
||||
dictCode, _ := strconv.ParseInt(param, 10, 64)
|
||||
resp.OK(c, a.dictDataService.GetByCode(dictCode))
|
||||
}
|
||||
|
||||
// Add 添加字典数据
|
||||
func (a DictDataApi) Add(c *gin.Context) {
|
||||
data := entity.SysDictData{}
|
||||
if c.Bind(&data) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
data.CreateBy = util.GetUserInfo(c).UserName
|
||||
if a.dictDataService.Save(data) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Edit 编辑字典数据
|
||||
func (a DictDataApi) Edit(c *gin.Context) {
|
||||
data := entity.SysDictData{}
|
||||
if c.Bind(&data) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
data.UpdateBy = util.GetUserInfo(c).UserName
|
||||
data.UpdateTime = time.Now()
|
||||
if a.dictDataService.Edit(data) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func (a DictDataApi) Delete(c *gin.Context) {
|
||||
param := c.Param("dictCode")
|
||||
split := strings.Split(param, ",")
|
||||
dictCodeList := make([]int64, 0)
|
||||
for _, s := range split {
|
||||
diceCode, _ := strconv.ParseInt(s, 10, 64)
|
||||
dictCodeList = append(dictCodeList, diceCode)
|
||||
}
|
||||
if a.dictDataService.Remove(dictCodeList) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Export 导出excel
|
||||
func (a DictDataApi) Export(c *gin.Context) {
|
||||
query := request.DiceDataQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
items := make([]interface{}, 0)
|
||||
list, _ := a.dictDataService.FindPage(query)
|
||||
for _, data := range *list {
|
||||
items = append(items, data)
|
||||
}
|
||||
_, files := excels.ExportExcel(items, "字典数据表")
|
||||
file.DownloadExcel(c, files)
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/cache"
|
||||
"cutego/pkg/excels"
|
||||
"cutego/pkg/file"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DictTypeApi struct {
|
||||
dictTypeService service.DictTypeService
|
||||
dictDataService service.DictDataService
|
||||
}
|
||||
|
||||
// List 获取字典类型数据
|
||||
func (a DictTypeApi) List(c *gin.Context) {
|
||||
query := request.DictTypeQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
find, i := a.dictTypeService.FindPage(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: find,
|
||||
Total: i,
|
||||
Size: query.PageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// Get 根据id查询字典类型数据
|
||||
func (a DictTypeApi) Get(c *gin.Context) {
|
||||
param := c.Param("dictTypeId")
|
||||
dictTypeId, _ := strconv.ParseInt(param, 0, 64)
|
||||
resp.OK(c, a.dictTypeService.GetById(dictTypeId))
|
||||
}
|
||||
|
||||
// Edit 修改字典类型
|
||||
func (a DictTypeApi) Edit(c *gin.Context) {
|
||||
dictType := entity.SysDictType{}
|
||||
if c.Bind(&dictType) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
//检验字典类型是否存在
|
||||
if a.dictTypeService.CheckDictTypeUnique(dictType) {
|
||||
resp.Error(c, "修改字典'"+dictType.DictName+"'失败, 字典类型已存在")
|
||||
return
|
||||
}
|
||||
//修改数据
|
||||
if a.dictTypeService.Edit(dictType) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Add 新增字典类型
|
||||
func (a DictTypeApi) Add(c *gin.Context) {
|
||||
dictType := entity.SysDictType{}
|
||||
if c.Bind(&dictType) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
//检验字典类型是否存在
|
||||
if a.dictTypeService.CheckDictTypeUnique(dictType) {
|
||||
resp.Error(c, "新增字典'"+dictType.DictName+"'失败, 字典类型已存在")
|
||||
return
|
||||
}
|
||||
//新增字典类型
|
||||
if a.dictTypeService.Save(dictType) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 批量删除字典类型
|
||||
func (a DictTypeApi) Remove(c *gin.Context) {
|
||||
param := c.Param("dictId")
|
||||
split := strings.Split(param, ",")
|
||||
ids := make([]int64, 0)
|
||||
types := make([]string, 0)
|
||||
for _, s := range split {
|
||||
parseInt, _ := strconv.ParseInt(s, 10, 64)
|
||||
ids = append(ids, parseInt)
|
||||
}
|
||||
//校验字典类型是否使用
|
||||
for _, id := range ids {
|
||||
dictType := a.dictTypeService.GetById(id)
|
||||
if len(a.dictDataService.FindByDictType(dictType.DictType)) > 0 {
|
||||
resp.Error(c, dictType.DictName+"已分配,不能删除")
|
||||
return
|
||||
}
|
||||
types = append(types, dictType.DictType)
|
||||
}
|
||||
//批量删除
|
||||
if a.dictTypeService.Remove(ids) {
|
||||
//从缓存中删除数据
|
||||
cache.RemoveList(types)
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// RefreshCache 刷新缓存
|
||||
func (a DictTypeApi) RefreshCache(c *gin.Context) {
|
||||
a.dictTypeService.RefreshCache()
|
||||
resp.OK(c)
|
||||
}
|
||||
|
||||
// Export 导出Excel
|
||||
func (a DictTypeApi) Export(c *gin.Context) {
|
||||
query := request.DictTypeQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
find, _ := a.dictTypeService.FindPage(query)
|
||||
list := make([]interface{}, 0)
|
||||
for _, dictType := range *find {
|
||||
list = append(list, dictType)
|
||||
}
|
||||
_, files := excels.ExportExcel(list, "字典类型表")
|
||||
file.DownloadExcel(c, files)
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
request2 "cutego/modules/core/api/v1/request"
|
||||
cache2 "cutego/modules/core/cache"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/tree/tree_menu"
|
||||
"cutego/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LoginApi struct {
|
||||
loginService service.LoginService
|
||||
roleService service.RoleService
|
||||
permissionService service.PermissionService
|
||||
menuService service.MenuService
|
||||
loginInfoService service.LoginInfoService
|
||||
}
|
||||
|
||||
// Login 登录
|
||||
func (a LoginApi) Login(c *gin.Context) {
|
||||
loginBody := request2.LoginBody{}
|
||||
if c.BindJSON(&loginBody) == nil {
|
||||
m := make(map[string]string)
|
||||
loginStatus, token := a.loginService.Login(loginBody.UserName, loginBody.Password)
|
||||
if loginStatus {
|
||||
a.handleLoginInfo(c, loginBody, loginStatus)
|
||||
// 将token存入到redis中
|
||||
cache2.SetRedisToken(loginBody.UserName, token)
|
||||
m["token"] = token
|
||||
c.JSON(http.StatusOK, resp.Success(m))
|
||||
} else {
|
||||
a.handleLoginInfo(c, loginBody, loginStatus)
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(token))
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(500, "参数绑定错误"))
|
||||
}
|
||||
}
|
||||
|
||||
func (a LoginApi) handleLoginInfo(c *gin.Context, body request2.LoginBody, loginStatus bool) {
|
||||
status := common.If(loginStatus, "1", "0")
|
||||
|
||||
clientIp := a.loginInfoService.GetRequestClientIp(c)
|
||||
var location string
|
||||
if "127.0.0.1" == clientIp {
|
||||
location = "本机"
|
||||
} else {
|
||||
location = a.loginInfoService.GetLocationByIp(clientIp).Country
|
||||
}
|
||||
|
||||
a.loginInfoService.Save(entity.SysLoginInfo{
|
||||
UserName: body.UserName,
|
||||
IpAddr: clientIp,
|
||||
LoginLocation: location,
|
||||
Browser: c.GetHeader("User-Agent"),
|
||||
OS: strings.ReplaceAll(c.GetHeader("sec-ch-ua-platform"), "\"", ""),
|
||||
Status: status.(string),
|
||||
LoginTime: time.Now(),
|
||||
})
|
||||
}
|
||||
|
||||
// GetUserInfo 获取用户信息
|
||||
func (a LoginApi) GetUserInfo(c *gin.Context) {
|
||||
m := make(map[string]interface{})
|
||||
user := a.loginService.GetCurrentUser(c)
|
||||
// 查询用户角色集合
|
||||
roleKeys := a.permissionService.GetRolePermissionByUserId(user)
|
||||
// 权限集合
|
||||
perms := a.permissionService.GetMenuPermission(user)
|
||||
m["roles"] = roleKeys
|
||||
m["permissions"] = perms
|
||||
m["user"] = user
|
||||
c.JSON(http.StatusOK, resp.Success(m))
|
||||
}
|
||||
|
||||
// GetRouters 根据用户ID查询菜单
|
||||
func (a LoginApi) GetRouters(c *gin.Context) {
|
||||
// 获取当前登录用户
|
||||
user := a.loginService.GetCurrentUser(c)
|
||||
menus := a.menuService.GetMenuTreeByUserId(user)
|
||||
systemMenus := tree_menu.SystemMenus{}
|
||||
systemMenus = *menus
|
||||
array := systemMenus.ConvertToINodeArray(menus)
|
||||
generateTree := tree_menu.GenerateTree(array, nil)
|
||||
c.JSON(http.StatusOK, resp.Success(generateTree))
|
||||
}
|
||||
|
||||
// GetLoginHistory 根据用户名称查询登录记录
|
||||
func (a LoginApi) GetLoginHistory(c *gin.Context) {
|
||||
// 获取当前登录用户
|
||||
user := a.loginService.GetCurrentUser(c)
|
||||
// 配置参数
|
||||
query := request2.LoginInfoQuery{}
|
||||
query.UserName = user.UserName
|
||||
// 查询
|
||||
list, i := a.loginInfoService.FindPage(query)
|
||||
success := resp.Success(page.Page{
|
||||
Size: query.PageSize,
|
||||
Total: i,
|
||||
List: list,
|
||||
}, "查询成功")
|
||||
c.JSON(http.StatusOK, success)
|
||||
}
|
||||
|
||||
// Logout 退出登录
|
||||
func (a LoginApi) Logout(c *gin.Context) {
|
||||
// 删除Redis缓存
|
||||
name := util.GetUserInfo(c).UserName
|
||||
cache2.RemoveRedisToken(name)
|
||||
resp.OK(c)
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/tree/tree_menu"
|
||||
"cutego/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MenuApi struct {
|
||||
menuService service.MenuService
|
||||
}
|
||||
|
||||
// List 查询菜单数据
|
||||
func (a MenuApi) List(c *gin.Context) {
|
||||
// 获取当前登录用户
|
||||
info := util.GetUserInfo(c)
|
||||
// 获取参数
|
||||
query := request.MenuQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
resp.OK(c, a.menuService.FindMenuList(query, info))
|
||||
}
|
||||
|
||||
// GetInfo 根据id查询菜单详情
|
||||
func (a MenuApi) GetInfo(c *gin.Context) {
|
||||
param := c.Param("menuId")
|
||||
menuId, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
resp.ParamError(c, "参数绑定错误")
|
||||
return
|
||||
}
|
||||
resp.OK(c, a.menuService.GetMenuByMenuId(menuId))
|
||||
}
|
||||
|
||||
// RoleMenuTreeSelect 加载对应角色菜单列表树
|
||||
func (a MenuApi) RoleMenuTreeSelect(c *gin.Context) {
|
||||
m := make(map[string]interface{})
|
||||
param := c.Param("roleId")
|
||||
roleId, _ := strconv.ParseInt(param, 10, 64)
|
||||
// 获取当前登录用户
|
||||
info := util.GetUserInfo(c)
|
||||
menuList := a.menuService.GetMenuTreeByUserId(info)
|
||||
menus := tree_menu.SystemMenus{}
|
||||
tree := menus.GetTree(menuList)
|
||||
ids := a.menuService.FindMenuListByRoleId(roleId)
|
||||
m["checkedKeys"] = ids
|
||||
m["menus"] = tree
|
||||
c.JSON(http.StatusOK, resp.Success(m))
|
||||
}
|
||||
|
||||
// TreeSelect 获取菜单下拉树列表
|
||||
func (a MenuApi) TreeSelect(c *gin.Context) {
|
||||
info := util.GetUserInfo(c)
|
||||
menus := a.menuService.GetMenuTreeByUserId(info)
|
||||
systemMenus := tree_menu.SystemMenus{}
|
||||
tree := systemMenus.GetTree(menus)
|
||||
c.JSON(http.StatusOK, resp.Success(tree))
|
||||
}
|
||||
|
||||
// Add 添加菜单数据
|
||||
func (a MenuApi) Add(c *gin.Context) {
|
||||
menu := entity.SysMenu{}
|
||||
if c.Bind(&menu) != nil {
|
||||
resp.ParamError(c, "参数绑定异常")
|
||||
return
|
||||
}
|
||||
if a.menuService.Save(menu) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Edit 修改菜单数据
|
||||
func (a MenuApi) Edit(c *gin.Context) {
|
||||
menu := entity.SysMenu{}
|
||||
if c.Bind(&menu) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
menu.UpdateBy = util.GetUserInfo(c).UserName
|
||||
menu.UpdateTime = time.Now()
|
||||
if a.menuService.Edit(menu) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 删除菜单
|
||||
func (a MenuApi) Delete(c *gin.Context) {
|
||||
param := c.Param("menuId")
|
||||
menuId, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
if a.menuService.Remove(menuId) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NoticeApi struct {
|
||||
noticeService service.NoticeService
|
||||
}
|
||||
|
||||
// List 查询集合
|
||||
func (a NoticeApi) List(c *gin.Context) {
|
||||
query := request.NoticeQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
find, i := a.noticeService.FindPage(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: find,
|
||||
Total: i,
|
||||
Size: query.PageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// Add 添加公告
|
||||
func (a NoticeApi) Add(c *gin.Context) {
|
||||
notice := entity.SysNotice{}
|
||||
if c.Bind(¬ice) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
notice.CreateBy = util.GetUserInfo(c).UserName
|
||||
if a.noticeService.Save(notice) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
func (a NoticeApi) Delete(c *gin.Context) {
|
||||
ids := strings.Split(c.Param("ids"), ",")
|
||||
idList := make([]int64, 0)
|
||||
for _, s := range ids {
|
||||
id, _ := strconv.ParseInt(s, 10, 64)
|
||||
idList = append(idList, id)
|
||||
}
|
||||
if a.noticeService.Remove(idList) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Get 查询
|
||||
func (a NoticeApi) Get(c *gin.Context) {
|
||||
param := c.Param("id")
|
||||
id, _ := strconv.ParseInt(param, 10, 64)
|
||||
resp.OK(c, a.noticeService.GetById(id))
|
||||
}
|
||||
|
||||
// Edit 修改
|
||||
func (a NoticeApi) Edit(c *gin.Context) {
|
||||
notice := entity.SysNotice{}
|
||||
if c.Bind(¬ice) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
notice.UpdateTime = time.Now()
|
||||
notice.UpdateBy = util.GetUserInfo(c).UserName
|
||||
if a.noticeService.Edit(notice) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/excels"
|
||||
"cutego/pkg/file"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PostApi struct {
|
||||
postService service.PostService
|
||||
userPostService service.UserPostService
|
||||
}
|
||||
|
||||
// List 查询刚问分页数据
|
||||
func (a PostApi) List(c *gin.Context) {
|
||||
query := request.PostQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
list, i := a.postService.FindPage(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: list,
|
||||
Total: i,
|
||||
Size: query.PageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// Add 新增岗位
|
||||
func (a PostApi) Add(c *gin.Context) {
|
||||
post := entity.SysPost{}
|
||||
if c.Bind(&post) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
// 校验岗位名称是否存在
|
||||
if a.postService.CheckPostNameUnique(post) {
|
||||
resp.Error(c, "新增岗位'"+post.PostName+"'失败, 岗位名称已存在")
|
||||
return
|
||||
}
|
||||
// 检验岗位编码是否存在
|
||||
if a.postService.CheckPostCodeUnique(post) {
|
||||
resp.Error(c, "新增岗位'"+post.PostCode+"'失败, 岗位编码已存在")
|
||||
return
|
||||
}
|
||||
post.CreateBy = util.GetUserInfo(c).UserName
|
||||
if a.postService.Save(post) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Get 根据岗位编号获取详细信息
|
||||
func (a PostApi) Get(c *gin.Context) {
|
||||
param := c.Param("postId")
|
||||
postId, _ := strconv.ParseInt(param, 10, 64)
|
||||
resp.OK(c, a.postService.GetPostById(postId))
|
||||
}
|
||||
|
||||
// Delete 删除岗位数据
|
||||
func (a PostApi) Delete(c *gin.Context) {
|
||||
// 获取postId
|
||||
param := c.Param("postId")
|
||||
list := make([]int64, 0)
|
||||
if gotool.StrUtils.HasNotEmpty(param) {
|
||||
strs := strings.Split(param, ",")
|
||||
for _, str := range strs {
|
||||
postId, _ := strconv.ParseInt(str, 10, 64)
|
||||
list = append(list, postId)
|
||||
}
|
||||
}
|
||||
// 判断是否可以删除
|
||||
postId := a.userPostService.CountUserPostById(list)
|
||||
if postId > 0 {
|
||||
post := a.postService.GetPostById(postId)
|
||||
resp.Error(c, post.PostName+"岗位已分配, 不允许删除")
|
||||
return
|
||||
}
|
||||
if a.postService.Remove(list) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Edit 修改岗位数据接口
|
||||
func (a PostApi) Edit(c *gin.Context) {
|
||||
post := entity.SysPost{}
|
||||
if c.Bind(&post) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
// 校验岗位名称是否存在
|
||||
if a.postService.CheckPostNameUnique(post) {
|
||||
resp.Error(c, "修改岗位'"+post.PostName+"'失败, 岗位名称已存在")
|
||||
return
|
||||
}
|
||||
// 检验岗位编码是否存在
|
||||
if a.postService.CheckPostCodeUnique(post) {
|
||||
resp.Error(c, "修改岗位'"+post.PostCode+"'失败, 岗位编码已存在")
|
||||
return
|
||||
}
|
||||
post.UpdateBy = util.GetUserInfo(c).UserName
|
||||
post.UpdateTime = time.Now()
|
||||
if a.postService.Edit(post) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Export 导出excel
|
||||
func (a PostApi) Export(c *gin.Context) {
|
||||
query := request.PostQuery{}
|
||||
if c.Bind(&query) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
list, _ := a.postService.FindPage(query)
|
||||
excelList := make([]interface{}, 0)
|
||||
for _, post := range *list {
|
||||
excelList = append(excelList, post)
|
||||
}
|
||||
_, files := excels.ExportExcel(excelList, "岗位数据表")
|
||||
file.DownloadExcel(c, files)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
type ConfigQuery struct {
|
||||
base.GlobalQuery
|
||||
ConfigName string `form:"configName"`
|
||||
ConfigType string `form:"configType"`
|
||||
ConfigKey string `form:"configKey"`
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
type CronJobQuery struct {
|
||||
base.GlobalQuery
|
||||
JobName string `form:"jobName"`
|
||||
Status string `form:"Status"`
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package request
|
||||
|
||||
// DeptQuery 部门查询参数结构体 GET请求
|
||||
type DeptQuery struct {
|
||||
ParentId int `form:"parentId"`
|
||||
DeptName string `form:"deptName"`
|
||||
Status string `form:"status"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
type DiceDataQuery struct {
|
||||
base.GlobalQuery
|
||||
DictType string `form:"dictType"`
|
||||
DictLabel string `form:"dictLabel"`
|
||||
Status string `form:"status"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
type DictTypeQuery struct {
|
||||
base.GlobalQuery
|
||||
DictName string `form:"dictName"`
|
||||
Status string `form:"status"`
|
||||
DictType string `form:"dictType"`
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
type LogQuery struct {
|
||||
base.GlobalQuery
|
||||
Uid string `form:"uid"`
|
||||
Content string `form:"content"`
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
// LoginInfoQuery 用户get请求数据参数
|
||||
type LoginInfoQuery struct {
|
||||
base.GlobalQuery
|
||||
UserName string // 筛选用户名称
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package request
|
||||
|
||||
// LoginBody 登录参数
|
||||
type LoginBody struct {
|
||||
UserName string `json:"username"` // 用户名
|
||||
Password string `json:"password"` // 密码
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
// MenuQuery 菜单查询条件封装
|
||||
type MenuQuery struct {
|
||||
base.GlobalQuery
|
||||
MenuName string `json:"menuName" form:"menuName"`
|
||||
Visible string `json:"visible" form:"visible"`
|
||||
Status string `json:"status" form:"status"`
|
||||
UserId int64 `json:"userId" form:"userId"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
type NoticeQuery struct {
|
||||
base.GlobalQuery
|
||||
NoticeTitle string `form:"noticeTitle"`
|
||||
NoticeType string `form:"noticeType"`
|
||||
CreateBy string `form:"createBy"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
type PostQuery struct {
|
||||
base.GlobalQuery
|
||||
PostCode string `form:"postCode"`
|
||||
Status string `form:"status"`
|
||||
PostName string `form:"postName"`
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package request
|
||||
|
||||
import "cutego/pkg/base"
|
||||
|
||||
// RoleQuery 角色Get请求参数
|
||||
type RoleQuery struct {
|
||||
base.GlobalQuery
|
||||
RoleName string `form:"roleName"` // 角色名称
|
||||
Status string `form:"status"` // 角色状态
|
||||
RoleKey string `form:"roleKey"` // 角色Key
|
||||
}
|
||||
|
||||
// RoleBody 角色Post和Put参数
|
||||
type RoleBody struct {
|
||||
RoleId int64 `json:"roleId"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"cutego/pkg/base"
|
||||
"time"
|
||||
)
|
||||
|
||||
// UserQuery 用户get请求数据参数
|
||||
type UserQuery struct {
|
||||
base.GlobalQuery
|
||||
RoleId int64 `form:"roleId"` // 角色id
|
||||
UserName string `form:"userName"` // 用户名
|
||||
Status string `form:"status"` // 状态
|
||||
PhoneNumber string `form:"phoneNumber"` // 手机号
|
||||
DeptId int64 `form:"deptId"` // 部门id
|
||||
}
|
||||
|
||||
// UserBody 用户接收POST 或者 PUT请求参数
|
||||
type UserBody struct {
|
||||
UserId int64 `xorm:"pk autoincr" json:"userId"` // 用户ID
|
||||
DeptId int64 `json:"deptId"` // 部门ID
|
||||
UserName string `json:"userName"` // 登录用户名
|
||||
NickName string `json:"nickName"` // 用户昵称
|
||||
Email string `json:"email"` // 邮箱
|
||||
PhoneNumber string `json:"phoneNumber"` // 手机号
|
||||
Sex string `json:"sex"` // 性别0男1女
|
||||
Avatar string `json:"avatar"` // 头像路径
|
||||
Password string `json:"password"` // 密码
|
||||
Status string `json:"status"` // 状态 0正常1停用
|
||||
DelFlag string `json:"delFlag"` // 0正常1删除
|
||||
LoginIp string `json:"loginIp"` // 登录ip
|
||||
LoginDate time.Time `json:"loginDate"` // 登录时间
|
||||
CreateTime time.Time `xorm:"created" json:"createTime"` // 创建时间
|
||||
CreateBy string `json:"createBy"` // 创建人
|
||||
UpdateTime time.Time `json:"updateTime"` // 更新时间
|
||||
UpdateBy string `json:"updateBy"` // 更新人
|
||||
RoleIds []int64 `xorm:"-" json:"roleIds"` // 角色id组
|
||||
PostIds []int64 `xorm:"-" json:"postIds"` // 岗位id组
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package request
|
||||
|
||||
// UserRoleBody 用户角色Post和Put参数接收结构体
|
||||
type UserRoleBody struct {
|
||||
RoleId int64 `form:"roleId" json:"roleId"`
|
||||
UserIds []int64 `form:"userIds" json:"userIds"`
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package response
|
||||
|
||||
type CronJobResponse struct {
|
||||
JobId int `json:"jobId"` // 任务主键
|
||||
JobName string `json:"jobName"` // 任务名称
|
||||
JobCron string `json:"jobCron"` // cron表达式
|
||||
FuncAlias string `json:"funcAlias"` // 方法别名(程序内注册的别名)
|
||||
FuncParam string `json:"funcParam"` // 方法参数
|
||||
Status string `json:"status"` // 状态(1、Running 0、Stop)
|
||||
Level int `json:"level"` // 任务级别(0、普通 1、一般 2、重要 3、强保)
|
||||
Remark string `json:"remark"` // 备注
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package response
|
||||
|
||||
// DictDataResponse 字典数据实体返回结构体
|
||||
type DictDataResponse struct {
|
||||
DictCode int64 `excel:"name=字典编码" xorm:"pk autoincr" json:"dictCode"` // 字典ID
|
||||
DictSort int `excel:"name=字典排序" xorm:"int" json:"dictSort"` // 字典排序
|
||||
DictLabel string `excel:"name=字典标签" xorm:"varchar(128)" json:"dictLabel"` // 字典标签
|
||||
DictValue string `excel:"name=字典键值" xorm:"varchar(128)" json:"dictValue"` // 字典键值
|
||||
DictType string `excel:"name=字典类型" xorm:"varchar(128)" json:"dictType"` // 字典类型
|
||||
IsDefault string `excel:"name=是否默认,format=Y=是,N=否" json:"isDefault"` // 是否默认
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"cutego/modules/core/entity"
|
||||
"time"
|
||||
)
|
||||
|
||||
// UserResponse 用户实体返回结构体
|
||||
type UserResponse struct {
|
||||
UserId int64 `json:"userId"` // 用户ID
|
||||
DeptId int64 `excel:"name=部门" json:"deptId"` // 部门ID
|
||||
UserName string `excel:"name=用户登录名" json:"userName"` // 登录用户名
|
||||
NickName string `excel:"name=用户昵称" json:"nickName"` // 用户昵称
|
||||
Email string `excel:"name=用户邮箱" json:"email"` // 邮箱
|
||||
PhoneNumber string `excel:"name=手机号" json:"phoneNumber"` // 手机号
|
||||
Sex string `excel:"name=性别,format=0=男,1=女,2=未知" json:"sex"` // 性别0男1女
|
||||
Avatar string `json:"avatar"` // 头像路径
|
||||
Status string `json:"status"` // 状态 0正常1停用
|
||||
DelFlag string `json:"delFlag"` // 0正常1删除
|
||||
LoginIp string `json:"loginIp"` // 登录ip
|
||||
LoginDate time.Time `json:"loginDate"` // 登录时间
|
||||
CreateTime time.Time `xorm:"created" json:"createTime"` // 创建时间
|
||||
CreateBy string `json:"createBy"` // 创建人
|
||||
UpdateTime time.Time `json:"updateTime"` // 更新时间
|
||||
UpdateBy string `json:"updateBy"` // 更新人
|
||||
SysDept entity.SysDept `xorm:"extends" json:"dept"` // 部门实体
|
||||
}
|
||||
|
||||
// UserInfo 用户整体数据
|
||||
type UserInfo struct {
|
||||
User *UserResponse `json:"user,omitempty"` // 用户数据
|
||||
Roles []*entity.SysRole `json:"roles,omitempty"` // 角色集合
|
||||
Posts []*entity.SysPost `json:"posts,omitempty"` // 部门集合
|
||||
PostIds *[]int64 `json:"postIds,omitempty"` // 岗位id集合
|
||||
RoleIds *[]int64 `json:"roleIds,omitempty"` // 觉得id集合
|
||||
}
|
||||
|
||||
// IsAdmin 判断当前用户是否是管理员
|
||||
func (r UserResponse) IsAdmin() bool {
|
||||
return r.UserId == 1
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/excels"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RoleApi struct {
|
||||
roleService service.RoleService
|
||||
userService service.UserService
|
||||
}
|
||||
|
||||
// Find 分页查询角色数据
|
||||
func (a RoleApi) Find(c *gin.Context) {
|
||||
query := request.RoleQuery{}
|
||||
if c.BindQuery(&query) == nil {
|
||||
list, i := a.roleService.FindPage(query)
|
||||
p := page.Page{
|
||||
List: list,
|
||||
Total: i,
|
||||
Size: query.PageSize,
|
||||
}
|
||||
c.JSON(http.StatusOK, resp.Success(p))
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("参数绑定异常"))
|
||||
}
|
||||
}
|
||||
|
||||
// GetRoleId 根据人roleId查询角色数据
|
||||
func (a RoleApi) GetRoleId(c *gin.Context) {
|
||||
param := c.Param("roleId")
|
||||
roleId, err := strconv.ParseInt(param, 10, 64)
|
||||
if err == nil {
|
||||
role := a.roleService.GetRoleByRoleId(roleId)
|
||||
c.JSON(200, resp.Success(role))
|
||||
} else {
|
||||
c.JSON(500, resp.ErrorResp("参数绑定异常"))
|
||||
}
|
||||
}
|
||||
|
||||
// Add 添加角色业务操作
|
||||
func (a RoleApi) Add(c *gin.Context) {
|
||||
role := entity.SysRole{}
|
||||
if c.BindJSON(&role) == nil {
|
||||
if a.roleService.CheckRoleNameUnique(role) > 0 {
|
||||
c.JSON(500, resp.ErrorResp(500, "新增角色'"+role.RoleName+"'失败, 角色名称已存在"))
|
||||
} else if a.roleService.CheckRoleKeyUnique(role) > 0 {
|
||||
c.JSON(500, resp.ErrorResp(500, "新增角色'"+role.RoleName+"'失败, 角色权限已存在"))
|
||||
}
|
||||
role.CreateBy = util.GetUserInfo(c).UserName
|
||||
if a.roleService.Save(role) > 0 {
|
||||
c.JSON(200, resp.Success("保存成功"))
|
||||
} else {
|
||||
c.JSON(500, resp.Success("保存失败"))
|
||||
}
|
||||
} else {
|
||||
c.JSON(500, resp.ErrorResp("参数绑定异常"))
|
||||
}
|
||||
}
|
||||
|
||||
// Edit 修改角色
|
||||
func (a RoleApi) Edit(c *gin.Context) {
|
||||
role := entity.SysRole{}
|
||||
if c.BindJSON(&role) == nil {
|
||||
if a.roleService.CheckRoleNameUnique(role) > 0 {
|
||||
c.JSON(500, resp.ErrorResp(500, "修改角色'"+role.RoleName+"'失败, 角色名称已存在"))
|
||||
} else if a.roleService.CheckRoleKeyUnique(role) > 0 {
|
||||
c.JSON(500, resp.ErrorResp(500, "修改角色'"+role.RoleName+"'失败, 角色权限已存在"))
|
||||
}
|
||||
role.CreateBy = util.GetUserInfo(c).UserName
|
||||
role.CreateTime = time.Now()
|
||||
if a.roleService.Edit(role) > 0 {
|
||||
c.JSON(200, resp.Success("修改成功"))
|
||||
} else {
|
||||
c.JSON(500, resp.Success("修改失败"))
|
||||
}
|
||||
} else {
|
||||
c.JSON(500, resp.ErrorResp("参数绑定异常"))
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 删除角色
|
||||
func (a RoleApi) Delete(c *gin.Context) {
|
||||
param := c.Param("roleId")
|
||||
roleId, err := strconv.ParseInt(param, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(500, resp.ErrorResp("参数绑定异常"))
|
||||
return
|
||||
}
|
||||
if a.roleService.Remove(roleId) > 0 {
|
||||
c.JSON(200, resp.Success("删除成功"))
|
||||
} else {
|
||||
c.JSON(500, resp.Success("删除失败"))
|
||||
}
|
||||
}
|
||||
|
||||
// ChangeStatus 状态修改
|
||||
func (a RoleApi) ChangeStatus(c *gin.Context) {
|
||||
body := entity.SysRole{}
|
||||
if c.BindJSON(&body) != nil {
|
||||
c.JSON(500, resp.ErrorResp("参数绑定异常"))
|
||||
return
|
||||
}
|
||||
allowed, s := a.roleService.CheckRoleAllowed(body.RoleId)
|
||||
if !allowed {
|
||||
c.JSON(500, resp.ErrorResp(s))
|
||||
return
|
||||
}
|
||||
body.UpdateTime = time.Now()
|
||||
body.UpdateBy = util.GetUserInfo(c).UserName
|
||||
if a.roleService.EditRoleStatus(&body) > 0 {
|
||||
resp.OK(c, "修改成功")
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// AllocatedList 查询已分配用户角色列表
|
||||
func (a RoleApi) AllocatedList(c *gin.Context) {
|
||||
query := request.UserQuery{}
|
||||
if c.BindQuery(&query) != nil {
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
list, i := a.userService.GetAllocatedList(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: list,
|
||||
Total: i,
|
||||
})
|
||||
}
|
||||
|
||||
// UnallocatedList 查询未分配用户角色列表
|
||||
func (a RoleApi) UnallocatedList(c *gin.Context) {
|
||||
query := request.UserQuery{}
|
||||
if c.BindQuery(&query) != nil {
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
list, i := a.userService.GetUnallocatedList(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: list,
|
||||
Total: i,
|
||||
})
|
||||
}
|
||||
|
||||
// CancelAuthUser 取消授权用户
|
||||
func (a RoleApi) CancelAuthUser(c *gin.Context) {
|
||||
roleUser := entity.SysUserRole{}
|
||||
if c.BindJSON(&roleUser) != nil {
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
if a.roleService.DeleteAuthUser(roleUser) > 0 {
|
||||
resp.OK(c, "操作成功")
|
||||
} else {
|
||||
resp.Error(c, "操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateAuthUserAll 批量选择用户授权
|
||||
func (a RoleApi) UpdateAuthUserAll(c *gin.Context) {
|
||||
body := request.UserRoleBody{}
|
||||
if c.Bind(&body) != nil {
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
if a.roleService.InsertAuthUsers(body) > 0 {
|
||||
resp.OK(c, "操作成功")
|
||||
} else {
|
||||
resp.Error(c, "操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
// Export 导出Excel
|
||||
func (a RoleApi) Export(c *gin.Context) {
|
||||
query := request.RoleQuery{}
|
||||
items := make([]interface{}, 0)
|
||||
if c.BindQuery(&query) == nil {
|
||||
list, _ := a.roleService.FindPage(query)
|
||||
for _, role := range list {
|
||||
items = append(items, *role)
|
||||
}
|
||||
_, file := excels.ExportExcel(items, "角色表")
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
c.Header("Content-Disposition", "attachment; filename="+gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
|
||||
c.Header("Content-Transfer-Encoding", "binary")
|
||||
c.Header("FileName", gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
|
||||
file.Write(c.Writer)
|
||||
} else {
|
||||
c.JSON(200, resp.ErrorResp(500, "参数错误"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/api/v1/response"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/modules/core/service"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/excels"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// UserApi 用户操作api
|
||||
type UserApi struct {
|
||||
userService service.UserService
|
||||
roleService service.RoleService
|
||||
postService service.PostService
|
||||
}
|
||||
|
||||
// Find 查询用户列表
|
||||
func (a UserApi) Find(c *gin.Context) {
|
||||
query := request.UserQuery{}
|
||||
if c.BindQuery(&query) == nil {
|
||||
list, i := a.userService.FindList(query)
|
||||
success := resp.Success(page.Page{
|
||||
Size: query.PageSize,
|
||||
Total: i,
|
||||
List: list,
|
||||
}, "查询成功")
|
||||
c.JSON(http.StatusOK, success)
|
||||
} else {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "参数错误"))
|
||||
}
|
||||
}
|
||||
|
||||
// GetInfo 查询用户信息
|
||||
func (a UserApi) GetInfo(c *gin.Context) {
|
||||
param := c.Param("userId")
|
||||
r := new(response.UserInfo)
|
||||
// 查询角色
|
||||
roleAll, _ := a.roleService.FindAll(nil)
|
||||
// 岗位所有数据
|
||||
postAll := a.postService.FindAll()
|
||||
// 判断id传入的是否为空
|
||||
if !gotool.StrUtils.HasEmpty(param) {
|
||||
parseInt, err := strconv.ParseInt(param, 10, 64)
|
||||
if err == nil {
|
||||
// 判断当前登录用户是否是admin
|
||||
m := new(entity.SysUser)
|
||||
if m.IsAdmin(parseInt) {
|
||||
r.Roles = roleAll
|
||||
} else {
|
||||
roles := make([]*entity.SysRole, 0)
|
||||
for _, role := range roleAll {
|
||||
if role.RoleId != 1 {
|
||||
roles = append(roles, role)
|
||||
}
|
||||
}
|
||||
r.Roles = roles
|
||||
}
|
||||
// 根据id获取用户数据
|
||||
r.User = a.userService.GetUserById(parseInt)
|
||||
// 根据用户ID查询岗位id集合
|
||||
r.PostIds = a.postService.FindPostListByUserId(parseInt)
|
||||
// 根据用户ID查询角色id集合
|
||||
r.RoleIds = a.roleService.FindRoleListByUserId(parseInt)
|
||||
}
|
||||
} else {
|
||||
//id为空不取管理员角色
|
||||
roles := make([]*entity.SysRole, 0)
|
||||
for _, role := range roleAll {
|
||||
if role.RoleId != 1 {
|
||||
roles = append(roles, role)
|
||||
}
|
||||
}
|
||||
r.Roles = roles
|
||||
}
|
||||
r.Posts = postAll
|
||||
c.JSON(http.StatusOK, resp.Success(r, "操作成功"))
|
||||
}
|
||||
|
||||
// AuthRole 根据用户编号获取授权角色
|
||||
func (a UserApi) AuthRole(c *gin.Context) {
|
||||
m := make(map[string]interface{})
|
||||
userId := c.Param("userId")
|
||||
parseInt, err := strconv.ParseInt(userId, 10, 64)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp(err))
|
||||
}
|
||||
user := a.userService.GetUserById(parseInt)
|
||||
// 查询角色
|
||||
roles := a.roleService.GetRoleListByUserId(parseInt)
|
||||
flag := entity.SysUser{}.IsAdmin(parseInt)
|
||||
if flag {
|
||||
m["roles"] = roles
|
||||
} else {
|
||||
roleList := make([]entity.SysRole, 0)
|
||||
for _, role := range *roles {
|
||||
if role.RoleId != 1 {
|
||||
roleList = append(roleList, role)
|
||||
}
|
||||
}
|
||||
m["roles"] = roleList
|
||||
}
|
||||
m["user"] = user
|
||||
c.JSON(http.StatusOK, resp.Success(m))
|
||||
}
|
||||
|
||||
// Add 新增用户
|
||||
func (a UserApi) Add(c *gin.Context) {
|
||||
userBody := request.UserBody{}
|
||||
if c.BindJSON(&userBody) == nil {
|
||||
// 根据用户名查询用户
|
||||
user := a.userService.GetUserByUserName(userBody.UserName)
|
||||
if user != nil {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "失败, 登录账号已存在"))
|
||||
return
|
||||
} else if a.userService.CheckPhoneNumUnique(userBody) != nil {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "失败, 手机号码已存在"))
|
||||
return
|
||||
} else if a.userService.CheckEmailUnique(userBody) != nil {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "失败, 邮箱已存在"))
|
||||
return
|
||||
}
|
||||
// 进行密码加密
|
||||
userBody.Password = gotool.BcryptUtils.Generate(userBody.Password)
|
||||
// 添加用户
|
||||
if a.userService.Save(userBody) {
|
||||
c.JSON(http.StatusOK, resp.Success(nil))
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("保存失败"))
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("参数错误"))
|
||||
}
|
||||
}
|
||||
|
||||
// Edit 修改用户
|
||||
func (a UserApi) Edit(c *gin.Context) {
|
||||
userBody := request.UserBody{}
|
||||
if c.BindJSON(&userBody) == nil {
|
||||
if a.userService.CheckPhoneNumUnique(userBody) != nil {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "失败, 手机号码已存在"))
|
||||
return
|
||||
} else if a.userService.CheckEmailUnique(userBody) != nil {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "失败, 邮箱已存在"))
|
||||
return
|
||||
}
|
||||
// 进行用户修改操作
|
||||
if a.userService.Edit(userBody) > 0 {
|
||||
resp.OK(c)
|
||||
return
|
||||
} else {
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 删除用户
|
||||
func (a UserApi) Remove(c *gin.Context) {
|
||||
param := c.Param("userId")
|
||||
userId, err := strconv.ParseInt(param, 10, 64)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("参数错误"))
|
||||
return
|
||||
}
|
||||
if a.userService.Remove(userId) > 0 {
|
||||
c.JSON(http.StatusOK, resp.Success(nil))
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("删除失败"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// ResetPwd 修改重置密码
|
||||
func (a UserApi) ResetPwd(c *gin.Context) {
|
||||
userBody := request.UserBody{}
|
||||
if c.BindJSON(&userBody) == nil {
|
||||
if a.userService.CheckUserAllowed(userBody) {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("不允许操作超级管理员用户"))
|
||||
return
|
||||
}
|
||||
userBody.Password = gotool.BcryptUtils.Generate(userBody.Password)
|
||||
if a.userService.ResetPwd(userBody) > 0 {
|
||||
c.JSON(http.StatusOK, resp.Success(nil))
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("重置失败"))
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, resp.ErrorResp("参数错误"))
|
||||
}
|
||||
}
|
||||
|
||||
// Export 导出excel
|
||||
func (a UserApi) Export(c *gin.Context) {
|
||||
query := request.UserQuery{}
|
||||
if c.BindQuery(&query) == nil {
|
||||
items := make([]interface{}, 0)
|
||||
list, _ := a.userService.FindList(query)
|
||||
for _, userResponse := range list {
|
||||
items = append(items, *userResponse)
|
||||
}
|
||||
_, file := excels.ExportExcel(items, "用户表")
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
c.Header("Content-Disposition", "attachment; filename="+gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
|
||||
c.Header("Content-Transfer-Encoding", "binary")
|
||||
c.Header("FileName", gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
|
||||
file.Write(c.Writer)
|
||||
} else {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "参数错误"))
|
||||
}
|
||||
}
|
||||
|
||||
// Profile 查询个人信息
|
||||
func (a UserApi) Profile(c *gin.Context) {
|
||||
m := make(map[string]interface{})
|
||||
info := util.GetUserInfo(c)
|
||||
u := a.userService.GetUserById(info.UserId)
|
||||
m["user"] = u
|
||||
// 查询所属角色组
|
||||
m["roleGroup"] = a.roleService.GetRolesByUserName(info.UserName)
|
||||
m["postGroup"] = a.postService.FindPostByUserName(info.UserName)
|
||||
resp.OK(c, m)
|
||||
}
|
||||
|
||||
// UpdateProfile 修改个人数据
|
||||
func (a UserApi) UpdateProfile(c *gin.Context) {
|
||||
user := request.UserBody{}
|
||||
if c.Bind(&user) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
if a.userService.CheckEmailUnique(user) != nil {
|
||||
resp.Error(c, "修改用户'"+user.UserName+"'失败, 邮箱账号已存在")
|
||||
return
|
||||
}
|
||||
if a.userService.CheckPhoneNumUnique(user) != nil {
|
||||
resp.Error(c, "修改用户'"+user.UserName+"'失败, 手机号已存在")
|
||||
return
|
||||
}
|
||||
if a.userService.EditProfile(user) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdatePwd 修改个人密码
|
||||
func (a UserApi) UpdatePwd(c *gin.Context) {
|
||||
oldPassword := c.Query("oldPassword")
|
||||
newPassword := c.Query("newPassword")
|
||||
info := util.GetUserInfo(c)
|
||||
name := a.userService.GetUserByUserName(info.UserName)
|
||||
hash := gotool.BcryptUtils.CompareHash(name.Password, oldPassword)
|
||||
if !hash {
|
||||
resp.Error(c, "修改密码失败, 旧密码错误")
|
||||
return
|
||||
}
|
||||
generate := gotool.BcryptUtils.Generate(oldPassword)
|
||||
compareHash := gotool.BcryptUtils.CompareHash(generate, newPassword)
|
||||
if compareHash {
|
||||
resp.Error(c, "新密码不能与旧密码相同")
|
||||
return
|
||||
}
|
||||
pwd := a.userService.EditPwd(info.UserId, gotool.BcryptUtils.Generate(newPassword))
|
||||
if pwd {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Avatar 修改头像
|
||||
func (a UserApi) Avatar(c *gin.Context) {
|
||||
dirPath := common.GetDirPath("avatar")
|
||||
file, _, err := c.Request.FormFile("avatarFile")
|
||||
fileName := gotool.IdUtils.IdUUIDToRan(true) + ".jpg"
|
||||
filePath := dirPath + fileName
|
||||
fileAppend, err := gotool.FileUtils.OpenFileAppend(filePath)
|
||||
defer fileAppend.Close()
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
_, err = io.Copy(fileAppend, file)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
resp.Error(c)
|
||||
return
|
||||
}
|
||||
info := util.GetUserInfo(c)
|
||||
info.Avatar = filePath
|
||||
avatar := a.userService.EditAvatar(info)
|
||||
if avatar {
|
||||
m := make(map[string]interface{})
|
||||
m["imgUrl"] = filePath
|
||||
resp.OK(c, m)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// 修改可用状态
|
||||
func (a UserApi) ChangeStatus(c *gin.Context) {
|
||||
user := request.UserBody{}
|
||||
if c.Bind(&user) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
if user.UserId == 1 && user.Status == "1" {
|
||||
c.JSON(http.StatusOK, resp.ErrorResp(http.StatusInternalServerError, "不可禁用admin账号"))
|
||||
return
|
||||
}
|
||||
if a.userService.EditStatus(user) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user