From 6204bff7e72d2a490b20017d7fa86091411a72f7 Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 8 Jul 2024 23:30:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/api.go | 10 ++++++ admin/boot.go | 21 +++++++++++ admin/config.go | 11 ++++++ admin/login.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ api/api.go | 6 ---- api/info.go | 18 ---------- api/oem.go | 50 --------------------------- db/config.go | 5 ++- db/setting.go | 2 +- go.mod | 5 +-- go.sum | 10 +++--- main.go | 1 + 12 files changed, 147 insertions(+), 84 deletions(-) create mode 100644 admin/api.go create mode 100644 admin/boot.go create mode 100644 admin/config.go create mode 100644 admin/login.go delete mode 100644 api/info.go delete mode 100644 api/oem.go diff --git a/admin/api.go b/admin/api.go new file mode 100644 index 0000000..5faf385 --- /dev/null +++ b/admin/api.go @@ -0,0 +1,10 @@ +package admin + +import ( + "github.com/zgwit/iot-gateway/api" +) + +func init() { + api.Register("GET", "logout", logout) + api.Register("POST", "password", password) +} diff --git a/admin/boot.go b/admin/boot.go new file mode 100644 index 0000000..5a2161b --- /dev/null +++ b/admin/boot.go @@ -0,0 +1,21 @@ +package admin + +import ( + "github.com/god-jason/bucket/boot" + "github.com/god-jason/bucket/web" +) + +func init() { + boot.Register("admin", &boot.Task{ + Startup: Startup, + Shutdown: nil, + Depends: []string{"config", "web"}, + }) +} + +func Startup() error { + + web.Engine.POST("api/login", login) + + return nil +} diff --git a/admin/config.go b/admin/config.go new file mode 100644 index 0000000..0e172e9 --- /dev/null +++ b/admin/config.go @@ -0,0 +1,11 @@ +package admin + +import ( + "github.com/god-jason/bucket/config" +) + +const MODULE = "admin" + +func init() { + config.Register(MODULE, "password", md5hash("123456")) +} diff --git a/admin/login.go b/admin/login.go new file mode 100644 index 0000000..b2417cf --- /dev/null +++ b/admin/login.go @@ -0,0 +1,92 @@ +package admin + +import ( + "crypto/md5" + "encoding/hex" + "github.com/gin-contrib/sessions" + "github.com/gin-gonic/gin" + "github.com/god-jason/bucket/config" + "github.com/zgwit/iot-gateway/curd" +) + +type loginObj struct { + Password string `json:"password"` + Remember bool `json:"remember"` +} + +func md5hash(text string) string { + h := md5.New() + h.Write([]byte(text)) + sum := h.Sum(nil) + return hex.EncodeToString(sum) +} + +func login(ctx *gin.Context) { + session := sessions.Default(ctx) + + var obj loginObj + if err := ctx.ShouldBind(&obj); err != nil { + curd.Error(ctx, err) + return + } + + password := config.GetString(MODULE, "password") + if password != obj.Password { + curd.Fail(ctx, "密码错误") + return + } + + //_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user.id, ModEvent: types.ModEvent{Type: "登录"}}) + + //存入session + session.Set("user", "admin") + _ = session.Save() + + curd.OK(ctx, nil) +} + +func logout(ctx *gin.Context) { + session := sessions.Default(ctx) + u := session.Get("user") + if u == nil { + curd.Fail(ctx, "未登录") + return + } + + //user := u.(int64) + //_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user, ModEvent: types.ModEvent{Type: "退出"}}) + + session.Clear() + _ = session.Save() + curd.OK(ctx, nil) +} + +type passwordObj struct { + Old string `json:"old"` + New string `json:"new"` +} + +func password(ctx *gin.Context) { + + var obj passwordObj + if err := ctx.ShouldBindJSON(&obj); err != nil { + curd.Error(ctx, err) + return + } + + if obj.Old != config.GetString(MODULE, "password") { + curd.Fail(ctx, "密码错误") + return + } + + //更新密码 + config.Set(MODULE, "password", obj.New) + + err := config.Store() + if err != nil { + curd.Error(ctx, err) + return + } + + curd.OK(ctx, nil) +} diff --git a/api/api.go b/api/api.go index 0cfa961..16d6697 100644 --- a/api/api.go +++ b/api/api.go @@ -79,9 +79,6 @@ func RegisterRoutes(router *gin.RouterGroup) { //错误恢复,并返回至前端 router.Use(catchError) - router.GET("/oem", oem) - router.GET("/info", info) - //检查 session,必须登录 router.Use(mustLogin) @@ -90,9 +87,6 @@ func RegisterRoutes(router *gin.RouterGroup) { router.Handle(a.Method, a.Path, a.Handlers...) } - //OEM - oemRouter(router.Group("/oem")) - backupRouter(router.Group("/backup")) //TODO 报接口错误(以下代码不生效,路由好像不是树形处理) diff --git a/api/info.go b/api/info.go deleted file mode 100644 index 119ed0c..0000000 --- a/api/info.go +++ /dev/null @@ -1,18 +0,0 @@ -package api - -import ( - "github.com/gin-gonic/gin" - "github.com/god-jason/bucket/pkg/build" - "github.com/zgwit/iot-gateway/curd" - "runtime" -) - -func info(ctx *gin.Context) { - curd.OK(ctx, gin.H{ - "version": build.Version, - "build": build.Build, - "git": build.GitHash, - "gin": gin.Version, - "runtime": runtime.Version(), - }) -} diff --git a/api/oem.go b/api/oem.go deleted file mode 100644 index bb4207d..0000000 --- a/api/oem.go +++ /dev/null @@ -1,50 +0,0 @@ -package api - -import ( - "github.com/gin-gonic/gin" - "github.com/god-jason/bucket/config" - "github.com/zgwit/iot-gateway/curd" -) - -type OEM struct { - Name string `json:"name"` - Logo string `json:"logo"` -} - -// @Summary 获取oem信息 -// @Schemes -// @Description 获取oem信息 -// @Tags oem -// @Accept json -// @Produce json -// @Success 200 {object} curd.ReplyData[OEM] 返回信息 -// @Router /oem [get] -func oem(ctx *gin.Context) { - curd.OK(ctx, OEM{ - Name: config.GetString("oem", "name"), - Logo: config.GetString("oem", "logo"), - }) -} - -// @Summary 修改oem信息 -// @Schemes -// @Description 修改oem信息 -// @Tags oem -// @Param search body OEM true "信息" -// @Accept json -// @Produce json -// @Success 200 {object} curd.ReplyData[int] 返回nil -// @Router /oem [post] -func oemUpdate(ctx *gin.Context) { - var oem OEM - err := ctx.ShouldBindJSON(&oem) - if err != nil { - curd.Error(ctx, err) - return - } - curd.OK(ctx, nil) -} - -func oemRouter(app *gin.RouterGroup) { - app.POST("", oemUpdate) -} diff --git a/db/config.go b/db/config.go index be80aaf..b8fbc32 100644 --- a/db/config.go +++ b/db/config.go @@ -2,14 +2,13 @@ package db import ( "github.com/god-jason/bucket/config" - "github.com/god-jason/bucket/lib" ) const MODULE = "database" func init() { - config.Register(MODULE, "type", "sqlite") - config.Register(MODULE, "url", lib.AppName()+".db") + config.Register(MODULE, "type", "mysql") + config.Register(MODULE, "url", ".db") config.Register(MODULE, "debug", false) config.Register(MODULE, "sync", true) } diff --git a/db/setting.go b/db/setting.go index 700e493..63ec4a2 100644 --- a/db/setting.go +++ b/db/setting.go @@ -12,7 +12,7 @@ func init() { Title: "数据库配置", Form: []types.SmartField{ { - Key: "Type", Label: "数据库类型", Type: "select", Default: "sqlite", + Key: "Type", Label: "数据库类型", Type: "select", Default: "mysql", Options: []types.SmartSelectOption{ {Label: "SQLite(内置)", Value: "sqlite"}, {Label: "MySQL", Value: "mysql"}, diff --git a/go.mod b/go.mod index 2783bda..d1c44f7 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( github.com/eclipse/paho.mqtt.golang v1.4.3 github.com/gin-contrib/sessions v1.0.1 github.com/gin-gonic/gin v1.10.0 - github.com/go-sql-driver/mysql v1.7.1 - github.com/god-jason/bucket v0.0.1 + github.com/go-sql-driver/mysql v1.8.1 + github.com/god-jason/bucket v0.0.3 github.com/rs/xid v1.5.0 github.com/segmentio/ksuid v1.0.4 github.com/spf13/viper v1.19.0 @@ -17,6 +17,7 @@ require ( ) require ( + filippo.io/edwards25519 v1.1.0 // indirect github.com/bytedance/sonic v1.11.9 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect diff --git a/go.sum b/go.sum index 1a33ff5..5d6a51f 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU= github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg= @@ -43,12 +45,12 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao= github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= -github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= -github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= -github.com/god-jason/bucket v0.0.1 h1:GDqIpiXgG+c8Ddaq1lbsb29D+bPWWulgpibiEycHOS4= -github.com/god-jason/bucket v0.0.1/go.mod h1:l1eXs30hxn2UAPjz87zQxw0w9oO3I+TTfCiA1dkuv+w= +github.com/god-jason/bucket v0.0.3 h1:PfJAK5MNZC+nb3xiOr8EO2Ht+4LZtiJspMVAlk+NQvM= +github.com/god-jason/bucket v0.0.3/go.mod h1:l1eXs30hxn2UAPjz87zQxw0w9oO3I+TTfCiA1dkuv+w= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/main.go b/main.go index 6c58720..5bff325 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/god-jason/bucket/boot" "github.com/god-jason/bucket/pkg/service" "github.com/god-jason/bucket/web" + _ "github.com/zgwit/iot-gateway/admin" "github.com/zgwit/iot-gateway/api" "github.com/zgwit/iot-gateway/args" _ "github.com/zgwit/iot-gateway/client"