项目目录优化,任务模块后端代码

This commit is contained in:
PandaGoAdmin
2021-12-23 17:23:27 +08:00
parent 0caf81660c
commit 21ff92a93c
67 changed files with 802 additions and 206 deletions

124
apps/system/api/api.go Normal file
View File

@@ -0,0 +1,124 @@
package api
import (
"log"
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/casbin"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/utils"
)
type SystemApiApi struct {
ApiApp services2.SysApiModel
}
// @Tags SysApi
// @Summary 创建基础api
// @Security X-TOKEN
// @accept application/json
// @Produce application/json
// @Param data body entity.SysApi true "api路径, api中文描述, api组, 方法"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /system/api [post]
func (s *SystemApiApi) CreateApi(rc *ctx.ReqCtx) {
var api entity2.SysApi
ginx.BindJsonAndValid(rc.GinCtx, &api)
log.Println(api)
s.ApiApp.Insert(api)
}
// @Tags SysApi
// @Summary 删除api
// @Security X-TOKEN
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /system/api/{id} [delete]
func (s *SystemApiApi) DeleteApi(rc *ctx.ReqCtx) {
ids := rc.GinCtx.Param("id")
s.ApiApp.Delete(utils.IdsStrToIdsIntGroup(ids))
}
// @Tags SysApi
// @Summary 分页获取API列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param path query string false "path"
// @Param description query string false "description"
// @Param method query string false "method"
// @Param apiGroup query string false "apiGroup"
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /system/api/list [get]
func (s *SystemApiApi) GetApiList(rc *ctx.ReqCtx) {
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
path := rc.GinCtx.Query("path")
description := rc.GinCtx.Query("description")
method := rc.GinCtx.Query("method")
apiGroup := rc.GinCtx.Query("apiGroup")
api := entity2.SysApi{Path: path, Description: description, Method: method, ApiGroup: apiGroup}
list, total := s.ApiApp.FindListPage(pageNum, pageSize, api)
rc.ResData = map[string]interface{}{
"data": list,
"total": total,
"pageNum": pageNum,
"pageSize": pageSize,
}
}
// @Tags SysApi
// @Summary 根据id获取api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取api"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /system/api/{id} [get]
func (s *SystemApiApi) GetApiById(rc *ctx.ReqCtx) {
id := ginx.QueryInt(rc.GinCtx, "id", 0)
rc.ResData = s.ApiApp.FindOne(int64(id))
}
// @Tags SysApi
// @Summary 创建基础api
// @Security X-TOKEN
// @accept application/json
// @Produce application/json
// @Param data body entity.SysApi true "api路径, api中文描述, api组, 方法"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /api/api [put]
func (s *SystemApiApi) UpdateApi(rc *ctx.ReqCtx) {
var api entity2.SysApi
ginx.BindJsonAndValid(rc.GinCtx, &api)
s.ApiApp.Update(api)
}
// @Tags SysApi
// @Summary 获取所有的Api 不分页
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /system/api/all [get]
func (s *SystemApiApi) GetAllApis(rc *ctx.ReqCtx) {
rc.ResData = s.ApiApp.FindList(entity2.SysApi{})
}
// @Tags SysApi
// @Summary 获取Api权限列表
// @Security X-TOKEN
// @accept application/json
// @Produce application/json
// @Param data body request.CasbinInReceive true "权限id, 权限模型列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /casbin/getPolicyPathByRoleId [get]
func (s *SystemApiApi) GetPolicyPathByRoleId(rc *ctx.ReqCtx) {
roleKey := rc.GinCtx.Query("roleKey")
rc.ResData = casbin.GetPolicyPathByRoleId(roleKey)
}

112
apps/system/api/config.go Normal file
View File

@@ -0,0 +1,112 @@
package api
import (
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/utils"
)
type ConfigApi struct {
ConfigApp services2.SysConfigModel
}
// @Summary 配置列表数据
// @Description 获取JSON
// @Tags 配置
// @Param configName query string false "configName"
// @Param configKey query string false "configKey"
// @Param configType query string false "configType"
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/config [get]
// @Security
func (p *ConfigApi) GetConfigList(rc *ctx.ReqCtx) {
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
configName := rc.GinCtx.Query("configName")
configKey := rc.GinCtx.Query("configKey")
configType := rc.GinCtx.Query("configType")
config := entity2.SysConfig{ConfigName: configName, ConfigKey: configKey, ConfigType: configType}
list, total := p.ConfigApp.FindListPage(pageNum, pageSize, config)
rc.ResData = map[string]interface{}{
"data": list,
"total": total,
"pageNum": pageNum,
"pageSize": pageSize,
}
}
// @Summary 配置列表数据ByKey
// @Description 获取JSON
// @Tags 配置
// @Param configKey query string false "configKey"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/config/configKey [get]
// @Security
func (p *ConfigApi) GetConfigListByKey(rc *ctx.ReqCtx) {
configKey := rc.GinCtx.Query("configKey")
biz.IsTrue(configKey != "", "请传入配置Key")
rc.ResData = p.ConfigApp.FindList(entity2.SysConfig{ConfigKey: configKey})
}
// @Summary 获取配置
// @Description 获取JSON
// @Tags 配置
// @Param configId path int true "configId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/config/{configId} [get]
// @Security
func (p *ConfigApi) GetConfig(rc *ctx.ReqCtx) {
id := ginx.PathParamInt(rc.GinCtx, "configId")
p.ConfigApp.FindOne(int64(id))
}
// @Summary 添加配置
// @Description 获取JSON
// @Tags 配置
// @Accept application/json
// @Product application/json
// @Param data body entity.SysConfig true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/config [post]
// @Security X-TOKEN
func (p *ConfigApi) InsertConfig(rc *ctx.ReqCtx) {
var config entity2.SysConfig
ginx.BindJsonAndValid(rc.GinCtx, &config)
p.ConfigApp.Insert(config)
}
// @Summary 修改配置
// @Description 获取JSON
// @Tags 配置
// @Accept application/json
// @Product application/json
// @Param data body entity.SysConfig true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/config [put]
// @Security X-TOKEN
func (p *ConfigApi) UpdateConfig(rc *ctx.ReqCtx) {
var post entity2.SysConfig
ginx.BindJsonAndValid(rc.GinCtx, &post)
p.ConfigApp.Update(post)
}
// @Summary 删除配置
// @Description 删除数据
// @Tags 配置
// @Param configId path string true "configId 多个使用逗号分割"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/config/{configId} [delete]
func (p *ConfigApi) DeleteConfig(rc *ctx.ReqCtx) {
configId := rc.GinCtx.Param("configId")
p.ConfigApp.Delete(utils.IdsStrToIdsIntGroup(configId))
}

172
apps/system/api/dept.go Normal file
View File

@@ -0,0 +1,172 @@
package api
import (
"errors"
"fmt"
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
)
type DeptApi struct {
DeptApp services2.SysDeptModel
UserApp services2.SysUserModel
RoleApp services2.SysRoleModel
}
// @Summary 获取角色的部门树
// @Description 获取JSON
// @Tags 菜单
// @Param roleId path int false "roleId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": 400, "message": "抱歉未找到相关信息"}"
// @Router /system/menu/menuTreRoleSelect/{roleId} [get]
// @Security X-TOKEN
func (m *DeptApi) GetDeptTreeRoleSelect(rc *ctx.ReqCtx) {
roleId := ginx.PathParamInt(rc.GinCtx, "roleId")
result := m.DeptApp.SelectDeptLable(entity2.SysDept{})
deptIds := make([]int64, 0)
if roleId != 0 {
deptIds = m.RoleApp.GetRoleDeptId(entity2.SysRole{RoleId: int64(roleId)})
}
rc.ResData = map[string]interface{}{
"depts": result,
"checkedKeys": deptIds,
}
}
// @Summary 部门列表数据
// @Description 分页列表
// @Tags 部门
// @Param deptName query string false "deptName"
// @Param status query string false "status"
// @Param deptId query int false "deptId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dept/deptList [get]
// @Security
func (a *DeptApi) GetDeptList(rc *ctx.ReqCtx) {
//pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
//pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
deptName := rc.GinCtx.Query("deptName")
status := rc.GinCtx.Query("status")
deptId := ginx.QueryInt(rc.GinCtx, "deptId", 0)
dept := entity2.SysDept{DeptName: deptName, Status: status, DeptId: int64(deptId)}
if dept.DeptName == "" {
rc.ResData = a.DeptApp.SelectDept(dept)
} else {
rc.ResData = a.DeptApp.FindList(dept)
}
}
// @Summary 所有部门列表数据
// @Description 部门列表
// @Tags 部门
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dept/ordinaryDeptLis [get]
// @Security
func (a *DeptApi) GetOrdinaryDeptList(rc *ctx.ReqCtx) {
rc.ResData = a.DeptApp.FindList(entity2.SysDept{})
}
// @Summary 所有部门树数据
// @Description 部门树列表
// @Tags 部门
// @Param deptName query string false "deptName"
// @Param status query string false "status"
// @Param deptId query int false "deptId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dept/deptTree [get]
// @Security
func (a *DeptApi) GetDeptTree(rc *ctx.ReqCtx) {
deptName := rc.GinCtx.Query("deptName")
status := rc.GinCtx.Query("status")
deptId := ginx.QueryInt(rc.GinCtx, "deptId", 0)
dept := entity2.SysDept{DeptName: deptName, Status: status, DeptId: int64(deptId)}
rc.ResData = a.DeptApp.SelectDept(dept)
}
// @Summary 部门数据
// @Description 获取JSON
// @Tags 部门
// @Param deptId path string false "deptId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dept/{deptId} [get]
// @Security
func (a *DeptApi) GetDept(rc *ctx.ReqCtx) {
deptId := ginx.PathParamInt(rc.GinCtx, "deptId")
rc.ResData = a.DeptApp.FindOne(int64(deptId))
}
// @Summary 添加部门
// @Description 获取JSON
// @Tags 部门
// @Accept application/json
// @Product application/json
// @Param data body entity.SysDept true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/dept [post]
// @Security Bearer
func (a *DeptApi) InsertDept(rc *ctx.ReqCtx) {
var dept entity2.SysDept
g := rc.GinCtx
ginx.BindJsonAndValid(g, &dept)
dept.CreateBy = rc.LoginAccount.UserName
a.DeptApp.Insert(dept)
}
// @Summary 修改部门
// @Description 获取JSON
// @Tags 部门
// @Accept application/json
// @Product application/json
// @Param data body entity.SysDept true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /system/dept [put]
// @Security Bearer
func (a *DeptApi) UpdateDept(rc *ctx.ReqCtx) {
var dept entity2.SysDept
g := rc.GinCtx
ginx.BindJsonAndValid(g, &dept)
dept.UpdateBy = rc.LoginAccount.UserName
a.DeptApp.Update(dept)
}
// @Summary 删除部门
// @Description 删除数据
// @Tags 部门
// @Param deptId path string true "deptId, 逗号隔开"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/dept/{deptId} [delete]
func (a *DeptApi) DeleteDept(rc *ctx.ReqCtx) {
deptId := rc.GinCtx.Param("deptId")
deptIds := utils.IdsStrToIdsIntGroup(deptId)
deList := make([]int64, 0)
for _, id := range deptIds {
user := entity2.SysUser{}
user.DeptId = id
list := a.UserApp.FindList(user)
if len(*list) == 0 {
deList = append(deList, id)
} else {
global.Log.Info(fmt.Sprintf("dictId: %d 存在用户绑定无法删除", id))
}
}
if len(deList) == 0 {
biz.ErrIsNil(errors.New("所有部门都已绑定用户无法删除"), "所有部门都已绑定用户,无法删除")
}
a.DeptApp.Delete(deList)
}

234
apps/system/api/dict.go Normal file
View File

@@ -0,0 +1,234 @@
package api
import (
"fmt"
"github.com/kakuilan/kgo"
"os"
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/config"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
)
type DictApi struct {
DictType services2.SysDictTypeModel
DictData services2.SysDictDataModel
}
// @Summary 字典类型列表数据
// @Description 获取JSON
// @Tags 职位
// @Param dictName query string false "DictName"
// @Param dictName query string false "dictType"
// @Param status query string false "status"
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dict/type/list [get]
// @Security
func (p *DictApi) GetDictTypeList(rc *ctx.ReqCtx) {
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
status := rc.GinCtx.Query("status")
dictName := rc.GinCtx.Query("dictName")
dictType := rc.GinCtx.Query("dictType")
list, total := p.DictType.FindListPage(pageNum, pageSize, entity2.SysDictType{Status: status, DictName: dictName, DictType: dictType})
rc.ResData = map[string]interface{}{
"data": list,
"total": total,
"pageNum": pageNum,
"pageSize": pageSize,
}
}
// @Summary 获取字典类型
// @Description 获取JSON
// @Tags 字典
// @Param dictId path int true "dictId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dict/type/{dictId} [get]
// @Security
func (p *DictApi) GetDictType(rc *ctx.ReqCtx) {
dictId := ginx.PathParamInt(rc.GinCtx, "dictId")
p.DictType.FindOne(int64(dictId))
}
// @Summary 添加字典类型
// @Description 获取JSON
// @Tags 字典
// @Accept application/json
// @Product application/json
// @Param data body entity.SysDictType true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/dict/type [post]
// @Security X-TOKEN
func (p *DictApi) InsertDictType(rc *ctx.ReqCtx) {
var dict entity2.SysDictType
ginx.BindJsonAndValid(rc.GinCtx, &dict)
dict.CreateBy = rc.LoginAccount.UserName
p.DictType.Insert(dict)
}
// @Summary 修改字典类型
// @Description 获取JSON
// @Tags 职位
// @Accept application/json
// @Product application/json
// @Param data body entity.SysDictType true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/dict/type [put]
// @Security X-TOKEN
func (p *DictApi) UpdateDictType(rc *ctx.ReqCtx) {
var dict entity2.SysDictType
ginx.BindJsonAndValid(rc.GinCtx, &dict)
dict.CreateBy = rc.LoginAccount.UserName
p.DictType.Update(dict)
}
// @Summary 删除字典类型
// @Description 删除数据
// @Tags 字典
// @Param dictId path string true "dictId "
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/dict/type/{dictId} [delete]
func (p *DictApi) DeleteDictType(rc *ctx.ReqCtx) {
dictId := rc.GinCtx.Param("dictId")
dictIds := utils.IdsStrToIdsIntGroup(dictId)
deList := make([]int64, 0)
for _, id := range dictIds {
one := p.DictType.FindOne(id)
list := p.DictData.FindList(entity2.SysDictData{DictType: one.DictType})
if len(*list) == 0 {
deList = append(deList, id)
} else {
global.Log.Info(fmt.Sprintf("dictId: %d 存在字典数据绑定无法删除", id))
}
}
p.DictType.Delete(deList)
}
// @Summary 导出字典类型
// @Description 导出数据
// @Tags 字典
// @Param dictName query string false "DictName"
// @Param dictName query string false "dictType"
// @Param status query string false "status"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/dict/type/export [get]
func (p *DictApi) ExportDictType(rc *ctx.ReqCtx) {
status := rc.GinCtx.Query("status")
dictName := rc.GinCtx.Query("dictName")
dictType := rc.GinCtx.Query("dictType")
list := p.DictType.FindList(entity2.SysDictType{Status: status, DictName: dictName, DictType: dictType})
fileName := utils.GetFileName(config.Conf.Server.ExcelDir, "字典")
utils.InterfaceToExcel(*list, fileName)
line, err := kgo.KFile.ReadFile(fileName)
if err != nil {
os.Remove(fileName)
biz.ErrIsNil(err, "读取文件失败")
}
rc.Download(line, fileName)
}
// @Summary 字典数据列表
// @Description 获取JSON
// @Tags 字典
// @Param dictLabel query string false "dictLabel"
// @Param dictType query string false "dictType"
// @Param status query string false "status"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dict/data/list [get]
// @Security
func (p *DictApi) GetDictDataList(rc *ctx.ReqCtx) {
dictLabel := rc.GinCtx.Query("dictLabel")
dictType := rc.GinCtx.Query("dictType")
status := rc.GinCtx.Query("status")
rc.ResData = p.DictData.FindList(entity2.SysDictData{Status: status, DictType: dictType, DictLabel: dictLabel})
}
// @Summary 字典数据获取
// @Description 获取JSON
// @Tags 字典
// @Param dictType path string false "dictType"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dict/data/type [get]
// @Security
func (p *DictApi) GetDictDataListByDictType(rc *ctx.ReqCtx) {
dictType := rc.GinCtx.Query("dictType")
biz.IsTrue(dictType != "", "请传入字典类型")
rc.ResData = p.DictData.FindList(entity2.SysDictData{DictType: dictType})
}
// @Summary 获取字典数据
// @Description 获取JSON
// @Tags 字典
// @Param dictCode path int true "dictCode"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/dict/data/{dictCode} [get]
// @Security
func (p *DictApi) GetDictData(rc *ctx.ReqCtx) {
dictCode := ginx.PathParamInt(rc.GinCtx, "dictCode")
p.DictData.FindOne(int64(dictCode))
}
// @Summary 添加字典数据
// @Description 获取JSON
// @Tags 字典
// @Accept application/json
// @Product application/json
// @Param data body entity.SysDictData true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/dict/data [post]
// @Security X-TOKEN
func (p *DictApi) InsertDictData(rc *ctx.ReqCtx) {
var data entity2.SysDictData
ginx.BindJsonAndValid(rc.GinCtx, &data)
data.CreateBy = rc.LoginAccount.UserName
p.DictData.Insert(data)
}
// @Summary 修改字典数据
// @Description 获取JSON
// @Tags 字典
// @Accept application/json
// @Product application/json
// @Param data body entity.SysDictData true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/dict/data [put]
// @Security X-TOKEN
func (p *DictApi) UpdateDictData(rc *ctx.ReqCtx) {
var data entity2.SysDictData
ginx.BindJsonAndValid(rc.GinCtx, &data)
data.CreateBy = rc.LoginAccount.UserName
p.DictData.Update(data)
}
// @Summary 删除字典数据
// @Description 删除数据
// @Tags 字典
// @Param dictCode path string true "dictCode "
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/dict/data/{dictCode} [delete]
func (p *DictApi) DeleteDictData(rc *ctx.ReqCtx) {
param := rc.GinCtx.Param("dictCode")
p.DictData.Delete(utils.IdsStrToIdsIntGroup(param))
}

View File

@@ -0,0 +1,22 @@
package form
// 分配角色资源表单信息
type RoleResourceForm struct {
Id int
ResourceIds string
}
// 保存角色信息表单
type RoleForm struct {
Id int
Status int `json:"status"` // 1可用-1不可用
Name string `binding:"required"`
Code string `binding:"required"`
Remark string `json:"remark"`
}
// 账号分配角色表单
type AccountRoleForm struct {
Id int `binding:"required"`
RoleIds string
}

View File

@@ -0,0 +1,34 @@
package form
// User register structure
type Register struct {
Username string `json:"userName"`
Password string `json:"passWord"`
NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
AuthorityId string `json:"authorityId" gorm:"default:888"`
}
// User login structure
type Login struct {
Username string `json:"username"` // 用户名
Password string `json:"password"` // 密码
Captcha string `json:"captcha"` // 验证码
CaptchaId string `json:"captchaId"` // 验证码ID
}
// Modify password structure
type ChangePasswordStruct struct {
Username string `json:"username"` // 用户名
Password string `json:"password"` // 密码
NewPassword string `json:"newPassword"` // 新密码
}
type UserSearch struct {
Username string `json:"username"` // 用户UUID
NickName string `json:"nickName"` // 角色ID
Status int64 `json:"status"` // 角色ID
Phone string `json:"phone"` // 角色ID
PostId int64 `json:"postId"` // 角色ID
DeptId int64 `json:"deptId"` // 角色ID
}

168
apps/system/api/menu.go Normal file
View File

@@ -0,0 +1,168 @@
package api
import (
"log"
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/ctx"
"pandax/base/ginx"
)
type MenuApi struct {
MenuApp services2.SysMenuModel
DeptApp services2.SysDeptModel
RoleMenuApp services2.SysRoleMenuModel
RoleApp services2.SysRoleModel
}
// @Summary 获取菜单树
// @Description 获取JSON
// @Tags 菜单
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/menu/menuTreSelect [get]
// @Security X-TOKEN
func (m *MenuApi) GetMenuTreeSelect(rc *ctx.ReqCtx) {
lable := m.MenuApp.SelectMenuLable(entity2.SysMenu{})
rc.ResData = lable
}
// @Summary 登陆成功获取的路由,根据角色名称获取菜单列表数据(左菜单使用)
// @Description 获取JSON
// @Tags 菜单
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": 400, "message": "抱歉未找到相关信息"}"
// @Router /system/menu/menuRole [get]
// @Security X-TOKEN
func (m *MenuApi) GetMenuRole(rc *ctx.ReqCtx) {
rc.ResData = Build(*m.MenuApp.SelectMenuRole(rc.LoginAccount.RoleKey))
}
// @Summary 获取角色的菜单树
// @Description 获取JSON
// @Tags 菜单
// @Param roleId path int false "roleId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": 400, "message": "抱歉未找到相关信息"}"
// @Router /system/menu/menuTreRoleSelect/{roleId} [get]
// @Security X-TOKEN
func (m *MenuApi) GetMenuTreeRoleSelect(rc *ctx.ReqCtx) {
roleId := ginx.PathParamInt(rc.GinCtx, "roleId")
result := m.MenuApp.SelectMenuLable(entity2.SysMenu{})
menuIds := make([]int64, 0)
if roleId != 0 {
menuIds = m.RoleApp.GetRoleMeunId(entity2.SysRole{RoleId: int64(roleId)})
}
rc.ResData = map[string]interface{}{
"menus": result,
"checkedKeys": menuIds,
}
}
// @Summary 获取角色对应的菜单路径数组
// @Description 获取JSON
// @Tags 菜单
// @Param roleKey query string true "roleKey"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": 400, "message": "抱歉未找到相关信息"}"
// @Router /system/menu/menuPaths [get]
// @Security X-TOKEN
func (m *MenuApi) GetMenuPaths(rc *ctx.ReqCtx) {
roleKey := rc.GinCtx.Query("roleKey")
biz.IsTrue(roleKey != "", "请传入角色Key")
rc.ResData = m.RoleMenuApp.GetMenuPaths(entity2.SysRoleMenu{RoleName: roleKey})
}
// @Summary Menu列表数据
// @Description 获取JSON
// @Tags 菜单
// @Param menuName query string false "menuName"
// @Param IsHide query string false "IsHide"
// @Param title query string false "title"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": 400, "message": "抱歉未找到相关信息"}"
// @Router /system/menu/menuList [get]
// @Security Bearer
func (m *MenuApi) GetMenuList(rc *ctx.ReqCtx) {
menuName := rc.GinCtx.Query("menuName")
status := rc.GinCtx.Query("status")
menu := entity2.SysMenu{MenuName: menuName, Status: status}
log.Println(menuName)
if menu.MenuName == "" {
rc.ResData = m.MenuApp.SelectMenu(menu)
} else {
rc.ResData = m.MenuApp.FindList(menu)
}
}
// @Summary Menu列表数据
// @Description 获取JSON
// @Tags 菜单
// @Param menuId path string true "menuId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": 400, "message": "抱歉未找到相关信息"}"
// @Router /system/menu/{menuId} [get]
// @Security Bearer
func (m *MenuApi) GetMenu(rc *ctx.ReqCtx) {
menuId := ginx.PathParamInt(rc.GinCtx, "menuId")
rc.ResData = m.MenuApp.FindOne(int64(menuId))
}
// @Summary 创建菜单
// @Description 获取JSON
// @Tags 菜单
// @Param data body entity.SysMenu true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /system/menu [post]
// @Security X-TOKEN
func (m *MenuApi) InsertMenu(rc *ctx.ReqCtx) {
var menu entity2.SysMenu
ginx.BindJsonAndValid(rc.GinCtx, &menu)
menu.CreateBy = rc.LoginAccount.UserName
m.MenuApp.Insert(menu)
permis := m.RoleMenuApp.GetPermis(rc.LoginAccount.RoleId)
menus := m.MenuApp.SelectMenuRole(rc.LoginAccount.RoleKey)
rc.ResData = map[string]interface{}{
"permissions": permis,
"menus": Build(*menus),
}
}
// @Summary 修改菜单
// @Description 获取JSON
// @Tags 菜单
// @Param data body entity.SysMenu true "body"
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
// @Router /system/menu [put]
// @Security X-TOKEN
func (m *MenuApi) UpdateMenu(rc *ctx.ReqCtx) {
var menu entity2.SysMenu
ginx.BindJsonAndValid(rc.GinCtx, &menu)
menu.UpdateBy = rc.LoginAccount.UserName
m.MenuApp.Update(menu)
permis := m.RoleMenuApp.GetPermis(rc.LoginAccount.RoleId)
menus := m.MenuApp.SelectMenuRole(rc.LoginAccount.RoleKey)
rc.ResData = map[string]interface{}{
"permissions": permis,
"menus": Build(*menus),
}
}
// @Summary 删除菜单
// @Description 删除数据
// @Tags 菜单
// @Param menuId path int true "menuId"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/menu/{menuId} [delete]
func (m *MenuApi) DeleteMenu(rc *ctx.ReqCtx) {
menuId := ginx.PathParamInt(rc.GinCtx, "menuId")
m.MenuApp.Delete([]int64{int64(menuId)})
}

125
apps/system/api/post.go Normal file
View File

@@ -0,0 +1,125 @@
package api
import (
"errors"
"fmt"
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
)
type PostApi struct {
PostApp services2.SysPostModel
UserApp services2.SysUserModel
RoleApp services2.SysRoleModel
}
// @Summary 职位列表数据
// @Description 获取JSON
// @Tags 职位
// @Param postName query string false "postName"
// @Param postCode query string false "postCode"
// @Param status query string false "status"
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/post [get]
// @Security
func (p *PostApi) GetPostList(rc *ctx.ReqCtx) {
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
status := rc.GinCtx.Query("status")
postName := rc.GinCtx.Query("postName")
postCode := rc.GinCtx.Query("postCode")
post := entity2.SysPost{Status: status, PostName: postName, PostCode: postCode}
list, total := p.PostApp.FindListPage(pageNum, pageSize, post)
rc.ResData = map[string]interface{}{
"data": list,
"total": total,
"pageNum": pageNum,
"pageSize": pageSize,
}
}
// @Summary 获取职位
// @Description 获取JSON
// @Tags 职位
// @Param postId path int true "postId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/post/{postId} [get]
// @Security
func (p *PostApi) GetPost(rc *ctx.ReqCtx) {
postId := ginx.PathParamInt(rc.GinCtx, "postId")
p.PostApp.FindOne(int64(postId))
}
// @Summary 添加职位
// @Description 获取JSON
// @Tags 职位
// @Accept application/json
// @Product application/json
// @Param data body entity.SysPost true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/post [post]
// @Security X-TOKEN
func (p *PostApi) InsertPost(rc *ctx.ReqCtx) {
var post entity2.SysPost
ginx.BindJsonAndValid(rc.GinCtx, &post)
post.CreateBy = rc.LoginAccount.UserName
p.PostApp.Insert(post)
}
// @Summary 修改职位
// @Description 获取JSON
// @Tags 职位
// @Accept application/json
// @Product application/json
// @Param data body entity.SysPost true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/post [put]
// @Security X-TOKEN
func (p *PostApi) UpdatePost(rc *ctx.ReqCtx) {
var post entity2.SysPost
ginx.BindJsonAndValid(rc.GinCtx, &post)
post.CreateBy = rc.LoginAccount.UserName
p.PostApp.Update(post)
}
// @Summary 删除职位
// @Description 删除数据
// @Tags 职位
// @Param postId path string true "postId "
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/post/{postId} [delete]
func (p *PostApi) DeletePost(rc *ctx.ReqCtx) {
postId := rc.GinCtx.Param("postId")
postIds := utils.IdsStrToIdsIntGroup(postId)
deList := make([]int64, 0)
for _, id := range postIds {
user := entity2.SysUser{}
user.PostId = id
list := p.UserApp.FindList(user)
if len(*list) == 0 {
deList = append(deList, id)
} else {
global.Log.Info(fmt.Sprintf("dictId: %d 存在岗位绑定用户无法删除", id))
}
}
if len(deList) == 0 {
biz.ErrIsNil(errors.New("所有岗位都已绑定用户,无法删除"), "所有岗位都已绑定用户,无法删除")
}
p.PostApp.Delete(deList)
}

211
apps/system/api/role.go Normal file
View File

@@ -0,0 +1,211 @@
package api
import (
"errors"
"fmt"
"github.com/kakuilan/kgo"
"os"
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/casbin"
"pandax/base/config"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
)
type RoleApi struct {
RoleApp services2.SysRoleModel
UserApp services2.SysUserModel
RoleMenuApp services2.SysRoleMenuModel
RoleDeptApp services2.SysRoleDeptModel
}
// @Summary 角色列表数据
// @Description Get JSON
// @Tags 角色
// @Param roleName query string false "roleName"
// @Param status query string false "status"
// @Param roleKey query string false "roleKey"
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/role/rolelist [get]
// @Security
func (r *RoleApi) GetRoleList(rc *ctx.ReqCtx) {
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
status := rc.GinCtx.Query("status")
roleName := rc.GinCtx.Query("roleName")
roleKey := rc.GinCtx.Query("roleKey")
role := entity2.SysRole{Status: status, RoleName: roleName, RoleKey: roleKey}
list, total := r.RoleApp.FindListPage(pageNum, pageSize, role)
rc.ResData = map[string]interface{}{
"data": list,
"total": total,
"pageNum": pageNum,
"pageSize": pageSize,
}
}
// @Summary 获取Role数据
// @Description 获取JSON
// @Tags 角色/Role
// @Param roleId path string true "roleId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": 400, "message": "抱歉未找到相关信息"}"
// @Router /system/role [get]
// @Security X-TOKEN
func (r *RoleApi) GetRole(rc *ctx.ReqCtx) {
roleId := ginx.PathParamInt(rc.GinCtx, "roleId")
role := r.RoleApp.FindOne(int64(roleId))
role.MenuIds = r.RoleApp.GetRoleMeunId(entity2.SysRole{RoleId: int64(roleId)})
rc.ResData = role
}
// @Summary 创建角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body entity.SysRole true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/role [post]
func (r *RoleApi) InsertRole(rc *ctx.ReqCtx) {
var role entity2.SysRole
ginx.BindJsonAndValid(rc.GinCtx, &role)
role.CreateBy = rc.LoginAccount.UserName
insert := r.RoleApp.Insert(role)
role.RoleId = insert.RoleId
r.RoleMenuApp.Insert(insert.RoleId, role.MenuIds)
//添加权限
casbin.UpdateCasbin(role.RoleKey, role.ApiIds)
}
// @Summary 修改用户角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body entity.SysRole true "body"
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": 400, "message": "修改失败"}"
// @Router /system/role [put]
func (r *RoleApi) UpdateRole(rc *ctx.ReqCtx) {
var role entity2.SysRole
ginx.BindJsonAndValid(rc.GinCtx, &role)
role.UpdateBy = rc.LoginAccount.UserName
// 修改角色
r.RoleApp.Update(role)
// 删除角色的菜单绑定
r.RoleMenuApp.DeleteRoleMenu(role.RoleId)
// 添加角色菜单绑定
r.RoleMenuApp.Insert(role.RoleId, role.MenuIds)
//修改api权限
casbin.UpdateCasbin(role.RoleKey, role.ApiIds)
}
// @Summary 修改用户角色状态
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body entity.SysRole true "body"
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": 400, "message": "修改失败"}"
// @Router /system/role/changeStatus [put]
func (r *RoleApi) UpdateRoleStatus(rc *ctx.ReqCtx) {
var role entity2.SysRole
ginx.BindJsonAndValid(rc.GinCtx, &role)
role.UpdateBy = rc.LoginAccount.UserName
// 修改角色
r.RoleApp.Update(role)
}
// @Summary 修改用户角色部门
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body entity.SysRole true "body"
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": 400, "message": "修改失败"}"
// @Router /system/role/dataScope [put]
func (r *RoleApi) UpdateRoleDataScope(rc *ctx.ReqCtx) {
var role entity2.SysRole
ginx.BindJsonAndValid(rc.GinCtx, &role)
role.UpdateBy = rc.LoginAccount.UserName
// 修改角色
update := r.RoleApp.Update(role)
if role.DataScope == "2" {
// 删除角色的部门绑定
r.RoleDeptApp.Delete(entity2.SysRoleDept{RoleId: update.RoleId})
// 添加角色部门绑定
r.RoleDeptApp.Insert(role.RoleId, role.DeptIds)
}
}
// @Summary 删除用户角色
// @Description 删除数据
// @Tags 角色/Role
// @Param roleId path string true "roleId 多个用,分割"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/role/{roleId} [delete]
func (r *RoleApi) DeleteRole(rc *ctx.ReqCtx) {
roleId := rc.GinCtx.Param("roleId")
roleIds := utils.IdsStrToIdsIntGroup(roleId)
user := entity2.SysUser{}
delList := make([]int64, 0)
// 判断角色下面是否绑定用户
for _, rid := range roleIds {
user.RoleId = rid
role := r.RoleApp.FindOne(rid)
list := r.UserApp.FindList(user)
if len(*list) == 0 {
delList = append(delList, rid)
//删除角色绑定api
casbin.ClearCasbin(0, role.RoleKey)
} else {
global.Log.Info(fmt.Sprintf("role:%d 存在用户无法删除", rid))
}
}
if len(delList) == 0 {
biz.ErrIsNil(errors.New("所有角色都已绑定用户无法删除"), "所有角色都已绑定用户,无法删除")
}
r.RoleApp.Delete(delList)
r.RoleMenuApp.DeleteRoleMenus(delList)
}
// @Summary 导出角色
// @Description 导出数据
// @Tags 角色
// @Param roleName query string false "roleName"
// @Param status query string false "status"
// @Param roleKey query string false "roleKey"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/dict/type/export [get]
func (p *RoleApi) ExportRole(rc *ctx.ReqCtx) {
status := rc.GinCtx.Query("status")
roleName := rc.GinCtx.Query("roleName")
roleKey := rc.GinCtx.Query("roleKey")
list := p.RoleApp.FindList(entity2.SysRole{Status: status, RoleName: roleName, RoleKey: roleKey})
fileName := utils.GetFileName(config.Conf.Server.ExcelDir, "角色")
utils.InterfaceToExcel(*list, fileName)
line, err := kgo.KFile.ReadFile(fileName)
if err != nil {
os.Remove(fileName)
biz.ErrIsNil(err, "读取文件失败")
}
rc.Download(line, fileName)
}

84
apps/system/api/system.go Normal file
View File

@@ -0,0 +1,84 @@
package api
import (
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/kakuilan/kgo"
"net/http"
"pandax/base/biz"
"pandax/base/ctx"
"pandax/base/ws"
"runtime"
)
type System struct{}
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
func (s *System) ServerInfo(g *gin.Context) {
osDic := make(map[string]interface{}, 0)
osDic["goOs"] = runtime.GOOS
osDic["arch"] = runtime.GOARCH
osDic["mem"] = runtime.MemProfileRate
osDic["compiler"] = runtime.Compiler
osDic["version"] = runtime.Version()
osDic["numGoroutine"] = runtime.NumGoroutine()
used, free, total := kgo.KOS.DiskUsage("/")
diskDic := make(map[string]interface{}, 0)
diskDic["total"] = total / GB
diskDic["free"] = free / GB
diskDic["used"] = used / GB
diskDic["progress"] = int64((float64(used) / float64(total)) * 100)
used2, free2, total2 := kgo.KOS.MemoryUsage(true)
memDic := make(map[string]interface{}, 0)
memDic["total"] = total2 / GB
memDic["used"] = used2 / GB
memDic["free"] = free2 / GB
memDic["progress"] = int64((float64(used2) / float64(total2)) * 100)
cpuDic := make(map[string]interface{}, 0)
used3, idle, total3 := kgo.KOS.CpuUsage()
cpuDic["total"] = total3 / GB
cpuDic["used"] = used3 / GB
cpuDic["free"] = idle / GB
cpuDic["progress"] = int64((float64(used3) / float64(total3)) * 100)
g.JSON(http.StatusOK, gin.H{
"code": 200,
"os": osDic,
"mem": memDic,
"cpu": cpuDic,
"disk": diskDic,
})
}
// 连接websocket
func (s *System) ConnectWs(g *gin.Context) {
wsConn, err := ws.Upgrader.Upgrade(g.Writer, g.Request, nil)
defer func() {
if err := recover(); err != nil {
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
wsConn.Close()
}
}()
if err != nil {
panic(biz.NewBizErr("升级websocket失败"))
}
// 权限校验
rc := ctx.NewReqCtxWithGin(g)
if err = ctx.PermissionHandler(rc); err != nil {
panic(biz.NewBizErr("没有权限"))
}
// 登录账号信息
la := rc.LoginAccount
ws.Put(uint64(la.UserId), wsConn)
}

447
apps/system/api/user.go Normal file
View File

@@ -0,0 +1,447 @@
package api
import (
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/kakuilan/kgo"
"github.com/mssola/user_agent"
"net/http"
"os"
form2 "pandax/apps/system/api/form"
vo2 "pandax/apps/system/api/vo"
entity2 "pandax/apps/system/entity"
services2 "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/captcha"
"pandax/base/config"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
"strings"
"time"
)
type UserApi struct {
UserApp services2.SysUserModel
MenuApp services2.SysMenuModel
PostApp services2.SysPostModel
RoleApp services2.SysRoleModel
RoleMenuApp services2.SysRoleMenuModel
DeptApp services2.SysDeptModel
LogLogin services2.LogLoginModel
}
// @Tags Base
// @Summary 获取验证码
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
// @Router /system/user/getCaptcha [get]
func (u *UserApi) GenerateCaptcha(c *gin.Context) {
id, image := captcha.Generate()
c.JSON(http.StatusOK, map[string]interface{}{"base64Captcha": image, "captchaId": id})
}
// @Tags Base
// @Summary 刷新token
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
// @Router /system/user/refreshToken [get]
func (u *UserApi) RefreshToken(rc *ctx.ReqCtx) {
tokenStr := rc.GinCtx.Request.Header.Get("X-TOKEN")
token, err := ctx.RefreshToken(tokenStr)
biz.ErrIsNil(err, "刷新token失败")
rc.ResData = map[string]interface{}{"token": token, "expire": time.Now().Unix() + config.Conf.Jwt.ExpireTime}
}
// @Tags Base
// @Summary 用户登录
// @Produce application/json
// @Param data body form.Login true "用户名, 密码, 验证码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
// @Router /system/user/login [post]
func (u *UserApi) Login(rc *ctx.ReqCtx) {
var l form2.Login
ginx.BindJsonAndValid(rc.GinCtx, &l)
biz.IsTrue(captcha.Verify(l.CaptchaId, l.Captcha), "验证码认证失败")
login := u.UserApp.Login(entity2.Login{Username: l.Username, Password: l.Password})
role := u.RoleApp.FindOne(login.RoleId)
token, err := ctx.CreateToken(
ctx.Claims{
UserId: login.UserId,
UserName: login.Username,
RoleId: login.RoleId,
RoleKey: role.RoleKey,
DeptId: login.DeptId,
PostId: login.PostId,
StandardClaims: jwt.StandardClaims{
NotBefore: time.Now().Unix() - 1000, // 签名生效时间
ExpiresAt: time.Now().Unix() + config.Conf.Jwt.ExpireTime, // 过期时间 7天 配置文件
Issuer: "PandaX", // 签名的发行者
},
})
biz.ErrIsNil(err, "生成Token失败")
//前端权限
permis := u.RoleMenuApp.GetPermis(role.RoleId)
menus := u.MenuApp.SelectMenuRole(role.RoleKey)
rc.ResData = map[string]interface{}{
"user": login,
"permissions": permis,
"menus": Build(*menus),
"token": token,
"expire": time.Now().Unix() + config.Conf.Jwt.ExpireTime,
}
var loginLog entity2.LogLogin
ua := user_agent.New(rc.GinCtx.Request.UserAgent())
loginLog.Ipaddr = rc.GinCtx.ClientIP()
loginLog.LoginLocation = utils.GetRealAddressByIP(rc.GinCtx.ClientIP())
loginLog.LoginTime = time.Now()
loginLog.Status = "0"
loginLog.Remark = rc.GinCtx.Request.UserAgent()
browserName, browserVersion := ua.Browser()
loginLog.Browser = browserName + " " + browserVersion
loginLog.Os = ua.OS()
loginLog.Platform = ua.Platform()
loginLog.Username = login.Username
loginLog.Msg = "登录成功"
loginLog.CreateBy = login.Username
u.LogLogin.Insert(loginLog)
}
// @Tags Base
// @Summary 退出登录
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
// @Router /system/user/logout [post]
func (u *UserApi) LogOut(rc *ctx.ReqCtx) {
var loginLog entity2.LogLogin
ua := user_agent.New(rc.GinCtx.Request.UserAgent())
loginLog.Ipaddr = rc.GinCtx.ClientIP()
loginLog.LoginTime = time.Now()
loginLog.Status = "0"
loginLog.Remark = rc.GinCtx.Request.UserAgent()
browserName, browserVersion := ua.Browser()
loginLog.Browser = browserName + " " + browserVersion
loginLog.Os = ua.OS()
loginLog.Platform = ua.Platform()
loginLog.Username = rc.LoginAccount.UserName
loginLog.Msg = "退出成功"
u.LogLogin.Insert(loginLog)
}
// @Summary 列表数据
// @Description 获取JSON
// @Tags 用户
// @Param userName query string false "userName"
// @Param phone query string false "phone"
// @Param status query string false "status"
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /system/user/sysUserList [get]
// @Security X-TOKEN
func (u *UserApi) GetSysUserList(rc *ctx.ReqCtx) {
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
status := rc.GinCtx.Query("status")
userName := rc.GinCtx.Query("username")
phone := rc.GinCtx.Query("phone")
deptId := ginx.QueryInt(rc.GinCtx, "deptId", 0)
var user entity2.SysUser
user.Status = status
user.Username = userName
user.Phone = phone
user.DeptId = int64(deptId)
list, total := u.UserApp.FindListPage(pageNum, pageSize, user)
rc.ResData = map[string]interface{}{
"data": list,
"total": total,
"pageNum": pageNum,
"pageSize": pageSize,
}
}
// @Summary 获取当前登录用户
// @Description 获取JSON
// @Tags 个人中心
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/user/profile [get]
// @Security
func (u *UserApi) GetSysUserProfile(rc *ctx.ReqCtx) {
sysUser := entity2.SysUser{}
sysUser.UserId = rc.LoginAccount.UserId
user := u.UserApp.FindOne(sysUser)
//获取角色列表
roleList := u.RoleApp.FindList(entity2.SysRole{RoleId: rc.LoginAccount.RoleId})
//岗位列表
postList := u.PostApp.FindList(entity2.SysPost{PostId: rc.LoginAccount.PostId})
//获取部门列表
deptList := u.DeptApp.FindList(entity2.SysDept{DeptId: rc.LoginAccount.DeptId})
postIds := make([]int64, 0)
postIds = append(postIds, rc.LoginAccount.PostId)
roleIds := make([]int64, 0)
roleIds = append(roleIds, rc.LoginAccount.RoleId)
rc.ResData = map[string]interface{}{
"data": user,
"postIds": postIds,
"roleIds": roleIds,
"roles": roleList,
"posts": postList,
"dept": deptList,
}
}
// @Summary 修改头像
// @Description 修改头像
// @Tags 用户
// @Param file formData file true "file"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /system/user/profileAvatar [post]
func (u *UserApi) InsetSysUserAvatar(rc *ctx.ReqCtx) {
form, err := rc.GinCtx.MultipartForm()
biz.ErrIsNil(err, "头像上传失败")
files := form.File["upload[]"]
guid, _ := kgo.KStr.UuidV4()
filPath := "static/uploadfile/" + guid + ".jpg"
for _, file := range files {
global.Log.Info(file.Filename)
// 上传文件至指定目录
biz.ErrIsNil(rc.GinCtx.SaveUploadedFile(file, filPath), "保存头像失败")
}
sysuser := entity2.SysUser{}
sysuser.UserId = rc.LoginAccount.UserId
sysuser.Avatar = "/" + filPath
sysuser.UpdateBy = rc.LoginAccount.UserName
u.UserApp.Update(sysuser)
}
// @Summary 修改密码
// @Description 修改密码
// @Tags 用户
// @Param pwd body entity.SysUserPwd true "pwd"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /system/user/updatePwd [post]
func (u *UserApi) SysUserUpdatePwd(rc *ctx.ReqCtx) {
var pws entity2.SysUserPwd
ginx.BindJsonAndValid(rc.GinCtx, &pws)
user := entity2.SysUser{}
user.UserId = rc.LoginAccount.UserId
u.UserApp.SetPwd(user, pws)
}
// @Summary 获取用户
// @Description 获取JSON
// @Tags 用户
// @Param userId path int true "用户编码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /system/user/sysUser/{userId} [get]
// @Security
func (u *UserApi) GetSysUser(rc *ctx.ReqCtx) {
userId := ginx.PathParamInt(rc.GinCtx, "userId")
user := entity2.SysUser{}
user.UserId = int64(userId)
result := u.UserApp.FindOne(user)
roles := u.RoleApp.FindList(entity2.SysRole{})
posts := u.PostApp.FindList(entity2.SysPost{})
rc.ResData = map[string]interface{}{
"data": result,
"postIds": result.PostIds,
"roleIds": result.RoleIds,
"roles": roles,
"posts": posts,
}
}
// @Summary 获取添加用户角色和职位
// @Description 获取JSON
// @Tags 用户
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/user/getInit [get]
// @Security
func (u *UserApi) GetSysUserInit(rc *ctx.ReqCtx) {
roles := u.RoleApp.FindList(entity2.SysRole{})
posts := u.PostApp.FindList(entity2.SysPost{})
mp := make(map[string]interface{}, 2)
mp["roles"] = roles
mp["posts"] = posts
rc.ResData = mp
}
// @Summary 获取添加用户角色和职位
// @Description 获取JSON
// @Tags 用户
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/user/getInit [get]
// @Security
func (u *UserApi) GetUserRolePost(rc *ctx.ReqCtx) {
var user entity2.SysUser
user.UserId = rc.LoginAccount.UserId
resData := u.UserApp.FindOne(user)
roles := make([]entity2.SysRole, 0)
posts := make([]entity2.SysPost, 0)
for _, roleId := range strings.Split(resData.RoleIds, ",") {
ro := u.RoleApp.FindOne(kgo.KConv.Str2Int64(roleId))
roles = append(roles, *ro)
}
for _, postId := range strings.Split(resData.PostIds, ",") {
po := u.PostApp.FindOne(kgo.KConv.Str2Int64(postId))
posts = append(posts, *po)
}
mp := make(map[string]interface{}, 2)
mp["roles"] = roles
mp["posts"] = posts
rc.ResData = mp
}
// @Summary 创建用户
// @Description 获取JSON
// @Tags 用户
// @Accept application/json
// @Product application/json
// @Param data body entity.SysUser true "用户数据"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/user/sysUser [post]
func (u *UserApi) InsertSysUser(rc *ctx.ReqCtx) {
var sysUser entity2.SysUser
ginx.BindJsonAndValid(rc.GinCtx, &sysUser)
sysUser.CreateBy = rc.LoginAccount.UserName
u.UserApp.Insert(sysUser)
}
// @Summary 修改用户数据
// @Description 获取JSON
// @Tags 用户
// @Accept application/json
// @Product application/json
// @Param data body entity.SysUser true "用户数据"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/user/sysUser [put]
func (u *UserApi) UpdateSysUser(rc *ctx.ReqCtx) {
var sysUser entity2.SysUser
ginx.BindJsonAndValid(rc.GinCtx, &sysUser)
sysUser.CreateBy = rc.LoginAccount.UserName
u.UserApp.Update(sysUser)
}
// @Summary 修改用户状态
// @Description 获取JSON
// @Tags 用户
// @Accept application/json
// @Product application/json
// @Param data body entity.SysUser true "用户数据"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/user/sysUser [put]
func (u *UserApi) UpdateSysUserStu(rc *ctx.ReqCtx) {
var sysUser entity2.SysUser
ginx.BindJsonAndValid(rc.GinCtx, &sysUser)
sysUser.CreateBy = rc.LoginAccount.UserName
u.UserApp.Update(sysUser)
}
// @Summary 删除用户数据
// @Description 删除数据
// @Tags 用户
// @Param userId path int true "多个id 使用逗号隔开"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/user/sysuser/{userId} [delete]
func (u *UserApi) DeleteSysUser(rc *ctx.ReqCtx) {
userIds := rc.GinCtx.Param("userId")
us := utils.IdsStrToIdsIntGroup(userIds)
u.UserApp.Delete(us)
}
// @Summary 导出用户
// @Description 导出数据
// @Tags 用户
// @Param userName query string false "userName"
// @Param phone query string false "phone"
// @Param status query string false "status"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /system/dict/type/export [get]
func (u *UserApi) ExportUser(rc *ctx.ReqCtx) {
status := rc.GinCtx.Query("status")
userName := rc.GinCtx.Query("username")
phone := rc.GinCtx.Query("phone")
var user entity2.SysUser
user.Status = status
user.Username = userName
user.Phone = phone
list := u.UserApp.FindList(user)
fileName := utils.GetFileName(config.Conf.Server.ExcelDir, "用户")
utils.InterfaceToExcel(*list, fileName)
line, err := kgo.KFile.ReadFile(fileName)
if err != nil {
os.Remove(fileName)
biz.ErrIsNil(err, "读取文件失败")
}
rc.Download(line, fileName)
}
// 构建前端路由
func Build(menus []entity2.SysMenu) []vo2.RouterVo {
equals := func(a string, b string) bool {
if a == b {
return true
}
return false
}
if len(menus) == 0 {
}
rvs := make([]vo2.RouterVo, 0)
for _, ms := range menus {
var rv vo2.RouterVo
rv.Name = ms.Path
rv.Path = ms.Path
rv.Component = ms.Component
auth := make([]string, 0)
if ms.Permission != "" {
auth = strings.Split(ms.Permission, ",")
}
rv.Meta = vo2.MetaVo{
Title: ms.MenuName,
IsLink: ms.IsLink,
IsHide: equals("1", ms.IsHide),
IsKeepAlive: equals("0", ms.IsKeepAlive),
IsAffix: equals("0", ms.IsAffix),
IsFrame: equals("0", ms.IsFrame),
Auth: auth,
Icon: ms.Icon,
}
rv.Children = Build(ms.Children)
rvs = append(rvs, rv)
}
return rvs
}

View File

@@ -0,0 +1,25 @@
package vo
/**s
* 路由meta对象参数说明
* meta: {
* title: 菜单栏及 tagsView 栏、菜单搜索名称
* isLink 是否超链接菜单,开启外链条件,`1、isLink:true 2、链接地址不为空`
* isHide 是否隐藏此路由
* isKeepAlive 是否缓存组件状态
* isAffix 是否固定在 tagsView 栏上
* isFrame 是否内嵌窗口,,开启条件,`1、isFrame:true 2、链接地址不为空`
* auth 当前路由权限标识(多个请用逗号隔开),最后转成数组格式,用于与当前用户权限进行对比,控制路由显示、隐藏
* icon 菜单、tagsView 图标,阿里:加 `iconfont xxx`fontawesome加 `fa xxx`
* }
*/
type MetaVo struct {
Title string `json:"title"`
IsLink string `json:"isLink"`
IsHide bool `json:"isHide"`
IsKeepAlive bool `json:"isKeepAlive"`
IsAffix bool `json:"isAffix"`
IsFrame bool `json:"isFrame"`
Auth []string `json:"auth"`
Icon string `json:"icon"`
}

View File

@@ -0,0 +1,31 @@
package vo
/**
* 路由对象参数说明
* {
* component: 组件地址
* redirect: 重定向地址,当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
* path: 路由地址
* name: 路由名字
* // 路由meta对象参数说明
* meta: {
* title: 菜单栏及 tagsView 栏、菜单搜索名称(国际化)
* isLink 是否超链接菜单,开启外链条件,`1、isLink:true 2、链接地址不为空`
* isHide 是否隐藏此路由
* isKeepAlive 是否缓存组件状态
* isAffix 是否固定在 tagsView 栏上
* isFrame 是否内嵌窗口,,开启条件,`1、isIframe:true 2、链接地址不为空`
* auth 当前路由权限标识(多个请用逗号隔开),最后转成数组格式,用于与当前用户权限进行对比,控制路由显示、隐藏
* icon 菜单、tagsView 图标,阿里:加 `iconfont xxx`fontawesome加 `fa xxx`
* }
* }
*
*/
type RouterVo struct {
Name string `json:"name"`
Path string `json:"path"`
Redirect string `json:"redirect"`
Component string `json:"component"`
Meta MetaVo `json:"meta"`
Children []RouterVo `json:"children"`
}