From b8b46be4c85c260ca1bee1719b4a8c3635a3ff8b Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 15 Jul 2024 00:23:15 +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 | 14 ++++++++++ admin/login.go | 48 +------------------------------- admin/logout.go | 23 +++++++++++++++ admin/password.go | 37 ++++++++++++++++++++++++ src/app/login/login.component.ts | 10 +++---- src/app/user.service.ts | 2 +- 6 files changed, 80 insertions(+), 54 deletions(-) create mode 100644 admin/logout.go create mode 100644 admin/password.go diff --git a/admin/api.go b/admin/api.go index 5faf385..639c79f 100644 --- a/admin/api.go +++ b/admin/api.go @@ -1,10 +1,24 @@ package admin import ( + "github.com/gin-gonic/gin" "github.com/zgwit/iot-gateway/api" + "github.com/zgwit/iot-gateway/curd" ) 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 == "" { + curd.Fail(ctx, "未登录") + return + } + + curd.OK(ctx, gin.H{"id": id}) +} diff --git a/admin/login.go b/admin/login.go index b2417cf..d143ca3 100644 --- a/admin/login.go +++ b/admin/login.go @@ -42,51 +42,5 @@ func login(ctx *gin.Context) { 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) + curd.OK(ctx, gin.H{"id": "admin"}) } diff --git a/admin/logout.go b/admin/logout.go new file mode 100644 index 0000000..86667d9 --- /dev/null +++ b/admin/logout.go @@ -0,0 +1,23 @@ +package admin + +import ( + "github.com/gin-contrib/sessions" + "github.com/gin-gonic/gin" + "github.com/zgwit/iot-gateway/curd" +) + +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) +} diff --git a/admin/password.go b/admin/password.go new file mode 100644 index 0000000..83b5796 --- /dev/null +++ b/admin/password.go @@ -0,0 +1,37 @@ +package admin + +import ( + "github.com/gin-gonic/gin" + "github.com/god-jason/bucket/config" + "github.com/zgwit/iot-gateway/curd" +) + +type passwordObj struct { + Old string `json:"old"` + New string `json:"new"` +} + +func password(ctx *gin.Context) { + + var obj passwordObj + if err := ctx.ShouldBind(&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/src/app/login/login.component.ts b/src/app/login/login.component.ts index a58ac2b..3efa870 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -9,6 +9,7 @@ import {NzCheckboxComponent} from "ng-zorro-antd/checkbox"; import {NzButtonComponent} from "ng-zorro-antd/button"; import {NzCardComponent} from "ng-zorro-antd/card"; import {SmartRequestService} from "@god-jason/smart"; +import {UserService} from "../user.service"; @Component({ selector: 'app-login', @@ -35,6 +36,7 @@ export class LoginComponent implements OnInit { constructor(private fb: FormBuilder, private rs: SmartRequestService, + private us: UserService, private router: Router, ) { } @@ -56,13 +58,9 @@ export class LoginComponent implements OnInit { //localStorage.setItem('token', res.data.token); //更新用户 - //this.us.setUser(res.data); + this.us.setUser(res.data); - if (res.data.admin) { - this.router.navigateByUrl('/admin') - } else { - this.router.navigateByUrl('/select') - } + this.router.navigateByUrl('/admin') }); } diff --git a/src/app/user.service.ts b/src/app/user.service.ts index e22a766..189c00c 100644 --- a/src/app/user.service.ts +++ b/src/app/user.service.ts @@ -14,7 +14,7 @@ export class UserService { constructor(private rs: SmartRequestService) { //console.log("user me") - rs.get('user/me').subscribe({ + rs.get('me').subscribe({ next: res => { //console.log("user me ok") this.setUser(res.data);