Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Jul 8, 2024
1 parent 2b6db76 commit 6204bff
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 84 deletions.
10 changes: 10 additions & 0 deletions admin/api.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions admin/boot.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions admin/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package admin

import (
"github.com/god-jason/bucket/config"
)

const MODULE = "admin"

func init() {
config.Register(MODULE, "password", md5hash("123456"))
}
92 changes: 92 additions & 0 deletions admin/login.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 0 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ func RegisterRoutes(router *gin.RouterGroup) {
//错误恢复,并返回至前端
router.Use(catchError)

router.GET("/oem", oem)
router.GET("/info", info)

//检查 session,必须登录
router.Use(mustLogin)

Expand All @@ -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 报接口错误(以下代码不生效,路由好像不是树形处理)
Expand Down
18 changes: 0 additions & 18 deletions api/info.go

This file was deleted.

50 changes: 0 additions & 50 deletions api/oem.go

This file was deleted.

5 changes: 2 additions & 3 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion db/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6204bff

Please sign in to comment.