mirror of
https://github.com/tiger1103/gfast.git
synced 2026-09-21 18:20:47 +00:00
接口修改
This commit is contained in:
@@ -5,13 +5,17 @@ import (
|
||||
"gfast/app/model/admin/auth_rule"
|
||||
"gfast/app/model/admin/role"
|
||||
"gfast/app/model/admin/user"
|
||||
"gfast/app/model/admin/user_post"
|
||||
"gfast/app/service/casbin_adapter_service"
|
||||
"gfast/library/utils"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
//获取isMenu==1菜单列表
|
||||
//获取MenuType==0,1菜单列表
|
||||
func GetIsMenuList() ([]*auth_rule.Entity, error) {
|
||||
list, err := GetMenuList()
|
||||
if err != nil {
|
||||
@@ -19,7 +23,7 @@ func GetIsMenuList() ([]*auth_rule.Entity, error) {
|
||||
}
|
||||
var gList = make([]*auth_rule.Entity, 0, len(list))
|
||||
for _, v := range list {
|
||||
if v.Ismenu == 1 {
|
||||
if v.MenuType == 0 || v.MenuType == 1 {
|
||||
gList = append(gList, v)
|
||||
}
|
||||
}
|
||||
@@ -34,7 +38,7 @@ func GetIsMenuStatusList() ([]*auth_rule.Entity, error) {
|
||||
}
|
||||
var gList = make([]*auth_rule.Entity, 0, len(list))
|
||||
for _, v := range list {
|
||||
if v.Ismenu == 1 && v.Status == 1 {
|
||||
if (v.MenuType == 0 || v.MenuType == 1) && v.Status == 1 {
|
||||
gList = append(gList, v)
|
||||
}
|
||||
}
|
||||
@@ -61,11 +65,35 @@ func GetMenuList() (list []*auth_rule.Entity, err error) {
|
||||
return auth_rule.GetMenuList()
|
||||
}
|
||||
|
||||
func GetMenuListSearch(req *auth_rule.ReqSearch) (list []*auth_rule.Entity, err error) {
|
||||
list, err = auth_rule.GetMenuList()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if req != nil {
|
||||
tmpList := make([]*auth_rule.Entity, 0, len(list))
|
||||
for _, entity := range list {
|
||||
if (req.Title == "" || gstr.Contains(gstr.ToUpper(entity.Title), gstr.ToUpper(req.Title))) &&
|
||||
(req.Status == "" || gconv.Uint(req.Status) == entity.Status) {
|
||||
tmpList = append(tmpList, entity)
|
||||
}
|
||||
}
|
||||
list = tmpList
|
||||
}
|
||||
g.Log().Debug(list)
|
||||
return
|
||||
}
|
||||
|
||||
//检查菜单规则是否存在
|
||||
func CheckMenuNameUnique(name string, id int) bool {
|
||||
return auth_rule.CheckMenuNameUnique(name, id)
|
||||
}
|
||||
|
||||
//检查菜单路由地址是否已经存在
|
||||
func CheckMenuPathUnique(path string, id int) bool {
|
||||
return auth_rule.CheckMenuPathUnique(path, id)
|
||||
}
|
||||
|
||||
// 添加菜单操作
|
||||
func AddMenu(req *auth_rule.MenuReq) (err error, insertId int64) {
|
||||
return auth_rule.Add(req)
|
||||
@@ -81,6 +109,10 @@ func GetRoleList() (list []*role.Entity, err error) {
|
||||
return role.GetList()
|
||||
}
|
||||
|
||||
func GetRoleListSearch(req *role.SelectPageReq) (total, page int, list []*role.Entity, err error) {
|
||||
return role.GetRoleListSearch(req)
|
||||
}
|
||||
|
||||
//保存角色信息并返回插入的id
|
||||
func AddRole(tx *gdb.TX, data map[string]interface{}) (InsId int64, err error) {
|
||||
return role.Add(tx, data)
|
||||
@@ -108,6 +140,14 @@ func EditRole(tx *gdb.TX, data map[string]interface{}) (err error) {
|
||||
return role.Edit(tx, data)
|
||||
}
|
||||
|
||||
func StatusSetRole(req *role.StatusSetReq) error {
|
||||
return role.StatusSetRole(req)
|
||||
}
|
||||
|
||||
func RoleDataScope(req *role.DataScopeReq) error {
|
||||
return role.DataScope(req)
|
||||
}
|
||||
|
||||
//修改角色的授权规则
|
||||
func EditRoleRule(iRule interface{}, roleId int64) (err error) {
|
||||
enforcer, e := casbin_adapter_service.GetEnforcer()
|
||||
@@ -141,21 +181,15 @@ func DeleteRoleRule(roleId int) (err error) {
|
||||
}
|
||||
|
||||
//添加管理员操作
|
||||
func AddUser(data map[string]interface{}) (InsertId int64, err error) {
|
||||
func AddUser(req *user.AddUserReq) (InsertId int64, err error) {
|
||||
//密码加密
|
||||
data["user_password"] = utils.EncryptCBC(gconv.String(data["user_password"]), utils.AdminCbcPublicKey)
|
||||
return user.Add(data)
|
||||
req.Password = utils.EncryptCBC(gconv.String(req.Password), utils.AdminCbcPublicKey)
|
||||
return user.Add(req)
|
||||
}
|
||||
|
||||
//修改用户信息
|
||||
func EditUser(data map[string]interface{}) (err error) {
|
||||
//提交了密码?密码加密
|
||||
if val, ok := data["user_password"]; ok && gconv.String(val) != "" {
|
||||
data["user_password"] = utils.EncryptCBC(gconv.String(data["user_password"]), utils.AdminCbcPublicKey)
|
||||
} else {
|
||||
delete(data, "user_password")
|
||||
}
|
||||
return user.Edit(data)
|
||||
func EditUser(req *user.EditUserReq) (err error) {
|
||||
return user.Edit(req)
|
||||
}
|
||||
|
||||
//添加用户角色信息
|
||||
@@ -175,6 +209,23 @@ func AddUserRole(roleIds interface{}, userId int64) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//添加用户岗位信息
|
||||
func AddUserPost(postIds []int64, userId int64) (err error) {
|
||||
//删除旧岗位信息
|
||||
err = user_post.DeleteByUserId(userId)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
err = gerror.New("设置用户岗位信息失败")
|
||||
}
|
||||
//添加用户岗位信息
|
||||
err = user_post.AddUserPost(postIds, userId)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
err = gerror.New("设置用户岗位信息失败")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//修改用户角色信息
|
||||
func EditUserRole(roleIds interface{}, userId int) (err error) {
|
||||
enforcer, e := casbin_adapter_service.GetEnforcer()
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package cms_service
|
||||
|
||||
import (
|
||||
"gfast/app/model/admin/cms_category"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
//获取频道列表
|
||||
func GetMenuListChannel() (list []*cms_category.Entity, err error) {
|
||||
//获取频道列表
|
||||
listAll, err := GetMenuList()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
list = make([]*cms_category.Entity, 0, len(listAll))
|
||||
for _, v := range listAll {
|
||||
if v.Status == 1 && v.CateType == cms_category.ChannelCateType {
|
||||
list = append(list, v)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//获取可发布文章栏目
|
||||
func GetPublishableMenuList(catId ...int) (list g.List, err error) {
|
||||
menuList, err := GetMenuList()
|
||||
var catIdMap map[int]int
|
||||
if len(catId) > 0 {
|
||||
catIdMap = make(map[int]int, len(catId))
|
||||
for _, v := range catId {
|
||||
catIdMap[v] = v
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
list = make(g.List, 0)
|
||||
for _, menu := range menuList {
|
||||
if menu.Status == 1 {
|
||||
mapMenu := gconv.Map(menu)
|
||||
//可发布栏目
|
||||
if menu.CateType == cms_category.PublishCateType {
|
||||
mapMenu["checkAble"] = true
|
||||
} else {
|
||||
mapMenu["checkAble"] = false
|
||||
}
|
||||
if _, ok := catIdMap[gconv.Int(menu.Id)]; ok {
|
||||
mapMenu["checked"] = true
|
||||
} else {
|
||||
mapMenu["checked"] = false
|
||||
}
|
||||
list = append(list, mapMenu)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//获取所有菜单列表
|
||||
func GetMenuList() (list []*cms_category.Entity, err error) {
|
||||
return cms_category.GetList()
|
||||
}
|
||||
|
||||
//保存栏目操作
|
||||
func AddSave(req *cms_category.ReqAdd) (id int64, err error) {
|
||||
return cms_category.AddSave(req)
|
||||
}
|
||||
|
||||
//修改栏目操作
|
||||
func EditSave(req *cms_category.ReqEdit) (id int64, err error) {
|
||||
return cms_category.EditSave(req)
|
||||
}
|
||||
|
||||
//获取搜索栏目结果
|
||||
func GetMenuListSearch(req *cms_category.ReqSearchList) (menus []*cms_category.Entity, err error) {
|
||||
return cms_category.GetListSearch(req)
|
||||
}
|
||||
|
||||
//根据栏目ID获取栏目信息
|
||||
func GetMenuInfoById(id int) (menu *cms_category.Entity, err error) {
|
||||
return cms_category.GetInfoById(id)
|
||||
}
|
||||
|
||||
func DeleteMenuByIds(ids []int) (err error) {
|
||||
return cms_category.DeleteByIds(ids)
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package cms_service
|
||||
|
||||
import (
|
||||
"gfast/app/model/admin/cms_category"
|
||||
"gfast/app/model/admin/cms_category_news"
|
||||
"gfast/app/model/admin/cms_news"
|
||||
"gfast/library/utils"
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
//添加文章操作
|
||||
func AddNews(req *cms_news.ReqAddParams, cateIds []int, userId int) (insId int64, err error) {
|
||||
return cms_news.AddNews(req, cateIds, userId)
|
||||
}
|
||||
|
||||
//修改文章操作
|
||||
func EditNews(req *cms_news.ReqEditParams, cateIds []int) (err error) {
|
||||
return cms_news.EditNews(req, cateIds)
|
||||
}
|
||||
|
||||
//文章列表查询
|
||||
func NewsListByPage(req *cms_news.ReqListSearchParams) (total, page int, list gdb.Result, err error) {
|
||||
var menuList []*cms_category.Entity
|
||||
//获取所有栏目
|
||||
menuList, err = GetMenuList()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(req.CateId) > 0 {
|
||||
//查询可发布栏目id
|
||||
menuListSlice := gconv.SliceMap(menuList)
|
||||
cateIds := req.CateId
|
||||
for _, cid := range cateIds {
|
||||
mList := make([]*cms_category.Entity, 0)
|
||||
sonList := utils.FindSonByParentId(menuListSlice, cid, "parent_id", "id")
|
||||
gconv.Structs(sonList, &mList)
|
||||
for _, v := range mList {
|
||||
if v.CateType == cms_category.PublishCateType {
|
||||
req.CateId = append(req.CateId, gconv.Int(v.Id))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
total, page, list, err = cms_news.ListByPage(req)
|
||||
if err != nil || len(list) == 0 {
|
||||
return
|
||||
}
|
||||
//匹配文章所属栏目
|
||||
var cateIds []int
|
||||
for _, v := range list {
|
||||
cateIds, err = GetCheckedCategoryIdByNewsId(gconv.Uint64(v["id"]))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cateNameList := make(map[int]string)
|
||||
for _, menu := range menuList {
|
||||
for _, cateId := range cateIds {
|
||||
if menu.Id == gconv.Uint64(cateId) {
|
||||
cateNameList[cateId] = menu.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
cateVal := new(gvar.Var)
|
||||
if len(cateNameList) > 0 {
|
||||
cateVal.Set(cateNameList)
|
||||
}
|
||||
v["cateList"] = cateVal
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//通过id获取文章信息
|
||||
func GetNewsById(id int) (news *cms_news.Entity, err error) {
|
||||
return cms_news.GetById(id)
|
||||
}
|
||||
|
||||
//通过文章id获取关联的栏目id
|
||||
func GetCheckedCategoryIdByNewsId(newsId uint64) (catIds []int, err error) {
|
||||
categories, err := GetCategoriesByNewsId(newsId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
catIds = make([]int, len(categories))
|
||||
for k, v := range categories {
|
||||
catIds[k] = gconv.Int(v.CategoryId)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//通过文章id获取关联栏目信息
|
||||
func GetCategoriesByNewsId(newsId uint64) (categories []*cms_category_news.Entity, err error) {
|
||||
return cms_category_news.GetCategoriesByNewsId(newsId)
|
||||
}
|
||||
|
||||
func DeleteCmsByIds(ids []int) (err error) {
|
||||
return cms_news.DeleteByIds(ids)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package dept_service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"gfast/app/model/admin/role_dept"
|
||||
"gfast/app/model/admin/sys_dept"
|
||||
)
|
||||
|
||||
/**
|
||||
获取列表数据
|
||||
*/
|
||||
func GetList(searchParams *sys_dept.SearchParams) ([]*sys_dept.Dept, error) {
|
||||
if list, err := sys_dept.GetList(searchParams); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return list, nil
|
||||
}
|
||||
}
|
||||
|
||||
func GetRoleDepts(roleId int64) ([]int64, error) {
|
||||
return role_dept.GetRoleDepts(roleId)
|
||||
}
|
||||
|
||||
func AddDept(data *sys_dept.AddParams) (sql.Result, error) {
|
||||
return sys_dept.AddDept(data)
|
||||
}
|
||||
|
||||
func EditDept(data *sys_dept.EditParams) error {
|
||||
return sys_dept.EditDept(data)
|
||||
}
|
||||
|
||||
func GetDeptById(id int64) (*sys_dept.Dept, error) {
|
||||
return sys_dept.GetDeptById(id)
|
||||
}
|
||||
|
||||
/**
|
||||
查询部门排除节点
|
||||
*/
|
||||
func Exclude(id int64) ([]*sys_dept.Dept, error) {
|
||||
return sys_dept.Exclude(id)
|
||||
}
|
||||
|
||||
/**
|
||||
删除
|
||||
*/
|
||||
func DelDept(id int64) error {
|
||||
return sys_dept.DelDept(id)
|
||||
}
|
||||
@@ -2,7 +2,10 @@ package dict_service
|
||||
|
||||
import (
|
||||
"gfast/app/model/admin/sys_dict_type"
|
||||
"gfast/app/service/cache_service"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
//检查字典类型是否唯一
|
||||
@@ -44,3 +47,22 @@ func GetDictWithDataByType(dictType, defaultValue, emptyLabel string) (dict g.Ma
|
||||
func DeleteDictByIds(ids []int) error {
|
||||
return sys_dict_type.DeleteDictByIds(ids)
|
||||
}
|
||||
|
||||
func GetAllDictType() (list []*sys_dict_type.Entity, err error) {
|
||||
cache := cache_service.New()
|
||||
//从缓存获取
|
||||
data := cache.Get(gconv.String(cache_service.AdminConfigDict) + "_dict_type_all")
|
||||
if data != nil {
|
||||
list = data.([]*sys_dict_type.Entity)
|
||||
return
|
||||
}
|
||||
list, err = sys_dict_type.GetAllDictType()
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
err = gerror.New("获取字典类型数据出错")
|
||||
return
|
||||
}
|
||||
//缓存
|
||||
cache.Set(gconv.String(cache_service.AdminConfigDict)+"_dict_type_all", list, 0, cache_service.AdminSysConfigTag)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,498 @@
|
||||
package gen_service
|
||||
|
||||
import (
|
||||
"gfast/app/model/admin/gen_table"
|
||||
"gfast/app/model/admin/gen_table_column"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//根据条件分页查询数据
|
||||
func SelectDbTableList(param *gen_table.SelectPageReq) (total int, list []*gen_table.Entity, err error) {
|
||||
return gen_table.SelectDbTableList(param)
|
||||
}
|
||||
|
||||
//根据条件分页查询数据
|
||||
func SelectListByPage(param *gen_table.SelectPageReq) (total int, list []*gen_table.Entity, err error) {
|
||||
return gen_table.SelectListByPage(param)
|
||||
}
|
||||
|
||||
//查询据库列表
|
||||
func SelectDbTableListByNames(tableNames []string) ([]*gen_table.Entity, error) {
|
||||
return gen_table.SelectDbTableListByNames(tableNames)
|
||||
}
|
||||
|
||||
//导入表结构
|
||||
func ImportGenTable(tableList []*gen_table.Entity, operName string) error {
|
||||
if tableList != nil && operName != "" {
|
||||
tx, err := g.DB().Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, table := range tableList {
|
||||
tableName := table.TableName
|
||||
InitTable(table, operName)
|
||||
result, err := tx.Table(gen_table.Table).Insert(table)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tmpid, err := result.LastInsertId()
|
||||
|
||||
if err != nil || tmpid <= 0 {
|
||||
tx.Rollback()
|
||||
return gerror.New("保存数据失败")
|
||||
}
|
||||
|
||||
table.TableId = tmpid
|
||||
|
||||
// 保存列信息
|
||||
genTableColumns, err := gen_table_column.SelectDbTableColumnsByName(tableName)
|
||||
|
||||
if err != nil || len(genTableColumns) <= 0 {
|
||||
tx.Rollback()
|
||||
return gerror.New("获取列数据失败")
|
||||
}
|
||||
|
||||
for _, column := range genTableColumns {
|
||||
InitColumnField(column, table)
|
||||
_, err = tx.Table("gen_table_column").Insert(column)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return gerror.New("保存列数据失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
} else {
|
||||
return gerror.New("参数错误")
|
||||
}
|
||||
}
|
||||
|
||||
//获取数据库类型字段
|
||||
func GetDbType(columnType string) string {
|
||||
if strings.Index(columnType, "(") > 0 {
|
||||
return columnType[0:strings.Index(columnType, "(")]
|
||||
} else {
|
||||
return columnType
|
||||
}
|
||||
}
|
||||
|
||||
//将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
|
||||
func ConvertToCamelCase(name string) string {
|
||||
if name == "" {
|
||||
return ""
|
||||
} else if !strings.Contains(name, "_") {
|
||||
// 不含下划线,仅将首字母大写
|
||||
return strings.ToUpper(name[0:1]) + name[1:len(name)]
|
||||
}
|
||||
var result string = ""
|
||||
camels := strings.Split(name, "_")
|
||||
for index := range camels {
|
||||
if camels[index] == "" {
|
||||
continue
|
||||
}
|
||||
camel := camels[index]
|
||||
result = result + strings.ToUpper(camel[0:1]) + strings.ToLower(camel[1:len(camel)])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
////将下划线大写方式命名的字符串转换为驼峰式,首字母小写。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->helloWorld
|
||||
func ConvertToCamelCase1(name string) string {
|
||||
if name == "" {
|
||||
return ""
|
||||
} else if !strings.Contains(name, "_") {
|
||||
// 不含下划线,原值返回
|
||||
return name
|
||||
}
|
||||
var result string = ""
|
||||
camels := strings.Split(name, "_")
|
||||
for index := range camels {
|
||||
if camels[index] == "" {
|
||||
continue
|
||||
}
|
||||
camel := camels[index]
|
||||
if result == "" {
|
||||
result = strings.ToLower(camel[0:1]) + strings.ToLower(camel[1:len(camel)])
|
||||
} else {
|
||||
result = result + strings.ToUpper(camel[0:1]) + strings.ToLower(camel[1:len(camel)])
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
//获取字段长度
|
||||
func GetColumnLength(columnType string) int {
|
||||
start := strings.Index(columnType, "(")
|
||||
end := strings.Index(columnType, ")")
|
||||
result := ""
|
||||
if start >= 0 && end >= 0 {
|
||||
result = columnType[start+1 : end-1]
|
||||
}
|
||||
return gconv.Int(result)
|
||||
}
|
||||
|
||||
//初始化列属性字段
|
||||
func InitColumnField(column *gen_table_column.Entity, table *gen_table.Entity) {
|
||||
dataType := GetDbType(column.ColumnType)
|
||||
columnName := column.ColumnName
|
||||
column.TableId = table.TableId
|
||||
column.CreateBy = table.CreateBy
|
||||
//设置字段名
|
||||
column.GoField = ConvertToCamelCase(columnName)
|
||||
column.HtmlField = ConvertToCamelCase1(columnName)
|
||||
|
||||
if gen_table_column.IsStringObject(dataType) {
|
||||
//字段为字符串类型
|
||||
column.GoType = "string"
|
||||
columnLength := GetColumnLength(column.ColumnType)
|
||||
if columnLength >= 500 {
|
||||
column.HtmlType = "textarea"
|
||||
} else {
|
||||
column.HtmlType = "input"
|
||||
}
|
||||
} else if gen_table_column.IsTimeObject(dataType) {
|
||||
//字段为时间类型
|
||||
column.GoType = "Time"
|
||||
column.HtmlType = "datatime"
|
||||
} else if gen_table_column.IsNumberObject(dataType) {
|
||||
//字段为数字类型
|
||||
column.HtmlType = "input"
|
||||
// 如果是浮点型
|
||||
tmp := column.ColumnType
|
||||
start := strings.Index(tmp, "(")
|
||||
end := strings.Index(tmp, ")")
|
||||
result := "0"
|
||||
if start > 0 && end > 0 {
|
||||
result = tmp[start+1 : end]
|
||||
}
|
||||
arr := strings.Split(result, ",")
|
||||
if len(arr) == 2 && gconv.Int(arr[1]) > 0 {
|
||||
column.GoType = "float64"
|
||||
} else if len(arr) == 1 && gconv.Int(arr[0]) <= 10 {
|
||||
column.GoType = "int"
|
||||
} else {
|
||||
column.GoType = "int64"
|
||||
}
|
||||
}
|
||||
//新增字段
|
||||
if columnName == "create_by" || columnName == "create_time" || columnName == "update_by" || columnName == "update_time" {
|
||||
column.IsRequired = "0"
|
||||
column.IsInsert = "0"
|
||||
} else {
|
||||
column.IsRequired = "0"
|
||||
column.IsInsert = "1"
|
||||
if strings.Index(columnName, "name") >= 0 || strings.Index(columnName, "status") >= 0 {
|
||||
column.IsRequired = "1"
|
||||
}
|
||||
}
|
||||
|
||||
// 编辑字段
|
||||
if gen_table_column.IsNotEdit(columnName) {
|
||||
if column.IsPk == "1" {
|
||||
column.IsEdit = "0"
|
||||
} else {
|
||||
column.IsEdit = "1"
|
||||
}
|
||||
} else {
|
||||
column.IsEdit = "0"
|
||||
}
|
||||
// 列表字段
|
||||
if gen_table_column.IsNotList(columnName) {
|
||||
column.IsList = "1"
|
||||
} else {
|
||||
column.IsList = "0"
|
||||
}
|
||||
// 查询字段
|
||||
if gen_table_column.IsNotQuery(columnName) {
|
||||
column.IsQuery = "1"
|
||||
} else {
|
||||
column.IsQuery = "0"
|
||||
}
|
||||
|
||||
// 查询字段类型
|
||||
if CheckNameColumn(columnName) {
|
||||
column.QueryType = "LIKE"
|
||||
} else {
|
||||
column.QueryType = "EQ"
|
||||
}
|
||||
|
||||
// 状态字段设置单选框
|
||||
if CheckStatusColumn(columnName) {
|
||||
column.HtmlType = "radio"
|
||||
} else if CheckTypeColumn(columnName) || CheckSexColumn(columnName) {
|
||||
// 类型&性别字段设置下拉框
|
||||
column.HtmlType = "select"
|
||||
}
|
||||
}
|
||||
|
||||
//检查字段名后3位是否是sex
|
||||
func CheckSexColumn(columnName string) bool {
|
||||
if len(columnName) >= 3 {
|
||||
end := len(columnName)
|
||||
start := end - 3
|
||||
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
|
||||
if columnName[start:end] == "sex" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//检查字段名后4位是否是type
|
||||
func CheckTypeColumn(columnName string) bool {
|
||||
if len(columnName) >= 4 {
|
||||
end := len(columnName)
|
||||
start := end - 4
|
||||
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
|
||||
if columnName[start:end] == "type" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//检查字段名后6位是否是status
|
||||
func CheckStatusColumn(columnName string) bool {
|
||||
if len(columnName) >= 6 {
|
||||
end := len(columnName)
|
||||
start := end - 6
|
||||
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
tmp := columnName[start:end]
|
||||
|
||||
if tmp == "status" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
//检查字段名后4位是否是name
|
||||
func CheckNameColumn(columnName string) bool {
|
||||
if len(columnName) >= 4 {
|
||||
end := len(columnName)
|
||||
start := end - 4
|
||||
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
|
||||
tmp := columnName[start:end]
|
||||
|
||||
if tmp == "name" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//初始化表信息
|
||||
func InitTable(table *gen_table.Entity, operName string) {
|
||||
table.ClassName = ConvertClassName(table.TableName)
|
||||
table.PackageName = g.Cfg().GetString("gen.packageName")
|
||||
table.ModuleName = g.Cfg().GetString("gen.moduleName")
|
||||
table.BusinessName = GetBusinessName(table.TableName)
|
||||
table.FunctionName = strings.ReplaceAll(table.TableComment, "表", "")
|
||||
table.FunctionAuthor = g.Cfg().GetString("gen.author")
|
||||
table.CreateBy = operName
|
||||
table.TplCategory = "crud"
|
||||
table.CreateTime = gtime.Now()
|
||||
}
|
||||
|
||||
//表名转换成类名
|
||||
func ConvertClassName(tableName string) string {
|
||||
autoRemovePre := g.Cfg().GetBool("gen.autoRemovePre")
|
||||
tablePrefix := g.Cfg().GetString("gen.tablePrefix")
|
||||
if autoRemovePre && tablePrefix != "" {
|
||||
searchList := strings.Split(tablePrefix, ",")
|
||||
for _, str := range searchList {
|
||||
tableName = strings.ReplaceAll(tableName, str, "")
|
||||
}
|
||||
}
|
||||
return tableName
|
||||
}
|
||||
|
||||
//获取业务名
|
||||
func GetBusinessName(tableName string) string {
|
||||
lastIndex := strings.LastIndex(tableName, "_")
|
||||
nameLength := len(tableName)
|
||||
businessName := tableName[lastIndex+1 : nameLength]
|
||||
return businessName
|
||||
}
|
||||
|
||||
//根据table_id查询表列数据
|
||||
func SelectGenTableColumnListByTableId(tableId int64) ([]*gen_table_column.Entity, error) {
|
||||
return gen_table_column.SelectGenTableColumnListByTableId(tableId)
|
||||
}
|
||||
|
||||
func GetTableInfoByTableId(tableId int64) (info *gen_table.Entity, err error) {
|
||||
return gen_table.GetInfoById(tableId)
|
||||
}
|
||||
|
||||
//修改表和列信息
|
||||
func SaveEdit(req *gen_table.EditReq) (err error) {
|
||||
if req == nil {
|
||||
err = gerror.New("参数错误")
|
||||
return
|
||||
}
|
||||
table, err := gen_table.FindOne("table_id=?", req.TableId)
|
||||
if err != nil || table == nil {
|
||||
err = gerror.New("数据不存在")
|
||||
return
|
||||
}
|
||||
if req.TableName != "" {
|
||||
table.TableName = req.TableName
|
||||
}
|
||||
if req.TableComment != "" {
|
||||
table.TableComment = req.TableComment
|
||||
}
|
||||
if req.BusinessName != "" {
|
||||
table.BusinessName = req.BusinessName
|
||||
}
|
||||
if req.ClassName != "" {
|
||||
table.ClassName = req.ClassName
|
||||
}
|
||||
if req.FunctionAuthor != "" {
|
||||
table.FunctionAuthor = req.FunctionAuthor
|
||||
}
|
||||
if req.FunctionName != "" {
|
||||
table.FunctionName = req.FunctionName
|
||||
}
|
||||
if req.ModuleName != "" {
|
||||
table.ModuleName = req.ModuleName
|
||||
}
|
||||
if req.PackageName != "" {
|
||||
table.PackageName = req.PackageName
|
||||
}
|
||||
if req.Remark != "" {
|
||||
table.Remark = req.Remark
|
||||
}
|
||||
if req.TplCategory != "" {
|
||||
table.TplCategory = req.TplCategory
|
||||
}
|
||||
if req.Params != "" {
|
||||
table.Options = req.Params
|
||||
}
|
||||
table.UpdateTime = gtime.Now()
|
||||
table.UpdateBy = req.UserName
|
||||
if req.TplCategory == "tree" {
|
||||
//树表设置options
|
||||
options := g.Map{
|
||||
"tree_code": req.TreeCode,
|
||||
"tree_parent_code": req.TreeParentCode,
|
||||
"tree_name": req.TreeName,
|
||||
}
|
||||
table.Options = gconv.String(options)
|
||||
} else {
|
||||
table.Options = ""
|
||||
}
|
||||
|
||||
var tx *gdb.TX
|
||||
tx, err = g.DB().Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = tx.Table(gen_table.Table).Save(table)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
//保存列数据
|
||||
if req.Columns != "" {
|
||||
var j *gjson.Json
|
||||
if j, err = gjson.DecodeToJson([]byte(req.Columns)); err != nil {
|
||||
tx.Rollback()
|
||||
return
|
||||
} else {
|
||||
var columnList []gen_table_column.Entity
|
||||
err = j.ToStructs(&columnList)
|
||||
if err == nil && columnList != nil && len(columnList) > 0 {
|
||||
for _, column := range columnList {
|
||||
if column.ColumnId > 0 {
|
||||
tmp, _ := gen_table_column.FindOne("column_id=?", column.ColumnId)
|
||||
if tmp != nil {
|
||||
tmp.ColumnComment = column.ColumnComment
|
||||
tmp.GoType = column.GoType
|
||||
tmp.HtmlType = column.HtmlType
|
||||
tmp.QueryType = column.QueryType
|
||||
tmp.GoField = column.GoField
|
||||
tmp.DictType = column.DictType
|
||||
tmp.IsInsert = column.IsInsert
|
||||
tmp.IsEdit = column.IsEdit
|
||||
tmp.IsList = column.IsList
|
||||
tmp.IsQuery = column.IsQuery
|
||||
_, err = tx.Table(gen_table_column.Table).Save(tmp)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
//删除表格
|
||||
func Delete(ids []int) error {
|
||||
tx, err := g.DB().Begin()
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
return gerror.New("开启删除事务出错")
|
||||
}
|
||||
_, err = tx.Table(gen_table.Table).Where(gen_table.Columns.TableId+" in(?)", ids).Delete()
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
tx.Rollback()
|
||||
return gerror.New("删除表格数据失败")
|
||||
}
|
||||
_, err = tx.Table(gen_table_column.Table).Where(gen_table_column.Columns.TableId+" in(?)", ids).Delete()
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
tx.Rollback()
|
||||
return gerror.New("删除表格字段数据失败")
|
||||
}
|
||||
tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
||||
func SelectRecordById(tableId int64) (entity *gen_table.EntityExtend, err error) {
|
||||
entity, err = gen_table.SelectRecordById(tableId)
|
||||
return
|
||||
}
|
||||
|
||||
//设置主键列信息
|
||||
func SetPkColumn(table *gen_table.EntityExtend, columns []*gen_table_column.Entity) {
|
||||
for _, column := range columns {
|
||||
if column.IsPk == "1" {
|
||||
table.PkColumn = column
|
||||
break
|
||||
}
|
||||
}
|
||||
if table.PkColumn == nil {
|
||||
table.PkColumn = columns[0]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package post_service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"gfast/app/model/admin/sys_post"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
)
|
||||
|
||||
func List(req *sys_post.SearchParams) (total, page int, list gdb.Result, err error) {
|
||||
return sys_post.List(req)
|
||||
}
|
||||
|
||||
//获取正常状态的岗位
|
||||
func GetUsedPost() (list []*sys_post.Entity, err error) {
|
||||
return sys_post.GetUsedPost()
|
||||
}
|
||||
|
||||
func Add(addParams *sys_post.AddParams) (result sql.Result, err error) {
|
||||
return sys_post.Add(addParams)
|
||||
}
|
||||
|
||||
func Edit(editParams *sys_post.EditParams) (result sql.Result, err error) {
|
||||
return sys_post.Edit(editParams)
|
||||
}
|
||||
|
||||
func GetOneById(id int64) (*sys_post.Entity, error) {
|
||||
return sys_post.GetOneById(id)
|
||||
}
|
||||
|
||||
func Delete(ids []int) error {
|
||||
return sys_post.DeleteByIds(ids)
|
||||
}
|
||||
@@ -70,6 +70,7 @@ func upByType(file *ghttp.UploadFile, fType string) (fileInfo *FileInfo, err err
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//检测文件类型
|
||||
rightType := checkFileType(file.Filename, config.ConfigValue)
|
||||
if !rightType {
|
||||
@@ -206,7 +207,7 @@ func getUpConfig(key string) (config *sys_config.Entity, err error) {
|
||||
|
||||
//判断上传文件类型是否合法
|
||||
func checkFileType(fileName, typeString string) bool {
|
||||
suffix := gstr.SubStr(fileName, gstr.SearchArray(gstr.Split(fileName, ""), ".")+1)
|
||||
suffix := gstr.SubStrRune(fileName, gstr.PosRRune(fileName, ".")+1, gstr.LenRune(fileName)-1)
|
||||
imageType := gstr.Split(typeString, ",")
|
||||
rightType := false
|
||||
for _, v := range imageType {
|
||||
|
||||
@@ -1,20 +1,113 @@
|
||||
package user_service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gfast/app/model/admin/auth_rule"
|
||||
"gfast/app/model/admin/role"
|
||||
"gfast/app/model/admin/sys_dept"
|
||||
"gfast/app/model/admin/sys_post"
|
||||
"gfast/app/model/admin/user"
|
||||
"gfast/app/model/admin/user_post"
|
||||
"gfast/app/service/admin/auth_service"
|
||||
"gfast/app/service/casbin_adapter_service"
|
||||
"gfast/boot"
|
||||
"gfast/library/service"
|
||||
"gfast/library/utils"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
type EditParams struct {
|
||||
Id int `p:"id" v:"required#用户id不能为空"`
|
||||
UserNickname string `p:"user_nickname" v:"required#用户昵称不能为空" orm:"user_nickname"` // 用户昵称
|
||||
Mobile string `p:"mobile" v:"required|phone#手机号不能为空|手机号格式错误" orm:"mobile,unique"`
|
||||
UserEmail string `p:"user_email" v:"email#邮箱格式错误" orm:"user_email"`
|
||||
Sex int `p:"sex" orm:"sex"`
|
||||
}
|
||||
|
||||
type UpdatePwdReq struct {
|
||||
OldPassword string `p:"oldPassword" v:"required#旧密码不能为空"`
|
||||
NewPassword string `p:"newPassword" v:"required#新密码不能为空"`
|
||||
}
|
||||
|
||||
/**
|
||||
修改密码
|
||||
*/
|
||||
func UpdatePwd(r *ghttp.Request, data *UpdatePwdReq) error {
|
||||
|
||||
currentUser, err := GetCurrentUserInfo(r)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
OldPassword := utils.EncryptCBC(gconv.String(data.OldPassword), utils.AdminCbcPublicKey)
|
||||
|
||||
if OldPassword != currentUser["user_password"].(string) {
|
||||
return errors.New("原始密码错误!")
|
||||
}
|
||||
|
||||
return ResetUserPwd(&user.ResetPwdReq{
|
||||
Id: currentUser["id"].(int),
|
||||
Password: data.NewPassword,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
用户中心修改用户信息
|
||||
*/
|
||||
func Edit(info *EditParams) (sql.Result, error) {
|
||||
return user.Model.Where("id", info.Id).Data(info).Update()
|
||||
}
|
||||
|
||||
// 获取单前登录用户的信息
|
||||
func GetCurrentUserInfo(r *ghttp.Request) (map[string]interface{}, error) {
|
||||
id := GetLoginID(r)
|
||||
userEntity, err := user.GetUserById(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userInfo := gconv.Map(userEntity)
|
||||
//delete(userInfo, "user_password")
|
||||
userInfo["roles"] = make([]string, 0)
|
||||
userInfo["posts"] = new([]*user_post.Entity)
|
||||
userInfo["dept_info"] = nil
|
||||
allRoles, err := auth_service.GetRoleList()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roles, err := GetAdminRole(userEntity.Id, allRoles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//角色
|
||||
userInfo["roles"] = roles
|
||||
//岗位
|
||||
posts, err := GetPostsByUserId(userEntity.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userInfo["posts"] = posts
|
||||
//部门
|
||||
if dept_info, err := sys_dept.GetDeptById(userEntity.DeptId); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
userInfo["dept_info"] = dept_info
|
||||
}
|
||||
|
||||
return userInfo, nil
|
||||
|
||||
}
|
||||
|
||||
func GetPostsByUserId(id int) ([]*sys_post.Entity, error) {
|
||||
return user_post.GetPostsByUserId(id)
|
||||
}
|
||||
|
||||
//获取登陆用户ID
|
||||
func GetLoginID(r *ghttp.Request) (userId int) {
|
||||
userInfo := GetLoginAdminInfo(r)
|
||||
@@ -31,9 +124,35 @@ func GetLoginAdminInfo(r *ghttp.Request) (userInfo *user.Entity) {
|
||||
return
|
||||
}
|
||||
|
||||
//获取当前登录用户信息,直接从数据库获取
|
||||
func GetCurrentUser(r *ghttp.Request) (userInfo *user.Entity, err error) {
|
||||
id := GetLoginID(r)
|
||||
userInfo, err = user.GetUserById(id)
|
||||
return
|
||||
}
|
||||
|
||||
//获取管理员列表
|
||||
func GetAdminList(where g.Map, page int) (total int, userList []*user.Entity, err error) {
|
||||
return user.GetAdminList(where, page, service.AdminPageNum)
|
||||
func GetAdminList(req *user.SearchReq) (total, page int, userList []*user.Entity, err error) {
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = service.AdminPageNum
|
||||
}
|
||||
var depts []*sys_dept.Dept
|
||||
if req.DeptId != "" {
|
||||
depts, err = sys_dept.GetList(&sys_dept.SearchParams{Status: "1"})
|
||||
if err != nil {
|
||||
g.Log().Debug(err)
|
||||
err = gerror.New("获取部门信息失败")
|
||||
return
|
||||
}
|
||||
mDepts := gconv.SliceMap(depts)
|
||||
deptId := gconv.Int(req.DeptId)
|
||||
req.DeptIds = append(req.DeptIds, deptId)
|
||||
childrenIds := utils.FindSonByParentId(mDepts, deptId, "parentId", "deptId")
|
||||
for _, d := range childrenIds {
|
||||
req.DeptIds = append(req.DeptIds, gconv.Int(d["deptId"]))
|
||||
}
|
||||
}
|
||||
return user.GetAdminList(req)
|
||||
}
|
||||
|
||||
//获取管理员的角色信息
|
||||
@@ -72,6 +191,10 @@ func GetAdminRoleIds(userId int) (roleIds []int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func GetAdminPosts(userId int) (postIds []int64, err error) {
|
||||
return user_post.GetAdminPosts(userId)
|
||||
}
|
||||
|
||||
//获取菜单
|
||||
func GetAllMenus() (menus g.List, err error) {
|
||||
//获取所有开启的菜单
|
||||
@@ -82,10 +205,10 @@ func GetAllMenus() (menus g.List, err error) {
|
||||
menus = make(g.List, len(allMenus))
|
||||
for k, v := range allMenus {
|
||||
menu := gconv.Map(v)
|
||||
menu["index"] = v.Name
|
||||
menu = setMenuMap(menu, v)
|
||||
menus[k] = menu
|
||||
}
|
||||
menus = utils.PushSonToParent(menus, 0, "pid", "id", "subs", "", nil, false)
|
||||
menus = utils.PushSonToParent(menus, 0, "pid", "id", "children", "", nil, true)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -115,10 +238,49 @@ func GetAdminMenusByRoleIds(roleIds []int) (menus g.List, err error) {
|
||||
for _, v := range allMenus {
|
||||
if _, ok := menuIds[gconv.Int64(v.Id)]; gstr.Equal(v.Condition, "nocheck") || ok {
|
||||
roleMenu := gconv.Map(v)
|
||||
roleMenu["index"] = v.Name
|
||||
roleMenu = setMenuMap(roleMenu, v)
|
||||
roleMenus = append(roleMenus, roleMenu)
|
||||
}
|
||||
}
|
||||
menus = utils.PushSonToParent(roleMenus, 0, "pid", "id", "subs", "", nil, false)
|
||||
menus = utils.PushSonToParent(roleMenus, 0, "pid", "id", "children", "", nil, true)
|
||||
return
|
||||
}
|
||||
|
||||
//组合返回menu前端数据
|
||||
func setMenuMap(menu g.Map, entity *auth_rule.Entity) g.Map {
|
||||
menu["index"] = entity.Name
|
||||
menu["name"] = gstr.UcFirst(entity.Path)
|
||||
menu["menuName"] = entity.Title
|
||||
if entity.MenuType != 0 {
|
||||
menu["component"] = entity.Name
|
||||
menu["path"] = entity.Path
|
||||
} else {
|
||||
menu["path"] = "/" + entity.Path
|
||||
menu["component"] = "Layout"
|
||||
}
|
||||
menu["meta"] = g.MapStrStr{
|
||||
"icon": entity.Icon,
|
||||
"title": entity.Title,
|
||||
}
|
||||
if entity.AlwaysShow == 1 {
|
||||
menu["hidden"] = false
|
||||
} else {
|
||||
menu["hidden"] = true
|
||||
}
|
||||
if entity.AlwaysShow == 1 && entity.MenuType == 0 {
|
||||
menu["alwaysShow"] = true
|
||||
} else {
|
||||
menu["alwaysShow"] = false
|
||||
}
|
||||
return menu
|
||||
}
|
||||
|
||||
func ChangeUserStatus(req *user.StatusReq) error {
|
||||
return user.ChangeUserStatus(req)
|
||||
}
|
||||
|
||||
func ResetUserPwd(req *user.ResetPwdReq) error {
|
||||
//密码加密
|
||||
req.Password = utils.EncryptCBC(gconv.String(req.Password), utils.AdminCbcPublicKey)
|
||||
return user.ResetUserPwd(req)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ const (
|
||||
AdminAuthMenu = iota
|
||||
AdminAuthRole
|
||||
AdminCmsMenu
|
||||
AdminConfigDict
|
||||
)
|
||||
|
||||
//缓存TAG标签
|
||||
|
||||
Reference in New Issue
Block a user