Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve logic in the starbook #135

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/course/starbook/第三章-会话管理/3.2.登录.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ import (
"star/utility"
)

type UserClaims struct {
type userClaims struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个结构体为什么不公开呢?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个结构体users内部用的,想着也没必要公开

Id uint
Username string
jwt.RegisteredClaims
}

func Login(ctx context.Context, username, password string) (tokenString string, err error) {
func (u *Users) Login(ctx context.Context, username, password string) (tokenString string, err error) {
var user entity.Users
err = dao.Users.Ctx(ctx).Where("username", username).Scan(&user)
if err != nil {
Expand All @@ -79,19 +79,19 @@ func Login(ctx context.Context, username, password string) (tokenString string,
}

// 生成token
userClaims := &UserClaims{
uc := &userClaims{
Id: user.Id,
Username: user.Username,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(6 * time.Hour)),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, userClaims)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, uc)
return token.SignedString(utility.JwtKey)
}
```

从上面的代码可以看到,我们需要声明一个结构体`UserClaims`保存签名信息,这里保存了`Id`和`Username`并设置了`Token`有效期为 6 个小时。最后的声明对象还需要调用`SignedString`方法传入`JwtKey`生成签名。
从上面的代码可以看到,我们需要声明一个结构体`userClaims`保存签名信息,这里保存了`Id`和`Username`并设置了`Token`有效期为 6 个小时。最后的声明对象还需要调用`SignedString`方法传入`JwtKey`生成签名。

## Controller调用Logic
---
Expand All @@ -107,7 +107,7 @@ import (
)

func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) {
token, err := users.Login(ctx, req.Username, req.Password)
token, err := c.users.Login(ctx, req.Username, req.Password)
if err != nil {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ import (

...

func Info(ctx context.Context) (user *entity.Users, err error) {
func (u *Users) Info(ctx context.Context) (user *entity.Users, err error) {
user = new(entity.Users)
tokenString := g.RequestFromCtx(ctx).Request.Header.Get("Authorization")

tokenClaims, _ := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
tokenClaims, _ := jwt.ParseWithClaims(tokenString, &userClaims{}, func(token *jwt.Token) (interface{}, error) {
return utility.JwtKey, nil
})

if claims, ok := tokenClaims.Claims.(*UserClaims); ok && tokenClaims.Valid {
if claims, ok := tokenClaims.Claims.(*userClaims); ok && tokenClaims.Valid {
err = dao.Users.Ctx(ctx).Where("id", claims.Id).Scan(&user)
}
return
Expand All @@ -113,6 +113,30 @@ func Info(ctx context.Context) (user *entity.Users, err error) {

## Controller调用Logic
---
同样将`logic`注册到控制器中。

*internal/controller/users/users_new.go*
```go
...

package account

import (
"star/api/account"
usersL "star/internal/logic/users"
)

type ControllerV1 struct {
users *usersL.Users
}

func NewV1() account.IAccountV1 {
return &ControllerV1{
users: &usersL.Users{},
}
}
```

*internal/controller/account/account_v1_info.go*
```go
package account
Expand All @@ -125,7 +149,7 @@ import (
)

func (c *ControllerV1) Info(ctx context.Context, req *v1.InfoReq) (res *v1.InfoRes, err error) {
user, err := users.Info(ctx)
user, err := c.users.Info(ctx)
if err != nil {
return nil, err
}
Expand Down
41 changes: 39 additions & 2 deletions docs/course/starbook/第二章-用户注册/2.3.注册接口.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ done!
---
`Logic` 是业务逻辑层,存放在`internal/logic`下,供`Controller`调用从而实现具体的业务逻辑。

定义一个`Users`对象:

*internal/logic/users/users.go*
```go
package users

type Users struct {
}
```

编写注册方法:

*internal/logic/users/register.go*
```go
package users
Expand All @@ -58,7 +70,7 @@ import (
"star/internal/model/do"
)

func Register(ctx context.Context, username, password, email string) error {
func (u *Users) Register(ctx context.Context, username, password, email string) error {
_, err := dao.Users.Ctx(ctx).Data(do.Users{
Username: username,
Password: password,
Expand All @@ -77,6 +89,31 @@ func Register(ctx context.Context, username, password, email string) error {
---
`Controller` 层负责接收 `Req` 请求对象后调用一个或多个`Logic`完成业务逻辑,一些简单的逻辑也可以直接放在`Controller`中处理。处理完成后的结果封装在约定的 `Res` 数据结构中返回。这里的`Res`数据结构为空,返回`nil`即可。

将`Users`对象封装到控制器中,方便后续调用。

*internal/controller/users/users_new.go*
```go
...

package users

import (
"star/api/users"
usersL "star/internal/logic/users"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在API中,接口包名应该是v1/v2这样的形式,而在logic中,应该是users这样的形式,因此并不需要设置别名。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

users"star/api/users"占用了,那个是gen ctrl生成的

)

type ControllerV1 struct {
users *usersL.Users
}

func NewV1() users.IUsersV1 {
return &ControllerV1{
users: &usersL.Users{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好通过New的创建方法来初始化业务逻辑封装对象,因为外部调用时并不知道一个逻辑对象需要做什么初始化逻辑。直接使用&usersL.Users{}的方式就无法进行初始化逻辑封装了。

}
}
```


*internal/controller/users/users_v1_register.go*
```go
package users
Expand All @@ -90,7 +127,7 @@ import (
)

func (c *ControllerV1) Register(ctx context.Context, req *v1.RegisterReq) (res *v1.RegisterRes, err error) {
err = users.Register(ctx, req.Username, req.Password, req.Email)
err = c.users.Register(ctx, req.Username, req.Password, req.Email)
return nil, err
}
```
Expand Down
14 changes: 7 additions & 7 deletions docs/course/starbook/第二章-用户注册/2.4.业务优化.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ package users

...

func Register(ctx context.Context, username, password, email string) error {
if err := checkUser(ctx, username); err != nil {
func (u *Users) Register(ctx context.Context, username, password, email string) error {
if err := u.checkUser(ctx, username); err != nil {
return err
}

Expand All @@ -169,7 +169,7 @@ func Register(ctx context.Context, username, password, email string) error {
return nil
}

func checkUser(ctx context.Context, username string) error {
func (u *Users) checkUser(ctx context.Context, username string) error {
count, err := dao.Users.Ctx(ctx).Where("username", username).Count()
if err != nil {
return err
Expand Down Expand Up @@ -222,7 +222,7 @@ package users

...

func Register(ctx context.Context, username, password, email string) error {
func (u *Users) Register(ctx context.Context, username, password, email string) error {
...

_, err := dao.Users.Ctx(ctx).Data(do.Users{
Expand Down Expand Up @@ -279,8 +279,8 @@ import (
...
)

func Register(ctx context.Context, in *model.UserInput) error {
if err := CheckUser(ctx, in.Username); err != nil {
func (u *Users) Register(ctx context.Context, in *model.UserInput) error {
if err := u.checkUser(ctx, in.Username); err != nil {
return err
}

Expand Down Expand Up @@ -310,7 +310,7 @@ import (
)

func (c *ControllerV1) Register(ctx context.Context, req *v1.RegisterReq) (res *v1.RegisterRes, err error) {
err = users.Register(ctx, &model.UserInput{
err = c.users.Register(ctx, &model.UserInput{
Username: req.Username,
Password: req.Password,
Email: req.Email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type RandListRes struct {

## 编写Logic
---
*internal/controller/words/words_v1_rand_list.go*
*internal/logic/words/learn_words.go*
```go
package words

Expand All @@ -50,7 +50,7 @@ import (
)

// Rand 随机若干获取单词
func Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) {
func (w *Words) Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) {
if limit <= 0 {
limit = 50
}
Expand All @@ -72,26 +72,24 @@ func Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) {

## Controller调用Logic
---
*internal/logic/words/learn_words.go*
*internal/controller/words/words_v1_rand_list.go*
```go
package words

import (
"context"

"star/api/words/v1"
"star/internal/logic/users"
"star/internal/logic/words"
"star/internal/model"
)

func (c *ControllerV1) RandList(ctx context.Context, req *v1.RandListReq) (res *v1.RandListRes, err error) {
uid, err := users.GetUid(ctx)
uid, err := c.users.GetUid(ctx)
if err != nil {
return nil, err
}

wordList, err := words.Rand(ctx, uid, req.Limit)
wordList, err := c.words.Rand(ctx, uid, req.Limit)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type SetLevelRes struct {

## 编写Logic
---
*internal/controller/words/words_v1_set_level.go*
*internal/logic/words/learn_words.go*
```go
...

// SetLevel 设置单词熟练度
func SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) error {
func (w *Words) SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) error {
if level < 0 || level > 5 {
return gerror.New("熟练度值不合法")
}
Expand All @@ -50,25 +50,23 @@ func SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) e

## Controller调用Logic
---
*internal/logic/words/learn_words.go*
*internal/controller/words/words_v1_set_level.go*
```go
package words

import (
"context"

"star/api/words/v1"
"star/internal/logic/users"
"star/internal/logic/words"
)

func (c *ControllerV1) SetLevel(ctx context.Context, req *v1.SetLevelReq) (res *v1.SetLevelRes, err error) {
uid, err := users.GetUid(ctx)
uid, err := c.users.GetUid(ctx)
if err != nil {
return nil, err
}

err = words.SetLevel(ctx, uid, req.Id, req.Level)
err = c.words.SetLevel(ctx, uid, req.Id, req.Level)
return nil, err
}
```
Expand Down
Loading
Loading