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

Commit b8b46be

Browse files
committed
优化
1 parent 1273b88 commit b8b46be

File tree

6 files changed

+80
-54
lines changed

6 files changed

+80
-54
lines changed

admin/api.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
package admin
22

33
import (
4+
"github.com/gin-gonic/gin"
45
"github.com/zgwit/iot-gateway/api"
6+
"github.com/zgwit/iot-gateway/curd"
57
)
68

79
func init() {
10+
api.Register("GET", "me", me)
811
api.Register("GET", "logout", logout)
912
api.Register("POST", "password", password)
1013
}
14+
15+
func me(ctx *gin.Context) {
16+
id := ctx.GetString("user")
17+
18+
if id == "" {
19+
curd.Fail(ctx, "未登录")
20+
return
21+
}
22+
23+
curd.OK(ctx, gin.H{"id": id})
24+
}

admin/login.go

+1-47
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,5 @@ func login(ctx *gin.Context) {
4242
session.Set("user", "admin")
4343
_ = session.Save()
4444

45-
curd.OK(ctx, nil)
46-
}
47-
48-
func logout(ctx *gin.Context) {
49-
session := sessions.Default(ctx)
50-
u := session.Get("user")
51-
if u == nil {
52-
curd.Fail(ctx, "未登录")
53-
return
54-
}
55-
56-
//user := u.(int64)
57-
//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user, ModEvent: types.ModEvent{Type: "退出"}})
58-
59-
session.Clear()
60-
_ = session.Save()
61-
curd.OK(ctx, nil)
62-
}
63-
64-
type passwordObj struct {
65-
Old string `json:"old"`
66-
New string `json:"new"`
67-
}
68-
69-
func password(ctx *gin.Context) {
70-
71-
var obj passwordObj
72-
if err := ctx.ShouldBindJSON(&obj); err != nil {
73-
curd.Error(ctx, err)
74-
return
75-
}
76-
77-
if obj.Old != config.GetString(MODULE, "password") {
78-
curd.Fail(ctx, "密码错误")
79-
return
80-
}
81-
82-
//更新密码
83-
config.Set(MODULE, "password", obj.New)
84-
85-
err := config.Store()
86-
if err != nil {
87-
curd.Error(ctx, err)
88-
return
89-
}
90-
91-
curd.OK(ctx, nil)
45+
curd.OK(ctx, gin.H{"id": "admin"})
9246
}

admin/logout.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package admin
2+
3+
import (
4+
"github.com/gin-contrib/sessions"
5+
"github.com/gin-gonic/gin"
6+
"github.com/zgwit/iot-gateway/curd"
7+
)
8+
9+
func logout(ctx *gin.Context) {
10+
session := sessions.Default(ctx)
11+
u := session.Get("user")
12+
if u == nil {
13+
curd.Fail(ctx, "未登录")
14+
return
15+
}
16+
17+
//user := u.(int64)
18+
//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user, ModEvent: types.ModEvent{Type: "退出"}})
19+
20+
session.Clear()
21+
_ = session.Save()
22+
curd.OK(ctx, nil)
23+
}

admin/password.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package admin
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/god-jason/bucket/config"
6+
"github.com/zgwit/iot-gateway/curd"
7+
)
8+
9+
type passwordObj struct {
10+
Old string `json:"old"`
11+
New string `json:"new"`
12+
}
13+
14+
func password(ctx *gin.Context) {
15+
16+
var obj passwordObj
17+
if err := ctx.ShouldBind(&obj); err != nil {
18+
curd.Error(ctx, err)
19+
return
20+
}
21+
22+
if obj.Old != config.GetString(MODULE, "password") {
23+
curd.Fail(ctx, "密码错误")
24+
return
25+
}
26+
27+
//更新密码
28+
config.Set(MODULE, "password", obj.New)
29+
30+
err := config.Store()
31+
if err != nil {
32+
curd.Error(ctx, err)
33+
return
34+
}
35+
36+
curd.OK(ctx, nil)
37+
}

src/app/login/login.component.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {NzCheckboxComponent} from "ng-zorro-antd/checkbox";
99
import {NzButtonComponent} from "ng-zorro-antd/button";
1010
import {NzCardComponent} from "ng-zorro-antd/card";
1111
import {SmartRequestService} from "@god-jason/smart";
12+
import {UserService} from "../user.service";
1213

1314
@Component({
1415
selector: 'app-login',
@@ -35,6 +36,7 @@ export class LoginComponent implements OnInit {
3536

3637
constructor(private fb: FormBuilder,
3738
private rs: SmartRequestService,
39+
private us: UserService,
3840
private router: Router,
3941
) {
4042
}
@@ -56,13 +58,9 @@ export class LoginComponent implements OnInit {
5658
//localStorage.setItem('token', res.data.token);
5759

5860
//更新用户
59-
//this.us.setUser(res.data);
61+
this.us.setUser(res.data);
6062

61-
if (res.data.admin) {
62-
this.router.navigateByUrl('/admin')
63-
} else {
64-
this.router.navigateByUrl('/select')
65-
}
63+
this.router.navigateByUrl('/admin')
6664
});
6765
}
6866

src/app/user.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class UserService {
1414

1515
constructor(private rs: SmartRequestService) {
1616
//console.log("user me")
17-
rs.get('user/me').subscribe({
17+
rs.get('me').subscribe({
1818
next: res => {
1919
//console.log("user me ok")
2020
this.setUser(res.data);

0 commit comments

Comments
 (0)