上传gm模块补充

This commit is contained in:
liwei1dao 2022-07-13 14:14:26 +08:00
parent c6c2b5daae
commit 3af54fdb14
14 changed files with 233 additions and 27 deletions

View File

@ -1,9 +1,14 @@
package gin
import (
"fmt"
"net/http"
"reflect"
"sort"
"strings"
"go_dreamfactory/lego/sys/gin/engine"
"go_dreamfactory/lego/utils/crypto/md5"
)
type ISys interface {
@ -71,3 +76,41 @@ func Static(relativePath string, root string) engine.IRoutes {
func StaticFS(relativePath string, fs http.FileSystem) engine.IRoutes {
return defsys.StaticFS(relativePath, fs)
}
//签名接口
func ParamSign(key string, param map[string]interface{}) (sign string) {
var keys []string
for k, _ := range param {
keys = append(keys, k)
}
sort.Strings(keys)
builder := strings.Builder{}
for _, v := range keys {
builder.WriteString(v)
builder.WriteString("=")
switch reflect.TypeOf(param[v]).Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64:
builder.WriteString(fmt.Sprintf("%d", param[v]))
break
default:
builder.WriteString(fmt.Sprintf("%s", param[v]))
break
}
builder.WriteString("&")
}
builder.WriteString("key=" + key)
sign = md5.MD5EncToLower(builder.String())
return
}

View File

@ -0,0 +1,13 @@
package base64
import "encoding/base64"
///编码
func EncodeToString(src []byte) string {
return base64.StdEncoding.EncodeToString(src)
}
///解码
func DecodeString(s string) ([]byte, error) {
return base64.StdEncoding.DecodeString(s)
}

View File

@ -0,0 +1,47 @@
package md5
import (
"crypto/md5"
"encoding/hex"
"fmt"
"sort"
"strings"
)
/*
MD5加密
*/
//MD5加密 大写
func MD5EncToUpper(str string) string {
h := md5.New()
h.Write([]byte(str))
return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
}
//MD5加密 小写
func MD5EncToLower(str string) string {
h := md5.New()
h.Write([]byte(str))
return strings.ToLower(hex.EncodeToString(h.Sum(nil)))
}
//参数签名
func ParamSign(param map[string]interface{}, key string) (orsign, sign string) {
a := sort.StringSlice{}
for k, _ := range param {
a = append(a, k)
}
sort.Sort(a)
orsign = ""
for _, k := range a {
switch param[k].(type) { //只签名基础数据
case bool, byte, int8, int16, uint16, int32, uint32, int, int64, uint64, float64, float32, string:
orsign = orsign + fmt.Sprintf("%s=%v&", k, param[k])
break
}
}
orsign = orsign + fmt.Sprintf("key=%s", key)
sign = MD5EncToLower(orsign)
return
}

View File

@ -1,4 +1,4 @@
package web
package gm
import (
"go_dreamfactory/lego/core"
@ -11,16 +11,16 @@ import (
*/
type Api_Comp struct {
cbase.ModuleCompBase
options *Options //模块参数
moduleWeb *Web //当前模块对象
gin gin.ISys //gin 框架 web的热门框架
options *Options //模块参数
module *GM //当前模块对象
gin gin.ISys //gin 框架 web的热门框架
}
//组件初始化接口 启动web服务 并注册api
func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.ModuleCompBase.Init(service, module, comp, options)
this.options = options.(*Options)
this.moduleWeb = module.(*Web)
this.module = module.(*GM)
this.gin, err = gin.NewSys(gin.SetListenPort(this.options.Port))
this.gin.POST("/register", this.Register)
this.gin.GET("/serverlist", this.ServerList)

View File

@ -0,0 +1,46 @@
package gm
import (
"go_dreamfactory/lego/sys/gin"
"go_dreamfactory/lego/sys/gin/engine"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"net/http"
)
type CreateNotifyReq struct {
pb.DBSystemNotify
Sign string `json:"sign"`
}
//服务列表
func (this *Api_Comp) CreateNotify(c *engine.Context) {
req := &CreateNotifyReq{}
c.BindJSON(&req)
defer log.Debugf("CreateNotify:%+v", req)
var (
code pb.ErrorCode
msg string
data interface{}
err error
)
c.JSON(http.StatusOK, &Respond{Code: code, Message: msg, Data: data})
if sign := gin.ParamSign(this.options.Key, map[string]interface{}{"Title": req.Title, "Ctime": req.Ctime, "Rtime": req.Rtime}); sign != req.Sign {
log.Errorf("LoginByCaptchaReq SignError sgin:%s", sign)
code = pb.ErrorCode_SignError
msg = pb.GetErrorCodeMsg(code)
return
}
if len(req.Title) == 0 {
code = pb.ErrorCode_ReqParameterError
msg = pb.GetErrorCodeMsg(code)
return
}
if err = this.module.modelNotify.CreateSystemNotify(&req.DBSystemNotify); err != nil {
log.Errorf("LoginByCaptchaReq CreateSystemNotify err:%v", err)
code = pb.ErrorCode_DBError
msg = pb.GetErrorCodeMsg(code)
return
}
msg = pb.GetErrorCodeMsg(code)
}

View File

@ -1,4 +1,4 @@
package web
package gm
import (
"go_dreamfactory/lego/sys/gin/engine"
@ -13,7 +13,7 @@ func (this *Api_Comp) Register(c *engine.Context) {
rsp := &pb.UserRegisterResp{}
err := c.BindJSON(&req)
if err == nil {
err := this.moduleWeb.modelUser.User_Create(&pb.DBUser{
err := this.module.modelUser.User_Create(&pb.DBUser{
Binduid: req.Account,
Sid: req.Sid,
})

View File

@ -1,4 +1,4 @@
package web
package gm
import (
"go_dreamfactory/lego/sys/gin/engine"
@ -7,7 +7,7 @@ import (
//服务列表
func (this *Api_Comp) ServerList(c *engine.Context) {
conf := this.moduleWeb.configure.getServerListConf()
conf := this.module.configure.getServerListConf()
c.JSON(http.StatusOK, conf)
}

View File

@ -1,4 +1,4 @@
package web
package gm
import (
"fmt"

9
modules/gm/core.go Normal file
View File

@ -0,0 +1,9 @@
package gm
import "go_dreamfactory/pb"
type Respond struct {
Code pb.ErrorCode `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

42
modules/gm/modelNotify.go Normal file
View File

@ -0,0 +1,42 @@
package gm
import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
///论坛 数据组件
type modelNotifyComp struct {
modules.MCompModel
module *GM
}
//组件初始化接口
func (this *modelNotifyComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*GM)
this.TableName = "notify"
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
//创建系统公告
func (this *modelNotifyComp) CreateSystemNotify(notify *pb.DBSystemNotify) (err error) {
if _, err = this.DB.InsertOne(core.SqlTable(this.TableName), notify); err != nil {
log.Errorf("CreateSystemNotify err:%v", err)
return
}
if err = this.Redis.HSet(this.TableName, notify.Id, notify); err != nil {
log.Errorf("CreateSystemNotify err:%v", err)
return
}
return
}

View File

@ -1,4 +1,4 @@
package web
package gm
import (
"go_dreamfactory/comm"
@ -9,40 +9,41 @@ import (
)
/*
模块名:web
描述:为服务集群提供一些http服务接口 方便测试和后期运维实现
模块名:gm
描述:提供管理员相关的http接口
开发:李伟
*/
func NewModule() core.IModule {
m := new(Web)
m := new(GM)
return m
}
type Web struct {
type GM struct {
cbase.ModuleBase
options *Options
api_comp *Api_Comp //提供weba pi服务的组件
modelUser *user.ModelUser
configure *configureComp
options *Options
api_comp *Api_Comp //提供weba pi服务的组件
modelUser *user.ModelUser
modelNotify *modelNotifyComp
configure *configureComp
}
//模块名
func (this *Web) GetType() core.M_Modules {
func (this *GM) GetType() core.M_Modules {
return comm.ModuleWeb
}
//模块自定义参数
func (this *Web) NewOptions() (options core.IModuleOptions) {
func (this *GM) NewOptions() (options core.IModuleOptions) {
return new(Options)
}
func (this *Web) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
func (this *GM) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.options = options.(*Options)
return
}
func (this *Web) OnInstallComp() {
func (this *GM) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
this.modelUser = this.RegisterComp(new(user.ModelUser)).(*user.ModelUser)

View File

@ -1,4 +1,4 @@
package web
package gm
import (
"go_dreamfactory/lego/utils/mapstructure"
@ -7,6 +7,7 @@ import (
type (
Options struct {
Port int
Key string
}
)

View File

@ -6,6 +6,7 @@ import (
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"time"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson"
@ -56,10 +57,13 @@ func (this *modelNotifyComp) GetFullNotify() (result []*pb.DBSystemNotify, err e
}
if len(notifys) > 0 {
n := 0
now := time.Now().Unix()
result = make([]*pb.DBSystemNotify, len(notifys))
for _, v := range notifys {
result[n] = v
n++
if now >= v.Rtime {
result[n] = v
n++
}
}
}
return

View File

@ -99,7 +99,7 @@ func (this *SCompGateRoute) ReceiveMsg(ctx context.Context, args *pb.AgentMessag
l := runtime.Stack(buf, false)
reply.Code = pb.ErrorCode_Exception
reply.ErrorMessage = fmt.Sprintf("%v: %s", r, buf[:l])
log.Errorf("HandleUserMsg:[%s-%s] err:%s", args.MainType, args.SubType, reply.ErrorMessage)
log.Errorf("[Handle Api]:[%s-%s] err:%s", args.MainType, args.SubType, reply.ErrorMessage)
}
}()
method := fmt.Sprintf("%s.%s", args.MainType, args.SubType)