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 14, 2024
1 parent 1273b88 commit b8b46be
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 54 deletions.
14 changes: 14 additions & 0 deletions admin/api.go
Original file line number Diff line number Diff line change
@@ -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})
}
48 changes: 1 addition & 47 deletions admin/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
}
23 changes: 23 additions & 0 deletions admin/logout.go
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions admin/password.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 4 additions & 6 deletions src/app/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -35,6 +36,7 @@ export class LoginComponent implements OnInit {

constructor(private fb: FormBuilder,
private rs: SmartRequestService,
private us: UserService,
private router: Router,
) {
}
Expand All @@ -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')
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b8b46be

Please sign in to comment.