Skip to content

Commit

Permalink
优化接口
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Jul 14, 2024
1 parent 60f698b commit d9a261d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 68 deletions.
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
2 changes: 1 addition & 1 deletion user/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func init() {

api.Register("GET", "logout", logout)
api.Register("POST", "password", password)
api.Register("GET", "user/me", userMe)
api.Register("GET", "me", userMe)
api.Register("POST", "user/count", api.Count(&_table))
api.Register("POST", "user/search", api.Search(&_table, nil))
api.Register("POST", "user/create", api.Create(&_table, nil))
Expand Down
68 changes: 2 additions & 66 deletions user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func login(ctx *gin.Context) {
return
}

var users []User
var user User
var users []*User
var user *User

err := _table.Find(bson.D{{"username", obj.Username}}, nil, 0, 1, &users)
if err != nil {
Expand Down Expand Up @@ -91,67 +91,3 @@ func login(ctx *gin.Context) {

api.OK(ctx, user)
}

func logout(ctx *gin.Context) {
session := sessions.Default(ctx)
u := session.Get("user")
if u == nil {
api.Fail(ctx, "未登录")
return
}

//user := u.(int64)
//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user, ModEvent: types.ModEvent{Type: "退出"}})

session.Clear()
_ = session.Save()
api.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 {
api.Error(ctx, err)
return
}

userId := ctx.GetString("user")

var pwd Password
has, err := _passwordTable.Get(userId, &pwd)
if err != nil {
api.Error(ctx, err)
return
}

if !has {
_, err = _passwordTable.Insert(map[string]any{
"_id": userId,
"password": obj.New,
})
if err != nil {
api.Error(ctx, err)
return
}
} else {
if obj.Old != pwd.Password {
api.Fail(ctx, "密码错误")
return
}

//pwd.Password = obj.New //前端已经加密过
err = _passwordTable.Update(userId, bson.M{"password": obj.New})
if err != nil {
api.Error(ctx, err)
return
}
}

api.OK(ctx, nil)
}
23 changes: 23 additions & 0 deletions user/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package user

import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/god-jason/bucket/api"
)

func logout(ctx *gin.Context) {
session := sessions.Default(ctx)
u := session.Get("user")
if u == nil {
api.Fail(ctx, "未登录")
return
}

//user := u.(int64)
//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user, ModEvent: types.ModEvent{Type: "退出"}})

session.Clear()
_ = session.Save()
api.OK(ctx, nil)
}
55 changes: 55 additions & 0 deletions user/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package user

import (
"github.com/gin-gonic/gin"
"github.com/god-jason/bucket/api"
"go.mongodb.org/mongo-driver/bson"
)

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 {
api.Error(ctx, err)
return
}

userId := ctx.GetString("user")

var pwd Password
has, err := _passwordTable.Get(userId, &pwd)
if err != nil {
api.Error(ctx, err)
return
}

if !has {
_, err = _passwordTable.Insert(map[string]any{
"_id": userId,
"password": obj.New,
})
if err != nil {
api.Error(ctx, err)
return
}
} else {
if obj.Old != pwd.Password {
api.Fail(ctx, "密码错误")
return
}

//pwd.Password = obj.New //前端已经加密过
err = _passwordTable.Update(userId, bson.M{"password": obj.New})
if err != nil {
api.Error(ctx, err)
return
}
}

api.OK(ctx, nil)
}

0 comments on commit d9a261d

Please sign in to comment.