-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
222 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package admin | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/god-jason/bucket/api" | ||
) | ||
|
||
func init() { | ||
api.Register("GET", "me", me) | ||
api.Register("GET", "logout", logout) | ||
api.Register("POST", "password", password) | ||
} | ||
|
||
func me(ctx *gin.Context) { | ||
id := ctx.GetString("user") | ||
|
||
if id == "" { | ||
Fail(ctx, "未登录") | ||
return | ||
} | ||
|
||
OK(ctx, gin.H{"id": id}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package admin | ||
|
||
import "github.com/god-jason/bucket/config" | ||
|
||
const MODULE = "admin" | ||
|
||
func init() { | ||
config.Register(MODULE, "password", md5hash("123456")) | ||
config.Register(MODULE, "lock", false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package admin | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
"github.com/god-jason/bucket/config" | ||
) | ||
|
||
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 { | ||
Error(ctx, err) | ||
return | ||
} | ||
|
||
password := config.GetString(MODULE, "password") | ||
if password != obj.Password { | ||
Fail(ctx, "密码错误") | ||
return | ||
} | ||
|
||
//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user.id, ModEvent: types.ModEvent{Type: "登录"}}) | ||
|
||
//存入session | ||
session.Set("user", "admin") | ||
_ = session.Save() | ||
|
||
OK(ctx, gin.H{"id": "admin"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package admin | ||
|
||
import ( | ||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func logout(ctx *gin.Context) { | ||
session := sessions.Default(ctx) | ||
u := session.Get("user") | ||
if u == nil { | ||
Fail(ctx, "未登录") | ||
return | ||
} | ||
|
||
//user := u.(int64) | ||
//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user, ModEvent: types.ModEvent{Type: "退出"}}) | ||
|
||
session.Clear() | ||
_ = session.Save() | ||
OK(ctx, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package admin | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/god-jason/bucket/config" | ||
) | ||
|
||
type passwordObj struct { | ||
Old string `json:"old"` | ||
New string `json:"new"` | ||
} | ||
|
||
func password(ctx *gin.Context) { | ||
if config.GetBool(MODULE, "lock") { | ||
Fail(ctx, "禁止修改密码") | ||
return | ||
} | ||
|
||
var obj passwordObj | ||
if err := ctx.ShouldBind(&obj); err != nil { | ||
Error(ctx, err) | ||
return | ||
} | ||
|
||
if obj.Old != config.GetString(MODULE, "password") { | ||
Fail(ctx, "密码错误") | ||
return | ||
} | ||
|
||
//更新密码 | ||
config.Set(MODULE, "password", obj.New) | ||
|
||
err := config.Store() | ||
if err != nil { | ||
Error(ctx, err) | ||
return | ||
} | ||
|
||
OK(ctx, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package admin | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
type ReplyData[T any] struct { | ||
Data T `json:"data"` | ||
Error string `json:"error,omitempty"` | ||
} | ||
|
||
type ReplyList[T any] struct { | ||
Data []T `json:"data"` | ||
Total int64 `json:"total"` | ||
Error string `json:"error,omitempty"` | ||
} | ||
|
||
func List(ctx *gin.Context, data any, total int64) { | ||
ctx.JSON(http.StatusOK, gin.H{"data": data, "total": total}) | ||
} | ||
|
||
func OK(ctx *gin.Context, data any) { | ||
ctx.JSON(http.StatusOK, gin.H{"data": data}) | ||
} | ||
|
||
func Fail(ctx *gin.Context, err string) { | ||
ctx.JSON(http.StatusOK, gin.H{"error": err}) | ||
} | ||
|
||
func Error(ctx *gin.Context, err error) { | ||
ctx.JSON(http.StatusOK, gin.H{"error": err.Error()}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters