mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 10:09:54 +08:00
发布代码生成、更新20+表单组件,优化数据字典,gf版本更新到2.3.1
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/tree"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sAdminDept struct{}
|
||||
@@ -47,16 +48,7 @@ func (s *sAdminDept) NameUnique(ctx context.Context, in adminin.DeptNameUniqueIn
|
||||
|
||||
// Delete 删除
|
||||
func (s *sAdminDept) Delete(ctx context.Context, in adminin.DeptDeleteInp) error {
|
||||
|
||||
exist, err := dao.AdminRoleDept.Ctx(ctx).Where("dept_id", in.Id).One()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if !exist.IsEmpty() {
|
||||
return gerror.New("请先解除该部门下所有已关联用户关联关系!")
|
||||
}
|
||||
_, err = dao.AdminDept.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
_, err := dao.AdminDept.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
@@ -118,7 +110,7 @@ func (s *sAdminDept) Status(ctx context.Context, in adminin.DeptStatusInp) (err
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
|
@@ -25,7 +25,8 @@ import (
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/adminin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/tree"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sAdminMember struct{}
|
||||
@@ -38,11 +39,11 @@ func init() {
|
||||
service.RegisterAdminMember(NewAdminMember())
|
||||
}
|
||||
|
||||
// UpdateProfile 修改登录密码
|
||||
// UpdateProfile 更新会员资料
|
||||
func (s *sAdminMember) UpdateProfile(ctx context.Context, in adminin.MemberUpdateProfileInp) (err error) {
|
||||
memberId := contexts.Get(ctx).User.Id
|
||||
if memberId <= 0 {
|
||||
err := gerror.New("获取用户信息失败!")
|
||||
err = gerror.New("获取用户信息失败!")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -55,9 +56,9 @@ func (s *sAdminMember) UpdateProfile(ctx context.Context, in adminin.MemberUpdat
|
||||
_, err = dao.AdminMember.Ctx(ctx).
|
||||
Where("id", memberId).
|
||||
Data(g.Map{
|
||||
"mobile": in.Mobile,
|
||||
"email": in.Email,
|
||||
"realname": in.Realname,
|
||||
"mobile": in.Mobile,
|
||||
"email": in.Email,
|
||||
"real_name": in.Realname,
|
||||
}).
|
||||
Update()
|
||||
|
||||
@@ -99,11 +100,18 @@ func (s *sAdminMember) UpdatePwd(ctx context.Context, in adminin.MemberUpdatePwd
|
||||
|
||||
// ResetPwd 重置密码
|
||||
func (s *sAdminMember) ResetPwd(ctx context.Context, in adminin.MemberResetPwdInp) (err error) {
|
||||
var memberInfo entity.AdminMember
|
||||
var (
|
||||
memberInfo entity.AdminMember
|
||||
memberId = contexts.GetUserId(ctx)
|
||||
)
|
||||
if err = dao.AdminMember.Ctx(ctx).Where("id", in.Id).Scan(&memberInfo); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
if memberInfo.Pid != memberId && !s.VerifySuperId(ctx, memberId) {
|
||||
err = gerror.New("操作非法")
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dao.AdminMember.Ctx(ctx).
|
||||
Where("id", in.Id).
|
||||
@@ -161,7 +169,7 @@ func (s *sAdminMember) NameUnique(ctx context.Context, in adminin.MemberNameUniq
|
||||
|
||||
// VerifySuperId 验证是否为超管
|
||||
func (s *sAdminMember) VerifySuperId(ctx context.Context, verifyId int64) bool {
|
||||
superIds, _ := g.Cfg().Get(ctx, "hotgo.admin.superIds")
|
||||
superIds := g.Cfg().MustGet(ctx, "hotgo.admin.superIds")
|
||||
for _, id := range superIds.Int64s() {
|
||||
if id == verifyId {
|
||||
return true
|
||||
@@ -176,7 +184,12 @@ func (s *sAdminMember) Delete(ctx context.Context, in adminin.MemberDeleteInp) e
|
||||
return gerror.New("超管账号禁止删除!")
|
||||
}
|
||||
|
||||
_, err := dao.AdminMember.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
memberId := contexts.GetUserId(ctx)
|
||||
if memberId <= 0 {
|
||||
return gerror.New("获取用户信息失败!")
|
||||
}
|
||||
|
||||
_, err := dao.AdminMember.Ctx(ctx).Where("id", in.Id).Where("pid", memberId).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
@@ -187,7 +200,10 @@ func (s *sAdminMember) Delete(ctx context.Context, in adminin.MemberDeleteInp) e
|
||||
|
||||
// Edit 修改/新增
|
||||
func (s *sAdminMember) Edit(ctx context.Context, in adminin.MemberEditInp) (err error) {
|
||||
|
||||
opMemberId := contexts.GetUserId(ctx)
|
||||
if opMemberId <= 0 {
|
||||
return gerror.New("获取用户信息失败!")
|
||||
}
|
||||
if in.Username == "" {
|
||||
return gerror.New("帐号不能为空")
|
||||
}
|
||||
@@ -197,7 +213,7 @@ func (s *sAdminMember) Edit(ctx context.Context, in adminin.MemberEditInp) (err
|
||||
return gerror.Wrap(err, consts.ErrorORM)
|
||||
}
|
||||
if !uniqueName {
|
||||
return gerror.New("帐号已存在")
|
||||
return gerror.New("用户名已存在")
|
||||
}
|
||||
|
||||
if in.Mobile != "" {
|
||||
@@ -221,12 +237,18 @@ func (s *sAdminMember) Edit(ctx context.Context, in adminin.MemberEditInp) (err
|
||||
}
|
||||
|
||||
// 修改
|
||||
in.UpdatedAt = gtime.Now()
|
||||
if in.Id > 0 {
|
||||
if s.VerifySuperId(ctx, in.Id) {
|
||||
return gerror.New("超管账号禁止编辑!")
|
||||
}
|
||||
_, err = dao.AdminMember.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
||||
|
||||
// 权限验证
|
||||
var mm = dao.AdminMember.Ctx(ctx).Where("id", in.Id)
|
||||
if !s.VerifySuperId(ctx, opMemberId) {
|
||||
mm = mm.Where("pid", opMemberId)
|
||||
}
|
||||
|
||||
_, err = mm.Data(in).Update()
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, consts.ErrorORM)
|
||||
}
|
||||
@@ -238,26 +260,25 @@ func (s *sAdminMember) Edit(ctx context.Context, in adminin.MemberEditInp) (err
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
in.CreatedAt = gtime.Now()
|
||||
|
||||
// 新增用户时的额外属性
|
||||
var data adminin.MemberAddInp
|
||||
data.MemberEditInp = in
|
||||
data.Salt = grand.S(6)
|
||||
data.PasswordHash = gmd5.MustEncryptString(data.Password + data.Salt)
|
||||
|
||||
insert, err := dao.AdminMember.Ctx(ctx).Data(data).Insert()
|
||||
// 关系树
|
||||
data.Pid = opMemberId
|
||||
data.Level, data.Tree, err = s.genTree(ctx, opMemberId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := dao.AdminMember.Ctx(ctx).Data(data).InsertAndGetId()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新岗位
|
||||
id, err := insert.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = dao.AdminMemberPost.UpdatePostIds(ctx, id, in.PostIds)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -292,11 +313,11 @@ func (s *sAdminMember) View(ctx context.Context, in adminin.MemberViewInp) (res
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sAdminMember) List(ctx context.Context, in adminin.MemberListInp) (list []*adminin.MemberListModel, totalCount int64, err error) {
|
||||
func (s *sAdminMember) List(ctx context.Context, in adminin.MemberListInp) (list []*adminin.MemberListModel, totalCount int, err error) {
|
||||
g.Log().Printf(ctx, "in:%#v", in)
|
||||
mod := dao.AdminMember.Ctx(ctx)
|
||||
if in.Realname != "" {
|
||||
mod = mod.WhereLike("realname", "%"+in.Realname+"%")
|
||||
mod = mod.WhereLike("real_name", "%"+in.Realname+"%")
|
||||
}
|
||||
if in.Username != "" {
|
||||
mod = mod.WhereLike("username", "%"+in.Username+"%")
|
||||
@@ -325,7 +346,7 @@ func (s *sAdminMember) List(ctx context.Context, in adminin.MemberListInp) (list
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
return nil, 0, gerror.Wrap(err, consts.ErrorORM)
|
||||
}
|
||||
|
||||
@@ -344,7 +365,7 @@ func (s *sAdminMember) List(ctx context.Context, in adminin.MemberListInp) (list
|
||||
// 角色
|
||||
roleName, err := dao.AdminRole.Ctx(ctx).
|
||||
Fields("name").
|
||||
Where("id", list[i].Role).
|
||||
Where("id", list[i].RoleId).
|
||||
Value()
|
||||
if err != nil {
|
||||
return nil, 0, gerror.Wrap(err, consts.ErrorORM)
|
||||
@@ -352,43 +373,62 @@ func (s *sAdminMember) List(ctx context.Context, in adminin.MemberListInp) (list
|
||||
list[i].RoleName = roleName.String()
|
||||
|
||||
// 岗位
|
||||
post, err := dao.AdminMemberPost.Ctx(ctx).
|
||||
posts, err := dao.AdminMemberPost.Ctx(ctx).
|
||||
Fields("post_id").
|
||||
Where("member_id", list[i].Id).
|
||||
Value()
|
||||
Array()
|
||||
if err != nil {
|
||||
return nil, 0, gerror.Wrap(err, consts.ErrorORM)
|
||||
}
|
||||
list[i].PostIds = post.Int64s()
|
||||
|
||||
for _, v := range posts {
|
||||
list[i].PostIds = append(list[i].PostIds, v.Int64())
|
||||
}
|
||||
}
|
||||
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
// genTree 生成关系树
|
||||
func (s *sAdminMember) genTree(ctx context.Context, pid int64) (level int, newTree string, err error) {
|
||||
var (
|
||||
pInfo *entity.AdminMember
|
||||
)
|
||||
err = dao.AdminMember.Ctx(ctx).Where("id", pid).Scan(&pInfo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if pInfo == nil {
|
||||
err = gerror.New("上级信息不存在")
|
||||
return
|
||||
}
|
||||
|
||||
level = pInfo.Level + 1
|
||||
newTree = tree.GenLabel(pInfo.Tree, pInfo.Id)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// LoginMemberInfo 获取登录用户信息
|
||||
func (s *sAdminMember) LoginMemberInfo(ctx context.Context, req *member.InfoReq) (res *adminin.MemberLoginModel, err error) {
|
||||
|
||||
var (
|
||||
permissions adminin.MemberLoginPermissions
|
||||
identity *model.Identity
|
||||
)
|
||||
|
||||
identity = contexts.Get(ctx).User
|
||||
|
||||
identity := contexts.Get(ctx).User
|
||||
if identity == nil {
|
||||
err = gerror.New("用户身份异常,请重新登录!")
|
||||
return
|
||||
}
|
||||
|
||||
permissions.Label = "主控台"
|
||||
permissions.Value = "value"
|
||||
permissions, err := service.AdminMenu().LoginPermissions(ctx, identity.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res = &adminin.MemberLoginModel{
|
||||
UserId: identity.Id,
|
||||
Username: identity.Username,
|
||||
RealName: identity.RealName,
|
||||
Avatar: identity.Avatar,
|
||||
Permissions: []adminin.MemberLoginPermissions{permissions},
|
||||
Permissions: permissions,
|
||||
Token: jwt.GetAuthorization(ghttp.RequestFromCtx(ctx)),
|
||||
}
|
||||
|
||||
@@ -429,7 +469,7 @@ func (s *sAdminMember) Login(ctx context.Context, in adminin.MemberLoginInp) (re
|
||||
|
||||
err = dao.AdminRole.Ctx(ctx).
|
||||
Fields("id,key,status").
|
||||
Where("id", memberInfo.Role).
|
||||
Where("id", memberInfo.RoleId).
|
||||
Scan(&roleInfo)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
@@ -445,30 +485,26 @@ func (s *sAdminMember) Login(ctx context.Context, in adminin.MemberLoginInp) (re
|
||||
return
|
||||
}
|
||||
|
||||
// 生成token
|
||||
jwtExpires, err := g.Cfg().Get(ctx, "jwt.expires", 1)
|
||||
if err != nil {
|
||||
err := gerror.New(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
// 有效期
|
||||
expires := jwtExpires.Int64()
|
||||
expires := g.Cfg().MustGet(ctx, "jwt.expires", 1).Int64()
|
||||
|
||||
// 过期时间戳
|
||||
exp := gconv.Int64(gtime.Timestamp()) + expires
|
||||
|
||||
identity = &model.Identity{
|
||||
Id: memberInfo.Id,
|
||||
Pid: memberInfo.Pid,
|
||||
DeptId: memberInfo.DeptId,
|
||||
RoleId: roleInfo.Id,
|
||||
RoleKey: roleInfo.Key,
|
||||
Username: memberInfo.Username,
|
||||
RealName: memberInfo.Realname,
|
||||
RealName: memberInfo.RealName,
|
||||
Avatar: memberInfo.Avatar,
|
||||
Email: memberInfo.Email,
|
||||
Mobile: memberInfo.Mobile,
|
||||
VisitCount: memberInfo.VisitCount,
|
||||
LastTime: memberInfo.LastTime,
|
||||
LastIp: memberInfo.LastIp,
|
||||
Role: roleInfo.Id,
|
||||
RoleKey: roleInfo.Key,
|
||||
Exp: exp,
|
||||
Expires: expires,
|
||||
App: consts.AppAdmin,
|
||||
@@ -509,7 +545,7 @@ func (s *sAdminMember) Login(ctx context.Context, in adminin.MemberLoginInp) (re
|
||||
}
|
||||
|
||||
// RoleMemberList 获取角色下的会员列表
|
||||
func (s *sAdminMember) RoleMemberList(ctx context.Context, in adminin.RoleMemberListInp) (list []*adminin.MemberListModel, totalCount int64, err error) {
|
||||
func (s *sAdminMember) RoleMemberList(ctx context.Context, in adminin.RoleMemberListInp) (list []*adminin.MemberListModel, totalCount int, err error) {
|
||||
mod := dao.AdminMember.Ctx(ctx)
|
||||
if in.Role > 0 {
|
||||
mod = mod.Where("role", in.Role)
|
||||
@@ -521,7 +557,7 @@ func (s *sAdminMember) RoleMemberList(ctx context.Context, in adminin.RoleMember
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list)
|
||||
err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
@@ -546,7 +582,7 @@ func (s *sAdminMember) Status(ctx context.Context, in adminin.MemberStatusInp) (
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/api/backend/menu"
|
||||
"hotgo/api/backend/role"
|
||||
@@ -198,8 +199,8 @@ func (s *sAdminMenu) Edit(ctx context.Context, req *menu.EditReq) (err error) {
|
||||
err = gerror.New("菜单名称不能为空")
|
||||
return err
|
||||
}
|
||||
if req.Path == "" {
|
||||
err = gerror.New("菜单路径不能为空")
|
||||
if req.Type != 3 && req.Path == "" {
|
||||
err = gerror.New("路由地址不能为空")
|
||||
return err
|
||||
}
|
||||
if req.Name == "" {
|
||||
@@ -367,3 +368,50 @@ func (s *sAdminMenu) GetMenuList(ctx context.Context, memberId int64) (lists rol
|
||||
lists.List = append(lists.List, s.genNaiveMenus(menus)...)
|
||||
return
|
||||
}
|
||||
|
||||
// LoginPermissions 获取登录成功后的细分权限
|
||||
func (s *sAdminMenu) LoginPermissions(ctx context.Context, memberId int64) (lists []*adminin.MemberLoginPermissions, err error) {
|
||||
// 空跑
|
||||
lists = append(lists, &adminin.MemberLoginPermissions{
|
||||
Value: "value",
|
||||
})
|
||||
|
||||
type Permissions struct {
|
||||
Permissions string `json:"permissions"`
|
||||
}
|
||||
|
||||
var (
|
||||
allPermissions []*Permissions
|
||||
mod = dao.AdminMenu.Ctx(ctx).Fields("permissions").Where("status", consts.StatusEnabled).Where("permissions != ?", "")
|
||||
)
|
||||
|
||||
// 非超管验证允许的菜单列表
|
||||
if !service.AdminMember().VerifySuperId(ctx, memberId) {
|
||||
array, err := dao.AdminRoleMenu.Ctx(ctx).
|
||||
Fields("menu_id").
|
||||
Where("role_id", contexts.GetRoleId(ctx)).
|
||||
Array()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mod = mod.Where("id", array)
|
||||
}
|
||||
|
||||
if err = mod.Scan(&allPermissions); err != nil {
|
||||
return lists, err
|
||||
}
|
||||
|
||||
if len(allPermissions) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range allPermissions {
|
||||
for _, p := range gstr.Explode(`,`, v.Permissions) {
|
||||
lists = append(lists, &adminin.MemberLoginPermissions{
|
||||
Value: p,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -16,8 +16,8 @@ import (
|
||||
"hotgo/internal/service"
|
||||
"hotgo/internal/websocket"
|
||||
"hotgo/utility/charset"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/simple"
|
||||
"hotgo/utility/validate"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -100,7 +100,7 @@ func (s *sAdminNotice) Status(ctx context.Context, in adminin.NoticeStatusInp) (
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (s *sAdminNotice) View(ctx context.Context, in adminin.NoticeViewInp) (res
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sAdminNotice) List(ctx context.Context, in adminin.NoticeListInp) (list []*adminin.NoticeListModel, totalCount int64, err error) {
|
||||
func (s *sAdminNotice) List(ctx context.Context, in adminin.NoticeListInp) (list []*adminin.NoticeListModel, totalCount int, err error) {
|
||||
mod := dao.AdminNotice.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -168,7 +168,7 @@ func (s *sAdminNotice) List(ctx context.Context, in adminin.NoticeListInp) (list
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ import (
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/input/adminin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sAdminPost struct{}
|
||||
@@ -153,7 +153,7 @@ func (s *sAdminPost) View(ctx context.Context, in adminin.PostViewInp) (res *adm
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sAdminPost) List(ctx context.Context, in adminin.PostListInp) (list []*adminin.PostListModel, totalCount int64, err error) {
|
||||
func (s *sAdminPost) List(ctx context.Context, in adminin.PostListInp) (list []*adminin.PostListModel, totalCount int, err error) {
|
||||
mod := dao.AdminPost.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -181,7 +181,7 @@ func (s *sAdminPost) List(ctx context.Context, in adminin.PostListInp) (list []*
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
@@ -226,7 +226,7 @@ func (s *sAdminPost) Status(ctx context.Context, in adminin.PostStatusInp) (err
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ package admin
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
@@ -17,9 +18,14 @@ import (
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/casbin"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/hgorm"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/adminin"
|
||||
"hotgo/internal/model/input/form"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/auth"
|
||||
"hotgo/utility/convert"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type sAdminRole struct{}
|
||||
@@ -38,9 +44,9 @@ func (s *sAdminRole) Verify(ctx context.Context, path, method string) bool {
|
||||
return true
|
||||
}
|
||||
var (
|
||||
user = contexts.Get(ctx).User
|
||||
superRoleKey, _ = g.Cfg().Get(ctx, "hotgo.admin.superRoleKey")
|
||||
err error
|
||||
user = contexts.Get(ctx).User
|
||||
superRoleKey = g.Cfg().MustGet(ctx, "hotgo.admin.superRoleKey")
|
||||
err error
|
||||
)
|
||||
|
||||
if user == nil {
|
||||
@@ -61,7 +67,7 @@ func (s *sAdminRole) Verify(ctx context.Context, path, method string) bool {
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sAdminRole) List(ctx context.Context, in adminin.RoleListInp) (list []*adminin.RoleListModel, totalCount int64, err error) {
|
||||
func (s *sAdminRole) List(ctx context.Context, in adminin.RoleListInp) (list []*adminin.RoleListModel, totalCount int, err error) {
|
||||
mod := dao.AdminRole.Ctx(ctx)
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
@@ -69,7 +75,7 @@ func (s *sAdminRole) List(ctx context.Context, in adminin.RoleListInp) (list []*
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
err = mod.Page(int(in.Page), int(in.PerPage)).Order("id asc").Scan(&list)
|
||||
err = mod.Page(in.Page, in.PerPage).Order("id asc").Scan(&list)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
@@ -128,7 +134,7 @@ func (s *sAdminRole) GetPermissions(ctx context.Context, reqInfo *role.GetPermis
|
||||
|
||||
// UpdatePermissions 更改角色菜单权限
|
||||
func (s *sAdminRole) UpdatePermissions(ctx context.Context, reqInfo *role.UpdatePermissionsReq) error {
|
||||
return dao.AdminRoleMenu.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) (err error) {
|
||||
return dao.AdminRoleMenu.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
_, err = dao.AdminRoleMenu.Ctx(ctx).Where("role_id", reqInfo.RoleId).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
@@ -185,6 +191,11 @@ func (s *sAdminRole) Edit(ctx context.Context, in *role.EditReq) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
in.Pid, in.Level, in.Tree, err = hgorm.GenSubTree(ctx, dao.AdminRole, in.Pid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改
|
||||
in.UpdatedAt = gtime.Now()
|
||||
if in.Id > 0 {
|
||||
@@ -219,3 +230,58 @@ func (s *sAdminRole) Delete(ctx context.Context, in *role.DeleteReq) (err error)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sAdminRole) DataScopeSelect(ctx context.Context) (res form.Selects) {
|
||||
for k, v := range consts.RoleDataNameMap {
|
||||
res = append(res, &form.Select{
|
||||
Value: k,
|
||||
Name: v,
|
||||
Label: v,
|
||||
})
|
||||
}
|
||||
sort.Sort(res)
|
||||
return res
|
||||
}
|
||||
|
||||
func (s *sAdminRole) DataScopeEdit(ctx context.Context, in *adminin.DataScopeEditInp) (err error) {
|
||||
if in.Id <= 0 {
|
||||
return gerror.New("角色ID不正确!")
|
||||
}
|
||||
|
||||
var (
|
||||
models *entity.AdminRole
|
||||
superRoleKey = g.Cfg().MustGet(ctx, "hotgo.admin.superRoleKey")
|
||||
)
|
||||
|
||||
err = dao.AdminRole.Ctx(ctx).Where("id", in.Id).Scan(&models)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if models == nil {
|
||||
return gerror.New("角色不存在")
|
||||
}
|
||||
|
||||
if models.Key == superRoleKey.String() {
|
||||
return gerror.New("超管角色拥有全部权限,无需修改!")
|
||||
}
|
||||
|
||||
if in.DataScope == consts.RoleDataDeptCustom && len(convert.UniqueSliceInt64(in.CustomDept)) == 0 {
|
||||
return gerror.New("自定义权限必须配置自定义部门!")
|
||||
}
|
||||
|
||||
models.DataScope = in.DataScope
|
||||
models.CustomDept = gjson.New(convert.UniqueSliceInt64(in.CustomDept))
|
||||
|
||||
_, err = dao.AdminRole.Ctx(ctx).
|
||||
Fields(dao.AdminRole.Columns().DataScope, dao.AdminRole.Columns().CustomDept).
|
||||
Where("id", in.Id).
|
||||
Data(models).
|
||||
Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
266
server/internal/logic/admin/test.go
Normal file
266
server/internal/logic/admin/test.go
Normal file
@@ -0,0 +1,266 @@
|
||||
// Package admin
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/hgorm"
|
||||
"hotgo/internal/model/input/adminin"
|
||||
"hotgo/internal/model/input/form"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/excel"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sAdminTest struct{}
|
||||
|
||||
func NewAdminTest() *sAdminTest {
|
||||
return &sAdminTest{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterAdminTest(NewAdminTest())
|
||||
}
|
||||
|
||||
// Model Orm模型
|
||||
func (s *sAdminTest) Model(ctx context.Context) *gdb.Model {
|
||||
return dao.Test.Ctx(ctx)
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sAdminTest) List(ctx context.Context, in adminin.TestListInp) (list []*adminin.TestListModel, totalCount int, err error) {
|
||||
mod := dao.Test.Ctx(ctx)
|
||||
|
||||
if in.Title != "" {
|
||||
mod = mod.WhereLike(dao.Test.Columns().Title, "%"+in.Title+"%")
|
||||
}
|
||||
|
||||
if in.Content != "" {
|
||||
mod = mod.WhereLike(dao.Test.Columns().Content, "%"+in.Content+"%")
|
||||
}
|
||||
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where(dao.Test.Columns().Status, in.Status)
|
||||
}
|
||||
|
||||
if in.Switch > 0 {
|
||||
mod = mod.Where(dao.Test.Columns().Switch, in.Switch)
|
||||
}
|
||||
|
||||
if len(in.Price) > 0 {
|
||||
if in.Price[0] > float64(0) && in.Price[1] > float64(0) {
|
||||
mod = mod.WhereBetween(dao.Test.Columns().Price, in.Price[0], in.Price[1])
|
||||
} else if in.Price[0] > float64(0) && in.Price[1] == float64(0) {
|
||||
mod = mod.WhereGTE(dao.Test.Columns().Price, in.Price[0])
|
||||
} else if in.Price[0] == float64(0) && in.Price[1] > float64(0) {
|
||||
mod = mod.WhereLTE(dao.Test.Columns().Price, in.Price[1])
|
||||
}
|
||||
}
|
||||
|
||||
if in.ActivityAt != nil {
|
||||
mod = mod.Where(dao.Test.Columns().ActivityAt, in.ActivityAt)
|
||||
}
|
||||
|
||||
if len(in.CreatedAt) == 2 {
|
||||
mod = mod.WhereBetween(dao.Test.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
|
||||
}
|
||||
|
||||
if !in.Flag.IsNil() {
|
||||
mod = mod.Where(fmt.Sprintf(`JSON_CONTAINS(%s,'%v')`, dao.Test.Columns().Flag, in.Flag))
|
||||
}
|
||||
|
||||
if !in.Hobby.IsNil() {
|
||||
mod = mod.Where(fmt.Sprintf(`JSON_CONTAINS(%s,'%v')`, dao.Test.Columns().Hobby, in.Hobby))
|
||||
}
|
||||
|
||||
//// 关联表testCategory
|
||||
//mod = mod.LeftJoin(hgorm.GenJoinOnRelation(
|
||||
// dao.Test.Table(), dao.Test.Columns().CategoryId, // 主表表名,关联条件
|
||||
// dao.TestCategory.Table(), "testCategory", dao.TestCategory.Columns().Id, // 关联表表名,别名,关联条件
|
||||
//)...)
|
||||
//
|
||||
//mod = mod.Where(`testCategory.`+dao.TestCategory.Columns().Name, "微信公众号")
|
||||
|
||||
totalCount, err = mod.Clone().Count(1)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
if totalCount == 0 {
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
////关联表select
|
||||
//fields, err := hgorm.GenJoinSelect(ctx, adminin.TestListModel{}, dao.Test, []*hgorm.Join{
|
||||
// {Dao: dao.TestCategory, Alias: "testCategory"},
|
||||
// //{Dao: dao.TestCategory, Alias: "testCategory"},
|
||||
//})
|
||||
|
||||
fields, err := hgorm.GenSelect(ctx, adminin.TestListModel{}, dao.Test)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err = mod.Fields(fields).Handler(hgorm.HandlerFilterAuth, hgorm.HandlerForceCache).Page(in.Page, in.PerPage).OrderAsc(dao.Test.Columns().Sort).OrderDesc(dao.Test.Columns().Id).Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
// Export 导出
|
||||
func (s *sAdminTest) Export(ctx context.Context, in adminin.TestListInp) (err error) {
|
||||
list, totalCount, err := s.List(ctx, in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 字段的排序是依据tags的字段顺序,如果你不想使用默认的排序方式,可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
|
||||
tags, err := convert.GetEntityDescTags(adminin.TestExportModel{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
fileName = "测试导出-" + gctx.CtxId(ctx) + ".xlsx"
|
||||
sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
|
||||
exports []adminin.TestExportModel
|
||||
)
|
||||
|
||||
err = gconv.Scan(list, &exports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改/新增
|
||||
func (s *sAdminTest) Edit(ctx context.Context, in adminin.TestEditInp) (err error) {
|
||||
if err = hgorm.IsUnique(ctx, dao.Test, g.Map{dao.Test.Columns().Qq: in.Qq}, "QQ号码已存在,请换一个", in.Id); err != nil {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 修改
|
||||
if in.Id > 0 {
|
||||
in.UpdatedBy = contexts.GetUserId(ctx)
|
||||
_, err = dao.Test.Ctx(ctx).Where(dao.Test.Columns().Id, in.Id).Data(in).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
in.CreatedBy = contexts.GetUserId(ctx)
|
||||
_, err = dao.Test.Ctx(ctx).Data(in).Insert()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
func (s *sAdminTest) Delete(ctx context.Context, in adminin.TestDeleteInp) (err error) {
|
||||
_, err = dao.Test.Ctx(ctx).Where(dao.Test.Columns().Id, in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Status 更新状态
|
||||
func (s *sAdminTest) Status(ctx context.Context, in adminin.TestStatusInp) (err error) {
|
||||
if in.Id <= 0 {
|
||||
err = gerror.New("ID不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
if in.Status <= 0 {
|
||||
err = gerror.New("状态不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改
|
||||
_, err = dao.Test.Ctx(ctx).Where(dao.Test.Columns().Id, in.Id).Data(dao.Test.Columns().Status, in.Status).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Switch 更新开关状态
|
||||
func (s *sAdminTest) Switch(ctx context.Context, in adminin.TestSwitchInp) (err error) {
|
||||
var fields = []string{
|
||||
dao.Test.Columns().Switch,
|
||||
// ...
|
||||
}
|
||||
|
||||
if !validate.InSliceString(fields, in.Key) {
|
||||
err = gerror.New("开关键名不在白名单")
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改
|
||||
_, err = dao.Test.Ctx(ctx).Where(dao.Test.Columns().Id, in.Id).Data(in.Key, in.Value).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxSort 最大排序
|
||||
func (s *sAdminTest) MaxSort(ctx context.Context, in adminin.TestMaxSortInp) (res *adminin.TestMaxSortModel, err error) {
|
||||
if err = dao.Test.Ctx(ctx).Fields(dao.Test.Columns().Sort).OrderDesc(dao.Test.Columns().Sort).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.Sort = res.Sort + g.Cfg().MustGet(ctx, "hotgo.admin.maxSortIncrement").Int()
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// View 获取指定字典类型信息
|
||||
func (s *sAdminTest) View(ctx context.Context, in adminin.TestViewInp) (res *adminin.TestViewModel, err error) {
|
||||
if err = dao.Test.Ctx(ctx).Where(dao.Test.Columns().Id, in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
@@ -8,19 +8,25 @@ package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
ufile "github.com/ufilesdk-dev/ufile-gosdk"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/encrypt"
|
||||
f "hotgo/utility/file"
|
||||
"hotgo/utility/format"
|
||||
"hotgo/utility/url"
|
||||
"hotgo/utility/validate"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -35,6 +41,38 @@ func init() {
|
||||
service.RegisterCommonUpload(NewCommonUpload())
|
||||
}
|
||||
|
||||
// UploadFile 上传文件
|
||||
func (s *sCommonUpload) UploadFile(ctx context.Context, file *ghttp.UploadFile) (result *sysin.AttachmentListModel, err error) {
|
||||
if file == nil {
|
||||
err = gerror.New("文件必须!")
|
||||
return
|
||||
}
|
||||
|
||||
meta, err := s.fileMeta(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = f.GetFileType(meta.Ext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conf, err := service.SysConfig().GetUpload(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch conf.Drive {
|
||||
case consts.UploadDriveLocal:
|
||||
return s.UploadLocal(ctx, conf, file, meta)
|
||||
case consts.UploadDriveUCloud:
|
||||
return s.UploadUCloud(ctx, conf, file, meta)
|
||||
default:
|
||||
return nil, gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
|
||||
}
|
||||
}
|
||||
|
||||
// UploadImage 上传图片
|
||||
func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile) (result *sysin.AttachmentListModel, err error) {
|
||||
if file == nil {
|
||||
@@ -50,11 +88,28 @@ func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile)
|
||||
if !f.IsImgType(meta.Ext) {
|
||||
return nil, gerror.New("上传的文件不是图片")
|
||||
}
|
||||
return s.UploadLocal(ctx, file, meta)
|
||||
|
||||
if meta.Size > 2*1024*1024 {
|
||||
return nil, gerror.New("图片大小不能超过2MB")
|
||||
}
|
||||
|
||||
conf, err := service.SysConfig().GetUpload(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch conf.Drive {
|
||||
case consts.UploadDriveLocal:
|
||||
return s.UploadLocal(ctx, conf, file, meta)
|
||||
case consts.UploadDriveUCloud:
|
||||
return s.UploadUCloud(ctx, conf, file, meta)
|
||||
default:
|
||||
return nil, gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
|
||||
}
|
||||
}
|
||||
|
||||
// UploadLocal 上传本地
|
||||
func (s *sCommonUpload) UploadLocal(ctx context.Context, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
result, err = dao.SysAttachment.GetMd5File(ctx, meta.Md5)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
@@ -66,8 +121,8 @@ func (s *sCommonUpload) UploadLocal(ctx context.Context, file *ghttp.UploadFile,
|
||||
}
|
||||
|
||||
var (
|
||||
value, _ = g.Cfg().Get(ctx, "server.serverRoot")
|
||||
nowDate = time.Now().Format("2006-01-02")
|
||||
value = g.Cfg().MustGet(ctx, "server.serverRoot")
|
||||
nowDate = time.Now().Format("2006-01-02")
|
||||
)
|
||||
|
||||
if value.IsEmpty() {
|
||||
@@ -75,21 +130,89 @@ func (s *sCommonUpload) UploadLocal(ctx context.Context, file *ghttp.UploadFile,
|
||||
return
|
||||
}
|
||||
|
||||
if conf.LocalPath == "" {
|
||||
err = gerror.New("本地上传驱动必须配置本地存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
// 包含静态文件夹的路径
|
||||
fullDirPath := strings.Trim(value.String(), "/") + "/attachment/" + nowDate
|
||||
fullDirPath := strings.Trim(value.String(), "/") + "/" + conf.LocalPath + nowDate
|
||||
fileName, err := file.Save(fullDirPath, true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 不含静态文件夹的路径
|
||||
fullPath := "attachment/" + nowDate + "/" + fileName
|
||||
fullPath := conf.LocalPath + nowDate + "/" + fileName
|
||||
|
||||
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveLocal)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
attachment.FileUrl = s.LastUrl(ctx, attachment.FileUrl, attachment.Drive)
|
||||
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
|
||||
result = &sysin.AttachmentListModel{
|
||||
SysAttachment: *attachment,
|
||||
SizeFormat: format.FileSize(attachment.Size),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UploadUCloud 上传UCloud对象存储
|
||||
func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
result, err = dao.SysAttachment.GetMd5File(ctx, meta.Md5)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return
|
||||
}
|
||||
|
||||
if result != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if conf.UCloudPath == "" {
|
||||
err = gerror.New("UCloud存储驱动必须配置存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
nowDate := time.Now().Format("2006-01-02")
|
||||
fileName := gfile.Basename(file.Filename)
|
||||
fileName = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
|
||||
fileName = fileName + gfile.Ext(file.Filename)
|
||||
fullPath := conf.UCloudPath + nowDate + "/" + fileName
|
||||
config := &ufile.Config{
|
||||
PublicKey: conf.UCloudPublicKey,
|
||||
PrivateKey: conf.UCloudPrivateKey,
|
||||
BucketHost: conf.UCloudBucketHost,
|
||||
BucketName: conf.UCloudBucketName,
|
||||
FileHost: conf.UCloudFileHost,
|
||||
Endpoint: conf.UCloudEndpoint,
|
||||
VerifyUploadMD5: false,
|
||||
}
|
||||
req, err := ufile.NewFileRequest(config, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() {
|
||||
_ = f2.Close()
|
||||
}()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = req.IOPut(f2, fullPath, ""); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g.Log().Warningf(ctx, "ras:%+v", string(req.LastResponseBody))
|
||||
|
||||
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveUCloud)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
|
||||
result = &sysin.AttachmentListModel{
|
||||
SysAttachment: *attachment,
|
||||
SizeFormat: format.FileSize(attachment.Size),
|
||||
@@ -98,16 +221,19 @@ func (s *sCommonUpload) UploadLocal(ctx context.Context, file *ghttp.UploadFile,
|
||||
}
|
||||
|
||||
// LastUrl 根据驱动获取最终文件访问地址
|
||||
func (s *sCommonUpload) LastUrl(ctx context.Context, fullPath, drive string) string {
|
||||
func (s *sCommonUpload) LastUrl(ctx context.Context, conf *model.UploadConfig, fullPath, drive string) string {
|
||||
if validate.IsURL(fullPath) {
|
||||
return fullPath
|
||||
}
|
||||
|
||||
if drive == consts.UploadDriveLocal {
|
||||
return fmt.Sprintf("http://%s/", g.RequestFromCtx(ctx).Host) + "/" + fullPath
|
||||
switch drive {
|
||||
case consts.UploadDriveLocal:
|
||||
return url.GetAddr(ctx) + "/" + fullPath
|
||||
case consts.UploadDriveUCloud:
|
||||
return conf.UCloudEndpoint + "/" + fullPath
|
||||
default:
|
||||
return fullPath
|
||||
}
|
||||
|
||||
return fullPath
|
||||
}
|
||||
|
||||
// fileMeta 上传文件元数据
|
||||
|
@@ -26,7 +26,7 @@ func (s *sMiddleware) AdminAuth(r *ghttp.Request) {
|
||||
)
|
||||
|
||||
// 替换掉模块前缀
|
||||
routerPrefix, _ := g.Cfg().Get(ctx, "router.admin.prefix", "/admin")
|
||||
routerPrefix := g.Cfg().MustGet(ctx, "router.admin.prefix", "/admin")
|
||||
path := gstr.Replace(r.URL.Path, routerPrefix.String(), "", 1)
|
||||
|
||||
/// 不需要验证登录的路由地址
|
||||
|
@@ -24,7 +24,7 @@ func (s *sMiddleware) ApiAuth(r *ghttp.Request) {
|
||||
)
|
||||
|
||||
// 替换掉模块前缀
|
||||
routerPrefix, _ := g.Cfg().Get(ctx, "router.api.prefix", "/api")
|
||||
routerPrefix := g.Cfg().MustGet(ctx, "router.api.prefix", "/api")
|
||||
path := gstr.Replace(r.URL.Path, routerPrefix.String(), "", 1)
|
||||
|
||||
/// 不需要验证登录的路由地址
|
||||
@@ -39,9 +39,9 @@ func (s *sMiddleware) ApiAuth(r *ghttp.Request) {
|
||||
}
|
||||
|
||||
//// 验证路由访问权限
|
||||
//verify := adminService.Role.Verify(ctx, customCtx.User.Id, path)
|
||||
//verify := service.AdminRole().Verify(ctx, path, r.Method)
|
||||
//if !verify {
|
||||
// response.JsonExit(r, gcode.CodeSecurityReason.Code(), "你没有访问权限!")
|
||||
// response.JsonExit(r, consts.CodeSecurityReason, "你没有访问权限!")
|
||||
// return
|
||||
//}
|
||||
|
||||
|
@@ -75,7 +75,7 @@ func (s *sMiddleware) CORS(r *ghttp.Request) {
|
||||
|
||||
// DemoLimit 演示系統操作限制
|
||||
func (s *sMiddleware) DemoLimit(r *ghttp.Request) {
|
||||
isDemo, _ := g.Cfg().Get(r.Context(), "hotgo.isDemo", false)
|
||||
isDemo := g.Cfg().MustGet(r.Context(), "hotgo.isDemo", false)
|
||||
if !isDemo.Bool() {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
@@ -86,7 +86,7 @@ func (s *sMiddleware) DemoLimit(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
response.JsonExit(r, gcode.CodeInvalidRequest.Code(), "演示系統禁止操作!")
|
||||
response.JsonExit(r, gcode.CodeNotSupported.Code(), "演示系统禁止操作!")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ func inspectAuth(r *ghttp.Request, appName string) error {
|
||||
|
||||
// 获取jwtToken
|
||||
jwtToken := consts.RedisJwtToken + gmd5.MustEncryptString(authorization)
|
||||
jwtSign, _ := g.Cfg().Get(ctx, "jwt.sign", "hotgo")
|
||||
jwtSign := g.Cfg().MustGet(ctx, "jwt.sign", "hotgo")
|
||||
|
||||
data, ParseErr := jwt.ParseToken(authorization, jwtSign.Bytes())
|
||||
if ParseErr != nil {
|
||||
@@ -131,7 +131,7 @@ func inspectAuth(r *ghttp.Request, appName string) error {
|
||||
}
|
||||
|
||||
// 是否开启多端登录
|
||||
if multiPort, _ := g.Cfg().Get(ctx, "jwt.multiPort", true); !multiPort.Bool() {
|
||||
if multiPort := g.Cfg().MustGet(ctx, "jwt.multiPort", true); !multiPort.Bool() {
|
||||
key := consts.RedisJwtUserBind + appName + ":" + gconv.String(user.Id)
|
||||
originJwtToken, originErr := c.Get(ctx, key)
|
||||
if originErr != nil {
|
||||
@@ -151,6 +151,10 @@ func inspectAuth(r *ghttp.Request, appName string) error {
|
||||
if user != nil {
|
||||
customCtx.User = &model.Identity{
|
||||
Id: user.Id,
|
||||
Pid: user.Pid,
|
||||
DeptId: user.DeptId,
|
||||
RoleId: user.RoleId,
|
||||
RoleKey: user.RoleKey,
|
||||
Username: user.Username,
|
||||
RealName: user.RealName,
|
||||
Avatar: user.Avatar,
|
||||
@@ -159,8 +163,6 @@ func inspectAuth(r *ghttp.Request, appName string) error {
|
||||
VisitCount: user.VisitCount,
|
||||
LastTime: user.LastTime,
|
||||
LastIp: user.LastIp,
|
||||
Role: user.Role,
|
||||
RoleKey: user.RoleKey,
|
||||
Exp: user.Exp,
|
||||
Expires: user.Expires,
|
||||
App: user.App,
|
||||
|
@@ -8,9 +8,9 @@ package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/utility/charset"
|
||||
@@ -35,15 +35,6 @@ func (s *sMiddleware) ResponseHandler(r *ghttp.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := r.GetError(); err != nil {
|
||||
g.Log().Print(ctx, err)
|
||||
// 记录到自定义错误日志文件
|
||||
//g.Log("exception").Error(err)
|
||||
////返回固定的友好信息
|
||||
//r.Response.ClearBuffer()
|
||||
//r.Response.Writeln("服务器居然开小差了,请稍后再试吧!")
|
||||
}
|
||||
|
||||
// 已存在响应内容,且是comResponse返回的时,中断运行
|
||||
if r.Response.BufferLength() > 0 && comResponse != nil {
|
||||
return
|
||||
@@ -51,26 +42,15 @@ func (s *sMiddleware) ResponseHandler(r *ghttp.Request) {
|
||||
|
||||
if err = r.GetError(); err != nil {
|
||||
// 记录到自定义错误日志文件
|
||||
g.Log("exception").Print(r.Context(), "exception:", err)
|
||||
g.Log("exception").Print(ctx, "exception:", err)
|
||||
|
||||
code = consts.CodeInternalError
|
||||
message = "服务器居然开小差了,请稍后再试吧!"
|
||||
code = gerror.Code(err).Code()
|
||||
message = err.Error()
|
||||
|
||||
// 是否输出错误到页面
|
||||
if debug, _ := g.Cfg().Get(ctx, "hotgo.debug", true); debug.Bool() {
|
||||
if g.Cfg().MustGet(ctx, "hotgo.debug", true).Bool() {
|
||||
data = charset.GetStack(err)
|
||||
message = err.Error()
|
||||
}
|
||||
|
||||
//} else if data, err = r.GetHandlerResponse(); err != nil {
|
||||
// errCode := gerror.Code(err)
|
||||
// if errCode == gcode.CodeNil {
|
||||
// errCode = gcode.CodeInternalError
|
||||
// }
|
||||
// code = errCode.Code()
|
||||
// message = err.Error()
|
||||
//}
|
||||
|
||||
} else {
|
||||
data = r.GetHandlerResponse()
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ func (s *sMiddleware) WebSocketToken(r *ghttp.Request) {
|
||||
)
|
||||
|
||||
// 替换掉模块前缀
|
||||
routerPrefix, _ := g.Cfg().Get(ctx, "router.ws.prefix", "/socket")
|
||||
routerPrefix := g.Cfg().MustGet(ctx, "router.ws.prefix", "/socket")
|
||||
path := gstr.Replace(r.URL.Path, routerPrefix.String(), "", 1)
|
||||
|
||||
/// 不需要验证登录的路由地址
|
||||
|
@@ -16,8 +16,8 @@ import (
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/format"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sSysAttachment struct{}
|
||||
@@ -82,7 +82,7 @@ func (s *sSysAttachment) Status(ctx context.Context, in sysin.AttachmentStatusIn
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
@@ -124,7 +124,7 @@ func (s *sSysAttachment) View(ctx context.Context, in sysin.AttachmentViewInp) (
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sSysAttachment) List(ctx context.Context, in sysin.AttachmentListInp) (list []*sysin.AttachmentListModel, totalCount int64, err error) {
|
||||
func (s *sSysAttachment) List(ctx context.Context, in sysin.AttachmentListInp) (list []*sysin.AttachmentListModel, totalCount int, err error) {
|
||||
mod := dao.SysAttachment.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -152,14 +152,18 @@ func (s *sSysAttachment) List(ctx context.Context, in sysin.AttachmentListInp) (
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("updated_at desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
conf, err := service.SysConfig().GetUpload(ctx)
|
||||
if err != nil {
|
||||
return list, totalCount, err
|
||||
}
|
||||
for _, v := range list {
|
||||
v.SizeFormat = format.FileSize(v.Size)
|
||||
v.FileUrl = service.CommonUpload().LastUrl(ctx, v.FileUrl, consts.UploadDriveLocal)
|
||||
v.FileUrl = service.CommonUpload().LastUrl(ctx, conf, v.FileUrl, v.Drive)
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
|
@@ -14,7 +14,7 @@ import (
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sSysBlacklist struct{}
|
||||
@@ -79,7 +79,7 @@ func (s *sSysBlacklist) Status(ctx context.Context, in sysin.BlacklistStatusInp)
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func (s *sSysBlacklist) View(ctx context.Context, in sysin.BlacklistViewInp) (re
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sSysBlacklist) List(ctx context.Context, in sysin.BlacklistListInp) (list []*sysin.BlacklistListModel, totalCount int64, err error) {
|
||||
func (s *sSysBlacklist) List(ctx context.Context, in sysin.BlacklistListInp) (list []*sysin.BlacklistListModel, totalCount int, err error) {
|
||||
mod := dao.SysBlacklist.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -141,7 +141,7 @@ func (s *sSysBlacklist) List(ctx context.Context, in sysin.BlacklistListInp) (li
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
@@ -32,6 +32,55 @@ func init() {
|
||||
service.RegisterSysConfig(NewSysConfig())
|
||||
}
|
||||
|
||||
// GetLoadGenerate 获取本地生成配置
|
||||
func (s *sSysConfig) GetLoadGenerate(ctx context.Context) (conf *model.GenerateConfig, err error) {
|
||||
generate := g.Cfg().MustGet(ctx, "hggen")
|
||||
if err = gconv.Struct(generate, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// GetSms 获取短信配置
|
||||
func (s *sSysConfig) GetSms(ctx context.Context) (conf *model.SmsConfig, err error) {
|
||||
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "sms"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// GetGeo 获取地理配置
|
||||
func (s *sSysConfig) GetGeo(ctx context.Context) (conf *model.GeoConfig, err error) {
|
||||
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "geo"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// GetUpload 获取上传配置
|
||||
func (s *sSysConfig) GetUpload(ctx context.Context) (conf *model.UploadConfig, err error) {
|
||||
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "upload"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// GetSmtp 获取邮件配置
|
||||
func (s *sSysConfig) GetSmtp(ctx context.Context) (conf *model.EmailConfig, err error) {
|
||||
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "smtp"})
|
||||
@@ -47,6 +96,22 @@ func (s *sSysConfig) GetSmtp(ctx context.Context) (conf *model.EmailConfig, err
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// GetLoadSSL 获取本地日志配置
|
||||
func (s *sSysConfig) GetLoadSSL(ctx context.Context) (conf *model.SSLConfig, err error) {
|
||||
if err = g.Cfg().MustGet(ctx, "hotgo.ssl").Struct(&conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// GetLoadLog 获取本地日志配置
|
||||
func (s *sSysConfig) GetLoadLog(ctx context.Context) (conf *model.LogConfig, err error) {
|
||||
if err = g.Cfg().MustGet(ctx, "hotgo.log").Struct(&conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// GetConfigByGroup 获取指定分组的配置
|
||||
func (s *sSysConfig) GetConfigByGroup(ctx context.Context, in sysin.GetConfigInp) (*sysin.GetConfigModel, error) {
|
||||
if in.Group == "" {
|
||||
@@ -60,7 +125,7 @@ func (s *sSysConfig) GetConfigByGroup(ctx context.Context, in sysin.GetConfigInp
|
||||
if err := mod.Fields("key", "value", "type").Where("group", in.Group).Scan(&models); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
isDemo, _ := g.Cfg().Get(ctx, "hotgo.isDemo", false)
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false)
|
||||
|
||||
if len(models) > 0 {
|
||||
res.List = make(g.Map, len(models))
|
||||
@@ -84,19 +149,7 @@ func (s *sSysConfig) ConversionType(ctx context.Context, models *entity.SysConfi
|
||||
if models == nil {
|
||||
return nil, gerror.New("数据不存在")
|
||||
}
|
||||
|
||||
switch models.Type {
|
||||
case consts.ConfigTypeInt:
|
||||
value = gconv.Int64(models.Value)
|
||||
return
|
||||
case consts.ConfigTypeBool:
|
||||
value = gconv.Bool(models.Value)
|
||||
return
|
||||
default:
|
||||
value = gconv.String(models.Value)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
return consts.ConvType(models.Value, models.Type), nil
|
||||
}
|
||||
|
||||
// UpdateConfigByGroup 更新指定分组的配置
|
||||
@@ -112,7 +165,7 @@ func (s *sSysConfig) UpdateConfigByGroup(ctx context.Context, in sysin.UpdateCon
|
||||
return err
|
||||
}
|
||||
|
||||
err := dao.SysConfig.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
||||
err := dao.SysConfig.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
for k, v := range in.List {
|
||||
row := s.getConfigByKey(k, models)
|
||||
// 新增
|
||||
|
@@ -17,7 +17,7 @@ import (
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sSysCron struct{}
|
||||
@@ -103,7 +103,7 @@ func (s *sSysCron) Status(ctx context.Context, in sysin.CronStatusInp) (err erro
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
@@ -147,7 +147,7 @@ func (s *sSysCron) View(ctx context.Context, in sysin.CronViewInp) (res *sysin.C
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sSysCron) List(ctx context.Context, in sysin.CronListInp) (list []*sysin.CronListModel, totalCount int64, err error) {
|
||||
func (s *sSysCron) List(ctx context.Context, in sysin.CronListInp) (list []*sysin.CronListModel, totalCount int, err error) {
|
||||
mod := dao.SysCron.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -170,7 +170,7 @@ func (s *sSysCron) List(ctx context.Context, in sysin.CronListInp) (list []*sysi
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
@@ -16,8 +16,8 @@ import (
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/tree"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sSysCronGroup struct{}
|
||||
@@ -82,7 +82,7 @@ func (s *sSysCronGroup) Status(ctx context.Context, in sysin.CronGroupStatusInp)
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func (s *sSysCronGroup) View(ctx context.Context, in sysin.CronGroupViewInp) (re
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sSysCronGroup) List(ctx context.Context, in sysin.CronGroupListInp) (list []*sysin.CronGroupListModel, totalCount int64, err error) {
|
||||
func (s *sSysCronGroup) List(ctx context.Context, in sysin.CronGroupListInp) (list []*sysin.CronGroupListModel, totalCount int, err error) {
|
||||
mod := dao.SysCronGroup.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -148,7 +148,7 @@ func (s *sSysCronGroup) List(ctx context.Context, in sysin.CronGroupListInp) (li
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
242
server/internal/logic/sys/curd_demo.go
Normal file
242
server/internal/logic/sys/curd_demo.go
Normal file
@@ -0,0 +1,242 @@
|
||||
// Package sys
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
// @AutoGenerate Version 2.1.0
|
||||
// @AutoGenerate Date 2023-01-18 15:19:42
|
||||
//
|
||||
package sys
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/hgorm"
|
||||
"hotgo/internal/model/input/form"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/excel"
|
||||
"hotgo/utility/validate"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type sSysCurdDemo struct{}
|
||||
|
||||
func NewSysCurdDemo() *sSysCurdDemo {
|
||||
return &sSysCurdDemo{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterSysCurdDemo(NewSysCurdDemo())
|
||||
}
|
||||
|
||||
// Model 生成演示Orm模型
|
||||
func (s *sSysCurdDemo) Model(ctx context.Context) *gdb.Model {
|
||||
return dao.SysGenCurdDemo.Ctx(ctx)
|
||||
}
|
||||
|
||||
// List 获取生成演示列表
|
||||
func (s *sSysCurdDemo) List(ctx context.Context, in sysin.CurdDemoListInp) (list []*sysin.CurdDemoListModel, totalCount int, err error) {
|
||||
mod := dao.SysGenCurdDemo.Ctx(ctx)
|
||||
|
||||
// 查询ID
|
||||
if in.Id > 0 {
|
||||
mod = mod.Where(dao.SysGenCurdDemo.Columns().Id, in.Id)
|
||||
}
|
||||
|
||||
// 查询状态
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where(dao.SysGenCurdDemo.Columns().Status, in.Status)
|
||||
}
|
||||
|
||||
// 查询创建时间
|
||||
if len(in.CreatedAt) == 2 {
|
||||
mod = mod.WhereBetween(dao.SysGenCurdDemo.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
|
||||
}
|
||||
|
||||
// 查询分类名称
|
||||
if in.TestCategoryName != "" {
|
||||
mod = mod.WhereLike(dao.TestCategory.Columns().Name, in.TestCategoryName)
|
||||
}
|
||||
|
||||
// 关联表testCategory
|
||||
mod = mod.LeftJoin(hgorm.GenJoinOnRelation(
|
||||
dao.SysGenCurdDemo.Table(), dao.SysGenCurdDemo.Columns().CategoryId, // 主表表名,关联条件
|
||||
dao.TestCategory.Table(), "testCategory", dao.TestCategory.Columns().Id, // 关联表表名,别名,关联条件
|
||||
)...)
|
||||
|
||||
totalCount, err = mod.Clone().Count(1)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
if totalCount == 0 {
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
//关联表select
|
||||
fields, err := hgorm.GenJoinSelect(ctx, sysin.CurdDemoListModel{}, dao.SysGenCurdDemo, []*hgorm.Join{
|
||||
{Dao: dao.TestCategory, Alias: "testCategory"},
|
||||
})
|
||||
if err = mod.Fields(fields).Handler(hgorm.HandlerFilterAuth).Page(in.Page, in.PerPage).OrderAsc(dao.SysGenCurdDemo.Columns().Sort).OrderDesc(dao.SysGenCurdDemo.Columns().Id).Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
// Export 导出生成演示
|
||||
func (s *sSysCurdDemo) Export(ctx context.Context, in sysin.CurdDemoListInp) (err error) {
|
||||
list, totalCount, err := s.List(ctx, in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 字段的排序是依据tags的字段顺序,如果你不想使用默认的排序方式,可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
|
||||
tags, err := convert.GetEntityDescTags(sysin.CurdDemoExportModel{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
fileName = "导出生成演示-" + gctx.CtxId(ctx) + ".xlsx"
|
||||
sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
|
||||
exports []sysin.CurdDemoExportModel
|
||||
)
|
||||
|
||||
err = gconv.Scan(list, &exports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改/新增生成演示
|
||||
func (s *sSysCurdDemo) Edit(ctx context.Context, in sysin.CurdDemoEditInp) (err error) {
|
||||
// 修改
|
||||
if in.Id > 0 {
|
||||
in.UpdatedBy = contexts.GetUserId(ctx)
|
||||
_, err = dao.SysGenCurdDemo.Ctx(ctx).
|
||||
FieldsEx(
|
||||
dao.SysGenCurdDemo.Columns().Id,
|
||||
dao.SysGenCurdDemo.Columns().CreatedBy,
|
||||
dao.SysGenCurdDemo.Columns().CreatedAt,
|
||||
dao.SysGenCurdDemo.Columns().DeletedAt,
|
||||
).
|
||||
Where(dao.SysGenCurdDemo.Columns().Id, in.Id).Data(in).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
in.CreatedBy = contexts.GetUserId(ctx)
|
||||
_, err = dao.SysGenCurdDemo.Ctx(ctx).
|
||||
FieldsEx(
|
||||
dao.SysGenCurdDemo.Columns().Id,
|
||||
dao.SysGenCurdDemo.Columns().UpdatedBy,
|
||||
dao.SysGenCurdDemo.Columns().DeletedAt,
|
||||
).
|
||||
Data(in).Insert()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除生成演示
|
||||
func (s *sSysCurdDemo) Delete(ctx context.Context, in sysin.CurdDemoDeleteInp) (err error) {
|
||||
_, err = dao.SysGenCurdDemo.Ctx(ctx).Where(dao.SysGenCurdDemo.Columns().Id, in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxSort 获取生成演示最大排序
|
||||
func (s *sSysCurdDemo) MaxSort(ctx context.Context, in sysin.CurdDemoMaxSortInp) (res *sysin.CurdDemoMaxSortModel, err error) {
|
||||
if err = dao.SysGenCurdDemo.Ctx(ctx).Fields(dao.SysGenCurdDemo.Columns().Sort).OrderDesc(dao.SysGenCurdDemo.Columns().Sort).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.Sort = res.Sort + g.Cfg().MustGet(ctx, "hotgo.admin.maxSortIncrement").Int()
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// View 获取生成演示指定信息
|
||||
func (s *sSysCurdDemo) View(ctx context.Context, in sysin.CurdDemoViewInp) (res *sysin.CurdDemoViewModel, err error) {
|
||||
if err = dao.SysGenCurdDemo.Ctx(ctx).Where(dao.SysGenCurdDemo.Columns().Id, in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Status 更新生成演示状态
|
||||
func (s *sSysCurdDemo) Status(ctx context.Context, in sysin.CurdDemoStatusInp) (err error) {
|
||||
if in.Id <= 0 {
|
||||
err = gerror.New("ID不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
if in.Status <= 0 {
|
||||
err = gerror.New("状态不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dao.SysGenCurdDemo.Ctx(ctx).Where(dao.SysGenCurdDemo.Columns().Id, in.Id).Data(dao.SysGenCurdDemo.Columns().Status, in.Status).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Switch 更新生成演示开关
|
||||
func (s *sSysCurdDemo) Switch(ctx context.Context, in sysin.CurdDemoSwitchInp) (err error) {
|
||||
var fields = []string{
|
||||
dao.SysGenCurdDemo.Columns().Switch,
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
if !validate.InSliceString(fields, in.Key) {
|
||||
err = gerror.New("开关键名不在白名单")
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dao.SysGenCurdDemo.Ctx(ctx).Where(dao.SysGenCurdDemo.Columns().Id, in.Id).Data(in.Key, in.Value).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@@ -82,7 +82,7 @@ func (s *sSysDictData) Edit(ctx context.Context, in sysin.DictDataEditInp) (err
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sSysDictData) List(ctx context.Context, in sysin.DictDataListInp) (list []*sysin.DictDataListModel, totalCount int64, err error) {
|
||||
func (s *sSysDictData) List(ctx context.Context, in sysin.DictDataListInp) (list []*sysin.DictDataListModel, totalCount int, err error) {
|
||||
mod := dao.SysDictData.Ctx(ctx)
|
||||
// 类型ID
|
||||
if in.TypeID > 0 {
|
||||
@@ -117,7 +117,7 @@ func (s *sSysDictData) List(ctx context.Context, in sysin.DictDataListInp) (list
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("sort asc,id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
@@ -128,3 +128,22 @@ func (s *sSysDictData) List(ctx context.Context, in sysin.DictDataListInp) (list
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
// Select 获取列表
|
||||
func (s *sSysDictData) Select(ctx context.Context, in sysin.DataSelectInp) (list sysin.DataSelectModel, err error) {
|
||||
mod := dao.SysDictData.Ctx(ctx).Where("type", in.Type)
|
||||
if in.Type != "" {
|
||||
mod = mod.Where("type", in.Type)
|
||||
}
|
||||
|
||||
if err = mod.Order("sort asc,id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, err
|
||||
}
|
||||
|
||||
for k, v := range list {
|
||||
list[k].Value = consts.ConvType(v.Value, v.ValueType)
|
||||
list[k].Key = list[k].Value
|
||||
}
|
||||
return list, err
|
||||
}
|
||||
|
@@ -171,3 +171,40 @@ func (s *sSysDictType) Select(ctx context.Context, in sysin.DictTypeSelectInp) (
|
||||
|
||||
return tree.GenTree(typeList), nil
|
||||
}
|
||||
|
||||
// TreeSelect 获取类型关系树选项
|
||||
func (s *sSysDictType) TreeSelect(ctx context.Context, in sysin.DictTreeSelectInp) (list sysin.DictTreeSelectModel, err error) {
|
||||
var (
|
||||
mod = dao.SysDictType.Ctx(ctx)
|
||||
models []*entity.SysDictType
|
||||
typeList []g.Map
|
||||
)
|
||||
|
||||
if err = mod.Order("pid asc,sort asc").Scan(&models); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(models); i++ {
|
||||
typeList = append(typeList, g.Map{
|
||||
"index": models[i].Id,
|
||||
"key": models[i].Id,
|
||||
"label": models[i].Name,
|
||||
"id": models[i].Id,
|
||||
"pid": models[i].Pid,
|
||||
"name": models[i].Name,
|
||||
"sort": models[i].Sort,
|
||||
"created_at": models[i].CreatedAt,
|
||||
"status": models[i].Status,
|
||||
})
|
||||
}
|
||||
|
||||
maps := tree.GenTree(typeList)
|
||||
for _, v := range maps {
|
||||
// 父类一律禁止选中
|
||||
if _, ok := v["children"]; ok {
|
||||
v["disabled"] = true
|
||||
}
|
||||
}
|
||||
return tree.GenTree(typeList), nil
|
||||
}
|
||||
|
259
server/internal/logic/sys/gen_codes.go
Normal file
259
server/internal/logic/sys/gen_codes.go
Normal file
@@ -0,0 +1,259 @@
|
||||
// Package sys
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package sys
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/hggen"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sSysGenCodes struct{}
|
||||
|
||||
func NewSysGenCodes() *sSysGenCodes {
|
||||
return &sSysGenCodes{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterSysGenCodes(NewSysGenCodes())
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
func (s *sSysGenCodes) Delete(ctx context.Context, in sysin.GenCodesDeleteInp) error {
|
||||
_, err := dao.SysGenCodes.Ctx(ctx).Where("id", in.Id).Delete()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Edit 修改/新增
|
||||
func (s *sSysGenCodes) Edit(ctx context.Context, in sysin.GenCodesEditInp) (res *sysin.GenCodesEditModel, err error) {
|
||||
if in.GenType == 0 {
|
||||
err = gerror.New("生成类型不能为空")
|
||||
return nil, err
|
||||
}
|
||||
if in.VarName == "" {
|
||||
err = gerror.New("实体名称不能为空")
|
||||
return nil, err
|
||||
}
|
||||
// 修改
|
||||
in.UpdatedAt = gtime.Now()
|
||||
if in.Id > 0 {
|
||||
_, err = dao.SysGenCodes.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
return &sysin.GenCodesEditModel{SysGenCodes: in.SysGenCodes}, nil
|
||||
}
|
||||
|
||||
// 新增
|
||||
if in.Options.IsNil() {
|
||||
in.Options = gjson.New(consts.NilJsonToString)
|
||||
}
|
||||
|
||||
in.MasterColumns = gjson.New("[]")
|
||||
in.Status = consts.GenCodesStatusWait
|
||||
in.CreatedAt = gtime.Now()
|
||||
id, err := dao.SysGenCodes.Ctx(ctx).Data(in).InsertAndGetId()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
in.Id = id
|
||||
return &sysin.GenCodesEditModel{SysGenCodes: in.SysGenCodes}, nil
|
||||
}
|
||||
|
||||
// Status 更新部门状态
|
||||
func (s *sSysGenCodes) Status(ctx context.Context, in sysin.GenCodesStatusInp) (err error) {
|
||||
if in.Id <= 0 {
|
||||
err = gerror.New("ID不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
if in.Status <= 0 {
|
||||
err = gerror.New("状态不能为空")
|
||||
return err
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改
|
||||
_, err = dao.SysGenCodes.Ctx(ctx).Where("id", in.Id).Data("status", in.Status).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxSort 最大排序
|
||||
func (s *sSysGenCodes) MaxSort(ctx context.Context, in sysin.GenCodesMaxSortInp) (*sysin.GenCodesMaxSortModel, error) {
|
||||
var res sysin.GenCodesMaxSortModel
|
||||
if in.Id > 0 {
|
||||
if err := dao.SysGenCodes.Ctx(ctx).Where("id", in.Id).Order("sort desc").Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
res.Sort = res.Sort + 10
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// View 获取指定字典类型信息
|
||||
func (s *sSysGenCodes) View(ctx context.Context, in sysin.GenCodesViewInp) (res *sysin.GenCodesViewModel, err error) {
|
||||
if err = dao.SysGenCodes.Ctx(ctx).Where("id", in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sSysGenCodes) List(ctx context.Context, in sysin.GenCodesListInp) (list []*sysin.GenCodesListModel, totalCount int, err error) {
|
||||
mod := dao.SysGenCodes.Ctx(ctx)
|
||||
|
||||
if in.GenType > 0 {
|
||||
mod = mod.Where("gen_type", in.GenType)
|
||||
}
|
||||
|
||||
if in.VarName != "" {
|
||||
mod = mod.Where("var_name", in.VarName)
|
||||
}
|
||||
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where("status", in.Status)
|
||||
}
|
||||
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
if totalCount == 0 {
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
// Selects 选项
|
||||
func (s *sSysGenCodes) Selects(ctx context.Context, in sysin.GenCodesSelectsInp) (res *sysin.GenCodesSelectsModel, err error) {
|
||||
return hggen.TableSelects(ctx, in)
|
||||
}
|
||||
|
||||
// TableSelect 表选项
|
||||
func (s *sSysGenCodes) TableSelect(ctx context.Context, in sysin.GenCodesTableSelectInp) (res []*sysin.GenCodesTableSelectModel, err error) {
|
||||
var (
|
||||
sql = "SELECT TABLE_NAME as value, TABLE_COMMENT as label FROM information_schema.`TABLES` WHERE TABLE_SCHEMA = '%s'"
|
||||
config = g.DB(in.Name).GetConfig()
|
||||
disableTables = g.Cfg().MustGet(ctx, "hggen.disableTables").Strings()
|
||||
lists []*sysin.GenCodesTableSelectModel
|
||||
)
|
||||
|
||||
err = g.DB(in.Name).Ctx(ctx).Raw(fmt.Sprintf(sql, config.Name)).Scan(&lists)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, v := range lists {
|
||||
if gstr.InArray(disableTables, v.Value) {
|
||||
continue
|
||||
}
|
||||
|
||||
row := new(sysin.GenCodesTableSelectModel)
|
||||
row = v
|
||||
row.DefTableComment = v.Label
|
||||
row.DaoName = gstr.CaseCamel(gstr.SubStrFromEx(v.Value, config.Prefix))
|
||||
row.DefVarName = row.DaoName
|
||||
row.DefAlias = gstr.CaseCamelLower(gstr.SubStrFromEx(v.Value, config.Prefix))
|
||||
row.Name = fmt.Sprintf("%s (%s)", v.Value, v.Label)
|
||||
row.Label = row.Name
|
||||
res = append(res, row)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ColumnSelect 表字段选项
|
||||
func (s *sSysGenCodes) ColumnSelect(ctx context.Context, in sysin.GenCodesColumnSelectInp) (res []*sysin.GenCodesColumnSelectModel, err error) {
|
||||
var (
|
||||
sql = "select COLUMN_NAME as value,COLUMN_COMMENT as label from information_schema.COLUMNS where TABLE_SCHEMA = '%s' and TABLE_NAME = '%s'"
|
||||
config = g.DB(in.Name).GetConfig()
|
||||
)
|
||||
|
||||
err = g.DB(in.Name).Ctx(ctx).Raw(fmt.Sprintf(sql, config.Name, in.Table)).Scan(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(res) == 0 {
|
||||
return nil, gerror.Newf("table does not exist:%v", in.Table)
|
||||
}
|
||||
|
||||
for k, v := range res {
|
||||
res[k].Name = fmt.Sprintf("%s (%s)", v.Value, v.Label)
|
||||
res[k].Label = res[k].Name
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ColumnList 表字段列表
|
||||
func (s *sSysGenCodes) ColumnList(ctx context.Context, in sysin.GenCodesColumnListInp) (res []*sysin.GenCodesColumnListModel, err error) {
|
||||
return hggen.TableColumns(ctx, in)
|
||||
}
|
||||
|
||||
// Preview 生成预览
|
||||
func (s *sSysGenCodes) Preview(ctx context.Context, in sysin.GenCodesPreviewInp) (res *sysin.GenCodesPreviewModel, err error) {
|
||||
return hggen.Preview(ctx, in)
|
||||
}
|
||||
|
||||
// Build 提交生成
|
||||
func (s *sSysGenCodes) Build(ctx context.Context, in sysin.GenCodesBuildInp) (err error) {
|
||||
// 先保存配置
|
||||
_, err = s.Edit(ctx, sysin.GenCodesEditInp{SysGenCodes: in.SysGenCodes})
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.Status(ctx, sysin.GenCodesStatusInp{Id: in.Id, Status: consts.GenCodesStatusOk})
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
if err = hggen.Build(ctx, in); err != nil {
|
||||
_ = s.Status(ctx, sysin.GenCodesStatusInp{Id: in.Id, Status: consts.GenCodesStatusFail})
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
@@ -9,6 +9,7 @@ package sys
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
@@ -21,13 +22,11 @@ import (
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/location"
|
||||
"hotgo/internal/library/queue"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/excel"
|
||||
"hotgo/utility/validate"
|
||||
"time"
|
||||
)
|
||||
|
||||
type sSysLog struct{}
|
||||
@@ -90,23 +89,10 @@ func (s *sSysLog) Export(ctx context.Context, in sysin.LogListInp) (err error) {
|
||||
exportList = append(exportList, row)
|
||||
}
|
||||
|
||||
// 强转类型
|
||||
writer := ghttp.RequestFromCtx(ctx).Response.Writer
|
||||
w, _ := interface{}(writer).(*ghttp.ResponseWriter)
|
||||
|
||||
if err = excel.ExportByStruct(w, titleList, gconv.Interfaces(exportList), fileName, sheetName); err != nil {
|
||||
err = gerror.Wrap(err, "ExportByStruct:")
|
||||
if err = excel.ExportByStructs(ctx, titleList, exportList, fileName, sheetName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 加入到上下文
|
||||
contexts.SetResponse(ctx, &model.Response{
|
||||
Code: consts.CodeOK,
|
||||
Message: "导出成功",
|
||||
Timestamp: time.Now().Unix(),
|
||||
TraceID: gctx.CtxId(ctx),
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,60 +102,42 @@ func (s *sSysLog) RealWrite(ctx context.Context, commonLog entity.SysLog) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := result.LastInsertId(); err != nil {
|
||||
if _, err = result.LastInsertId(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AutoLog 根据配置自动记录请求日志
|
||||
func (s *sSysLog) AutoLog(ctx context.Context) (err error) {
|
||||
// 日志开关
|
||||
logSwitch, _ := g.Cfg().Get(ctx, "hotgo.log.switch", true)
|
||||
if !logSwitch.Bool() {
|
||||
config, err := service.SysConfig().GetLoadLog(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !config.Switch {
|
||||
return nil
|
||||
}
|
||||
|
||||
data := s.AnalysisLog(ctx)
|
||||
|
||||
// 判断模块是否需要记录
|
||||
module, _ := g.Cfg().Get(ctx, "hotgo.log.module", nil)
|
||||
if module == nil {
|
||||
return nil
|
||||
}
|
||||
if exist := validate.InSliceExistStr(module.Strings(), data.Module); !exist {
|
||||
if ok := validate.InSliceExistStr(config.Module, data.Module); !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 判断状态码是否需要记录
|
||||
code, _ := g.Cfg().Get(ctx, "hotgo.log.skipCode", nil)
|
||||
if code != nil {
|
||||
if exist := validate.InSliceExistStr(code.Strings(), gconv.String(data.ErrorCode)); exist {
|
||||
return nil
|
||||
}
|
||||
if ok := validate.InSliceExistStr(config.SkipCode, gconv.String(data.ErrorCode)); ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 是否开启队列
|
||||
queueSwitch, _ := g.Cfg().Get(ctx, "hotgo.log.queue", true)
|
||||
if queueSwitch.Bool() {
|
||||
// 获取生产者实例
|
||||
queueInstance, err := queue.InstanceProducer()
|
||||
if config.Queue {
|
||||
q, err := queue.InstanceProducer()
|
||||
if err != nil {
|
||||
queue.FatalLog(ctx, "InstanceProducer异常", err)
|
||||
queue.FatalLog(ctx, "queue.InstanceProducer err:%+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// 生产消息
|
||||
mqMsg, err := queueInstance.SendMsg(consts.QueueLogTopic, gconv.String(data))
|
||||
|
||||
// 记录生产日志
|
||||
mqMsg, err := q.SendMsg(consts.QueueLogTopic, gconv.String(data))
|
||||
queue.ProducerLog(ctx, consts.QueueLogTopic, mqMsg.MsgId, err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return s.RealWrite(ctx, data)
|
||||
}
|
||||
|
||||
@@ -185,7 +153,6 @@ func (s *sSysLog) QueueJob(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
|
||||
// AnalysisLog 解析日志数据
|
||||
func (s *sSysLog) AnalysisLog(ctx context.Context) entity.SysLog {
|
||||
|
||||
var (
|
||||
modelContext = contexts.Get(ctx)
|
||||
response = modelContext.Response
|
||||
@@ -193,15 +160,14 @@ func (s *sSysLog) AnalysisLog(ctx context.Context) entity.SysLog {
|
||||
request = ghttp.RequestFromCtx(ctx)
|
||||
module = modelContext.Module
|
||||
clientIp = request.GetClientIp()
|
||||
locationData = location.GetLocation(ctx, clientIp)
|
||||
postData = "{}"
|
||||
getData = "{}"
|
||||
headerData = "{}"
|
||||
postData = gjson.New(consts.NilJsonToString)
|
||||
getData = gjson.New(consts.NilJsonToString)
|
||||
headerData = gjson.New(consts.NilJsonToString)
|
||||
data = entity.SysLog{}
|
||||
memberId int64 = 0
|
||||
errorCode = 0
|
||||
errorMsg = ""
|
||||
errorData = "{}"
|
||||
errorData = gjson.New(consts.NilJsonToString)
|
||||
traceID = ""
|
||||
timestamp int64 = 0
|
||||
appId = ""
|
||||
@@ -214,23 +180,27 @@ func (s *sSysLog) AnalysisLog(ctx context.Context) entity.SysLog {
|
||||
traceID = response.TraceID
|
||||
timestamp = response.Timestamp
|
||||
if len(gconv.String(response.Error)) > 0 {
|
||||
errorData = gconv.String(response.Error)
|
||||
errorData = gjson.New(response.Error)
|
||||
}
|
||||
}
|
||||
|
||||
// 请求头
|
||||
if reqHeadersBytes, _ := json.Marshal(request.Header); len(gconv.String(reqHeadersBytes)) > 0 {
|
||||
headerData = gconv.String(reqHeadersBytes)
|
||||
headerData = gjson.New(reqHeadersBytes)
|
||||
}
|
||||
|
||||
// post参数
|
||||
if gconv.String(request.PostForm) != "" {
|
||||
postData = gconv.String(request.PostForm)
|
||||
postData = gjson.New(gconv.String(request.PostForm))
|
||||
}
|
||||
|
||||
if postData.IsNil() {
|
||||
postData = gjson.New(request.GetBodyString())
|
||||
}
|
||||
|
||||
// get参数
|
||||
if len(request.URL.Query()) > 0 {
|
||||
getData = gconv.String(request.URL.Query())
|
||||
getData = gjson.New(request.URL.Query())
|
||||
}
|
||||
|
||||
// 当前登录用户
|
||||
@@ -239,6 +209,25 @@ func (s *sSysLog) AnalysisLog(ctx context.Context) entity.SysLog {
|
||||
appId = user.App
|
||||
}
|
||||
|
||||
var ipData = new(location.IpLocationData)
|
||||
//if validate.IsPublicIp(clientIp) {
|
||||
// ipData, err := location.GetLocation(ctx, clientIp)
|
||||
// if err != nil {
|
||||
// g.Log().Errorf(ctx, "location.GetLocation err:%+v", err)
|
||||
// }
|
||||
// if ipData == nil {
|
||||
// ipData = new(location.IpLocationData)
|
||||
// }
|
||||
//}
|
||||
|
||||
ipData, err := location.GetLocation(ctx, clientIp)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "location.GetLocation err:%+v", err)
|
||||
}
|
||||
if ipData == nil {
|
||||
ipData = new(location.IpLocationData)
|
||||
}
|
||||
|
||||
data = entity.SysLog{
|
||||
AppId: appId,
|
||||
MerchantId: 0,
|
||||
@@ -250,8 +239,8 @@ func (s *sSysLog) AnalysisLog(ctx context.Context) entity.SysLog {
|
||||
PostData: postData,
|
||||
HeaderData: headerData,
|
||||
Ip: clientIp,
|
||||
ProvinceId: locationData.ProvinceCode,
|
||||
CityId: locationData.CityCode,
|
||||
ProvinceId: ipData.ProvinceCode,
|
||||
CityId: ipData.CityCode,
|
||||
ErrorCode: errorCode,
|
||||
ErrorMsg: errorMsg,
|
||||
ErrorData: errorData,
|
||||
@@ -271,13 +260,13 @@ func (s *sSysLog) View(ctx context.Context, in sysin.LogViewInp) (res *sysin.Log
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return nil, err
|
||||
}
|
||||
isDemo, _ := g.Cfg().Get(ctx, "hotgo.isDemo", false)
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false)
|
||||
if isDemo.Bool() {
|
||||
res.HeaderData = `{
|
||||
"none": [
|
||||
"` + consts.DemoTips + `"
|
||||
]
|
||||
}`
|
||||
// res.HeaderData = `{
|
||||
// "none": [
|
||||
// "` + consts.DemoTips + `"
|
||||
// ]
|
||||
//}`
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
@@ -293,7 +282,7 @@ func (s *sSysLog) Delete(ctx context.Context, in sysin.LogDeleteInp) error {
|
||||
}
|
||||
|
||||
// List 列表
|
||||
func (s *sSysLog) List(ctx context.Context, in sysin.LogListInp) (list []*sysin.LogListModel, totalCount int64, err error) {
|
||||
func (s *sSysLog) List(ctx context.Context, in sysin.LogListInp) (list []*sysin.LogListModel, totalCount int, err error) {
|
||||
mod := dao.SysLog.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -353,11 +342,11 @@ func (s *sSysLog) List(ctx context.Context, in sysin.LogListInp) (list []*sysin.
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
isDemo, _ := g.Cfg().Get(ctx, "hotgo.isDemo", false)
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false)
|
||||
for i := 0; i < len(list); i++ {
|
||||
// 管理员
|
||||
if list[i].AppId == consts.AppAdmin {
|
||||
@@ -394,11 +383,11 @@ func (s *sSysLog) List(ctx context.Context, in sysin.LogListInp) (list []*sysin.
|
||||
}
|
||||
|
||||
if isDemo.Bool() {
|
||||
list[i].HeaderData = `{
|
||||
"none": [
|
||||
"` + consts.DemoTips + `"
|
||||
]
|
||||
}`
|
||||
// list[i].HeaderData = `{
|
||||
// "none": [
|
||||
// "` + consts.DemoTips + `"
|
||||
// ]
|
||||
//}`
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ import (
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sSysProvinces struct{}
|
||||
@@ -80,7 +80,7 @@ func (s *sSysProvinces) Status(ctx context.Context, in sysin.ProvincesStatusInp)
|
||||
return err
|
||||
}
|
||||
|
||||
if !convert.InSliceInt(consts.StatusMap, in.Status) {
|
||||
if !validate.InSliceInt(consts.StatusMap, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return err
|
||||
}
|
||||
@@ -123,7 +123,7 @@ func (s *sSysProvinces) View(ctx context.Context, in sysin.ProvincesViewInp) (re
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (s *sSysProvinces) List(ctx context.Context, in sysin.ProvincesListInp) (list []*sysin.ProvincesListModel, totalCount int64, err error) {
|
||||
func (s *sSysProvinces) List(ctx context.Context, in sysin.ProvincesListInp) (list []*sysin.ProvincesListModel, totalCount int, err error) {
|
||||
mod := dao.SysProvinces.Ctx(ctx)
|
||||
|
||||
// 访问路径
|
||||
@@ -146,7 +146,7 @@ func (s *sSysProvinces) List(ctx context.Context, in sysin.ProvincesListInp) (li
|
||||
return list, totalCount, nil
|
||||
}
|
||||
|
||||
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("id desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return list, totalCount, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user