Skip to content

Commit

Permalink
Merge pull request #62 from bufanyun/v2.0
Browse files Browse the repository at this point in the history
up
  • Loading branch information
maxbad committed May 9, 2024
2 parents 90ea290 + 817482b commit 406e3ef
Show file tree
Hide file tree
Showing 63 changed files with 663 additions and 469 deletions.
2 changes: 1 addition & 1 deletion server/addons/hgexample/logic/sys/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (s *sSysTable) Edit(ctx context.Context, in *sysin.TableEditInp) (err error

// 新增
in.CreatedBy = contexts.GetUserId(ctx)
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).Data(in).Insert(); err != nil {
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).Data(in).OmitEmptyData().Insert(); err != nil {
err = gerror.Wrap(err, "新增表格失败,请稍后重试!")
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/addons/hgexample/logic/sys/tree_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *sSysTreeTable) Edit(ctx context.Context, in *sysin.TableEditInp) (err e
}
} else {
in.CreatedBy = contexts.GetUserId(ctx)
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).Data(in).Insert(); err != nil {
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).Data(in).OmitEmptyData().Insert(); err != nil {
err = gerror.Wrap(err, "新增表格失败,请稍后重试!")
return err
}
Expand Down
10 changes: 0 additions & 10 deletions server/api/admin/loginlog/loginlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ type ExportReq struct {

type ExportRes struct{}

// ViewReq 获取登录日志指定信息
type ViewReq struct {
g.Meta `path:"/loginLog/view" method:"get" tags:"登录日志" summary:"获取登录日志指定信息"`
sysin.LoginLogViewInp
}

type ViewRes struct {
*sysin.LoginLogViewModel
}

// DeleteReq 删除登录日志
type DeleteReq struct {
g.Meta `path:"/loginLog/delete" method:"post" tags:"登录日志" summary:"删除登录日志"`
Expand Down
47 changes: 47 additions & 0 deletions server/internal/consts/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,56 @@
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package consts

import (
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
"hotgo/internal/library/dict"
"hotgo/internal/model"
"net/http"
)

func init() {
dict.RegisterEnums("HTTPMethod", "HTTP请求方式选项", HTTPMethodOptions)
dict.RegisterEnums("HTTPHandlerTime", "HTTP处理耗时选项", HTTPHandlerTimeOptions)
dict.RegisterEnums("HTTPApiCode", "HTTP接口状态码选项", HTTPApiCodeOptions)
}

const (
HTTPContentTypeXml = "text/xml"
HTTPContentTypeHtml = "text/html"
HTTPContentTypeStream = "text/event-stream"
HTTPContentTypeJson = "application/json"
)

// HTTPMethodOptions HTTP请求方式选项
var HTTPMethodOptions = []*model.Option{
dict.GenSuccessOption(http.MethodGet, "GET"),
dict.GenInfoOption(http.MethodPost, "POST"),
dict.GenSuccessOption(http.MethodPut, "PUT"),
dict.GenInfoOption(http.MethodDelete, "DELETE"),
}

const (
HTTPHandlerTime50 = "< 50"
HTTPHandlerTime200 = "< 200"
HTTPHandlerTime200To500 = "BETWEEN 200 AND 500"
HTTPHandlerTime500To1000 = "BETWEEN 500 AND 1000"
HTTPHandlerTime1000To10000 = "BETWEEN 1000 AND 10000"
HTTPHandlerTime10000UP = "> 10000"
)

// HTTPHandlerTimeOptions HTTP处理耗时选项
var HTTPHandlerTimeOptions = []*model.Option{
dict.GenSuccessOption(HTTPHandlerTime50, "50ms以内"),
dict.GenInfoOption(HTTPHandlerTime200, "200ms以内"),
dict.GenSuccessOption(HTTPHandlerTime200To500, "200~500ms"),
dict.GenSuccessOption(HTTPHandlerTime500To1000, "500~1000ms"),
dict.GenInfoOption(HTTPHandlerTime1000To10000, "1000~10000ms"),
dict.GenInfoOption(HTTPHandlerTime10000UP, "10000ms以上"),
}

// HTTPApiCodeOptions HTTP接口状态码选项
var HTTPApiCodeOptions = []*model.Option{
dict.GenSuccessOption(gcode.CodeOK.Code(), fmt.Sprintf("%v %v", gcode.CodeOK.Code(), "成功")),
dict.GenWarningOption(gcode.CodeNil.Code(), fmt.Sprintf("%v %v", gcode.CodeNil.Code(), "失败")),
}
12 changes: 0 additions & 12 deletions server/internal/controller/admin/sys/login_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ func (c *cLoginLog) Export(ctx context.Context, req *loginlog.ExportReq) (res *l
return
}

// View 获取指定登录日志信息
func (c *cLoginLog) View(ctx context.Context, req *loginlog.ViewReq) (res *loginlog.ViewRes, err error) {
data, err := service.SysLoginLog().View(ctx, &req.LoginLogViewInp)
if err != nil {
return
}

res = new(loginlog.ViewRes)
res.LoginLogViewModel = data
return
}

// Delete 删除登录日志
func (c *cLoginLog) Delete(ctx context.Context, req *loginlog.DeleteReq) (res *loginlog.DeleteRes, err error) {
err = service.SysLoginLog().Delete(ctx, &req.LoginLogDeleteInp)
Expand Down
125 changes: 125 additions & 0 deletions server/internal/global/httproutes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Package global
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2024 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package global

import (
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/util/gmeta"
"github.com/gogf/gf/v2/util/gtag"
"reflect"
"strings"
"sync"
)

// HTTPRouter http路由
type HTTPRouter struct {
ghttp.RouterItem
Tags string `json:"tags" dc:"接口所属的标签,用于接口分类"`
Summary string `json:"summary" dc:"接口/参数概要描述"`
Description string `json:"description" dc:"接口/参数详细描述"`
}

var (
httpRoutes map[string]*HTTPRouter
routeMutex sync.Mutex
shortTypeMapForTag = map[string]string{
gtag.SummaryShort: gtag.Summary,
gtag.SummaryShort2: gtag.Summary,
gtag.DescriptionShort: gtag.Description,
gtag.DescriptionShort2: gtag.Description,
}
)

// GetRequestRoute 获取当前请求路由属性
func GetRequestRoute(r *ghttp.Request) *HTTPRouter {
key := GenFilterRequestKey(r)
routes := LoadHTTPRoutes(r)
router, ok := routes[key]
if !ok {
return nil
}
return router
}

// GenFilterRequestKey 根据请求生成唯一key
func GenFilterRequestKey(r *ghttp.Request) string {
return GenRouteKey(r.Method, r.Request.URL.Path)
}

// GenFilterRouteKey 根据路由生成唯一key
func GenFilterRouteKey(r *ghttp.Router) string {
return GenRouteKey(r.Method, r.Uri)
}

// GenRouteKey 生成唯一key
func GenRouteKey(method, path string) string {
return strings.ToUpper(method) + " " + path
}

func LoadHTTPRoutes(r *ghttp.Request) map[string]*HTTPRouter {
if httpRoutes == nil {
routeMutex.Lock()
defer routeMutex.Unlock()

if httpRoutes != nil {
return httpRoutes
}

httpRoutes = make(map[string]*HTTPRouter, len(r.Server.GetRoutes()))
for _, v := range r.Server.GetRoutes() {
key := GenFilterRouteKey(v.Handler.Router)
if _, ok := httpRoutes[key]; !ok {
router := new(HTTPRouter)
router.RouterItem = v
httpRoutes[key] = setRouterMeta(router)
}
}

}
return httpRoutes
}

func setRouterMeta(router *HTTPRouter) *HTTPRouter {
if !router.RouterItem.Handler.Info.IsStrictRoute {
return router
}

var reflectValue = reflect.ValueOf(router.Handler.Info.Value.Interface())
for reflectValue.Kind() == reflect.Ptr {
reflectValue = reflectValue.Elem()
}

if reflectValue.Kind() != reflect.Func {
return router
}

var reflectType = reflect.TypeOf(router.Handler.Info.Value.Interface())
if reflectType.NumIn() != 2 || reflectType.NumOut() != 2 {
return router
}

var inputObject reflect.Value
if reflectType.In(1).Kind() == reflect.Ptr {
inputObject = reflect.New(reflectType.In(1).Elem()).Elem()
} else {
inputObject = reflect.New(reflectType.In(1)).Elem()
}

inputMetaMap := fillMapWithShortTags(gmeta.Data(inputObject.Interface()))
router.Tags = inputMetaMap["tags"]
router.Summary = inputMetaMap[gtag.Summary]
router.Description = inputMetaMap[gtag.Description]
return router
}

func fillMapWithShortTags(m map[string]string) map[string]string {
for k, v := range shortTypeMapForTag {
if m[v] == "" && m[k] != "" {
m[v] = m[k]
}
}
return m
}
2 changes: 1 addition & 1 deletion server/internal/library/addons/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Install(m Module) (err error) {
_, _ = GetModel(m.Ctx()).Where("id", record.Id).Delete()
}

if _, err = GetModel(m.Ctx()).Data(data).Insert(); err != nil {
if _, err = GetModel(m.Ctx()).Data(data).OmitEmptyData().Insert(); err != nil {
return err
}
return m.Install(ctx)
Expand Down
6 changes: 3 additions & 3 deletions server/internal/library/casbin/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (a *adapter) SavePolicy(model model.Model) (err error) {
}

if count := len(policyRules); count > 0 {
if _, err = a.model().Insert(policyRules); err != nil {
if _, err = a.model().OmitEmptyData().Insert(policyRules); err != nil {
return
}
}
Expand All @@ -160,7 +160,7 @@ func (a *adapter) SavePolicy(model model.Model) (err error) {

// AddPolicy adds a policy rule to the storage.
func (a *adapter) AddPolicy(sec string, ptype string, rule []string) (err error) {
_, err = a.model().Insert(a.buildPolicyRule(ptype, rule))
_, err = a.model().OmitEmptyData().Insert(a.buildPolicyRule(ptype, rule))
return
}

Expand All @@ -176,7 +176,7 @@ func (a *adapter) AddPolicies(sec string, ptype string, rules [][]string) (err e
policyRules = append(policyRules, a.buildPolicyRule(ptype, rule))
}

_, err = a.model().Insert(policyRules)
_, err = a.model().OmitEmptyData().Insert(policyRules)
return
}

Expand Down
4 changes: 3 additions & 1 deletion server/internal/library/dict/dict_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func GenHashOption(key interface{}, label string) *model.Option {
tag := "default"
if _, err := hash.Write(gconv.Bytes(label)); err == nil {
index := int(hash.Sum32()) % len(strings)
tag = strings[index]
if index < len(strings) {
tag = strings[index]
}
}
return &model.Option{
Key: key,
Expand Down
2 changes: 1 addition & 1 deletion server/internal/library/hggen/views/curd_generate_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
LogicWhereComments = "\n\t// 查询%s\n"
LogicWhereNoSupport = "\t// TODO 暂不支持生成[ %s ]查询方式,请自行补充此处代码!"
LogicEditUpdate = "\tif _, err = s.Model(ctx%s).\n\t\t\tFields(%sin.%sUpdateFields{}).\n\t\t\tWherePri(in.%s).Data(in).Update(); err != nil {\n\t\t\terr = gerror.Wrap(err, \"修改%s失败,请稍后重试!\")\n\t\t}\n\t\treturn"
LogicEditInsert = "\tif _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).\n\t\tFields(%sin.%sInsertFields{}).\n\t\tData(in).Insert(); err != nil {\n\t\terr = gerror.Wrap(err, \"新增%s失败,请稍后重试!\")\n\t}"
LogicEditInsert = "\tif _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).\n\t\tFields(%sin.%sInsertFields{}).\n\t\tData(in).OmitEmptyData().Insert(); err != nil {\n\t\terr = gerror.Wrap(err, \"新增%s失败,请稍后重试!\")\n\t}"
LogicEditUnique = "\t// 验证'%s'唯一\n\tif err = hgorm.IsUnique(ctx, &dao.%s, g.Map{dao.%s.Columns().%s: in.%s}, \"%s已存在\", in.Id); err != nil {\n\t\treturn\n\t}\n"
LogicSwitchUpdate = "g.Map{\n\t\tin.Key: in.Value,\n%s}"
LogicStatusUpdate = "g.Map{\n\t\tdao.%s.Columns().Status: in.Status,\n%s}"
Expand Down
2 changes: 1 addition & 1 deletion server/internal/library/hgorm/hook/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestSaveTenant_User(t *testing.T) {
cols.Status: consts.PayStatusWait,
}

_, err := dao.AddonHgexampleTenantOrder.Ctx(ctx).Data(data).Hook(hook.SaveTenant).Insert()
_, err := dao.AddonHgexampleTenantOrder.Ctx(ctx).Data(data).Hook(hook.SaveTenant).OmitEmptyData().Insert()
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/internal/library/network/tcp/msg_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (m *MsgParser) RegisterRPCRouter(routers ...interface{}) (err error) {

// RegisterInterceptor 注册拦截器
func (m *MsgParser) RegisterInterceptor(interceptors ...Interceptor) {
m.interceptors = append(interceptors, interceptors...)
m.interceptors = append(m.interceptors, interceptors...)
}

// Encoding 消息编码
Expand Down
2 changes: 1 addition & 1 deletion server/internal/library/storager/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func write(ctx context.Context, meta *FileMeta, fullPath string) (models *entity
Status: consts.StatusEnabled,
}

id, err := GetModel(ctx).Data(models).InsertAndGetId()
id, err := GetModel(ctx).Data(models).OmitEmptyData().InsertAndGetId()
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/internal/logic/admin/cash.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (s *sAdminCash) Apply(ctx context.Context, in *adminin.CashApplyInp) (err e
"status": consts.CashStatusWait,
"msg": "",
"ip": location.GetClientIp(ghttp.RequestFromCtx(ctx)),
}).InsertAndGetId()
}).OmitEmptyData().InsertAndGetId()
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions server/internal/logic/admin/credits_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *sAdminCreditsLog) SaveBalance(ctx context.Context, in *adminin.CreditsL
data.Ip = in.Ip
data.MapId = in.MapId

_, err = dao.AdminCreditsLog.Ctx(ctx).Data(data).Insert()
_, err = dao.AdminCreditsLog.Ctx(ctx).Data(data).OmitEmptyData().Insert()
return
}

Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *sAdminCreditsLog) SaveIntegral(ctx context.Context, in *adminin.Credits
data.Ip = in.Ip
data.MapId = in.MapId

_, err = dao.AdminCreditsLog.Ctx(ctx).Data(data).Insert()
_, err = dao.AdminCreditsLog.Ctx(ctx).Data(data).OmitEmptyData().Insert()
return
}

Expand Down
2 changes: 1 addition & 1 deletion server/internal/logic/admin/dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *sAdminDept) Edit(ctx context.Context, in *adminin.DeptEditInp) (err err
}

// 新增
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).Data(in).Insert(); err != nil {
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).Data(in).OmitEmptyData().Insert(); err != nil {
err = gerror.Wrap(err, "新增部门管理失败,请稍后重试!")
}
return
Expand Down
2 changes: 1 addition & 1 deletion server/internal/logic/admin/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (s *sAdminMember) Edit(ctx context.Context, in *adminin.MemberEditInp) (err
}

return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
id, err := dao.AdminMember.Ctx(ctx).Data(data).InsertAndGetId()
id, err := dao.AdminMember.Ctx(ctx).Data(data).OmitEmptyData().InsertAndGetId()
if err != nil {
err = gerror.Wrap(err, "新增用户失败,请稍后重试!")
return
Expand Down
2 changes: 1 addition & 1 deletion server/internal/logic/admin/member_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *sAdminMemberPost) UpdatePostIds(ctx context.Context, memberId int64, po
}

for i := 0; i < len(postIds); i++ {
_, err = dao.AdminMemberPost.Ctx(ctx).Insert(entity.AdminMemberPost{
_, err = dao.AdminMemberPost.Ctx(ctx).OmitEmptyData().Insert(entity.AdminMemberPost{
MemberId: memberId,
PostId: postIds[i],
})
Expand Down
2 changes: 1 addition & 1 deletion server/internal/logic/admin/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *sAdminMenu) Edit(ctx context.Context, in *adminin.MenuEditInp) (err err
return err
}
} else {
if _, err = dao.AdminMenu.Ctx(ctx).Data(in).Insert(); err != nil {
if _, err = dao.AdminMenu.Ctx(ctx).Data(in).OmitEmptyData().Insert(); err != nil {
err = gerror.Wrap(err, "新增菜单失败!")
return err
}
Expand Down
Loading

0 comments on commit 406e3ef

Please sign in to comment.