Skip to content

Commit

Permalink
enhance services
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailJixiangyuyu committed Mar 9, 2024
1 parent 65a2686 commit 7322940
Show file tree
Hide file tree
Showing 26 changed files with 452 additions and 154 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ log
zip

__debug*
applications/api/docs/swagger.yaml
15 changes: 14 additions & 1 deletion applications/api/biz/handler/api/user_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions applications/api/biz/router/api/middleware.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 70 additions & 3 deletions applications/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions applications/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,40 @@
}
}
},
"/douyin/user/login": {
"post": {
"description": "添加一个用户到数据库中",
"tags": [
"User用户相关"
],
"summary": "用户登录",
"parameters": [
{
"description": "用户信息",
"name": "req",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.DouyinUserLoginRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/user.DouyinUserResponse"
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/api.DouyinUserLoginResponse"
}
}
}
}
},
"/douyin/user/register": {
"post": {
"description": "添加一个用户到数据库中",
Expand Down Expand Up @@ -915,6 +949,40 @@
}
}
},
"api.DouyinUserLoginRequest": {
"type": "object",
"properties": {
"password": {
"description": "密码,最长32个字符",
"type": "string"
},
"username": {
"description": "登陆用户名,最长32个字符",
"type": "string"
}
}
},
"api.DouyinUserLoginResponse": {
"type": "object",
"properties": {
"status_code": {
"description": "状态码,0-成功,其他值-失败",
"type": "integer"
},
"status_msg": {
"description": "返回状态描述",
"type": "string"
},
"token": {
"description": "用户鉴权token",
"type": "string"
},
"user_id": {
"description": "用户id",
"type": "integer"
}
}
},
"api.DouyinUserRegisterRequest": {
"type": "object",
"properties": {
Expand Down
24 changes: 18 additions & 6 deletions applications/api/initialize/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initialize

import (
"context"
"github.com/cloudwego/hertz/pkg/protocol"
"strings"
"time"

Expand All @@ -18,6 +19,13 @@ import (

var AuthMiddleware *jwt.HertzJWTMiddleware

type LoginResp struct {
StatusCode int `json:"status_code"`
StatusMsg string `json:"status_msg"`
UserId int64 `json:"user_id,string"`
Token string `json:"token"`
}

func InitJwt() {
AuthMiddleware, _ = jwt.New(&jwt.HertzJWTMiddleware{
Key: []byte(ViperConfig.Viper.GetString("JWT.signingKey")),
Expand All @@ -41,12 +49,16 @@ func InitJwt() {
},
LoginResponse: func(ctx context.Context, c *app.RequestContext, code int, token string, expire time.Time) {
claims := jwt.ExtractClaims(ctx, c)
// Long 型数据在返回前端时会失真,需使用string类型
//userId := strconv.FormatInt(claims[constants.IdentityKey].(int64), 10)
c.SetCookie("token", token, 24*60*60, "/", "", protocol.CookieSameSiteDefaultMode, false, true)

userId := claims[constants.IdentityKey].(int64)
c.JSON(consts.StatusOK, map[string]interface{}{
"status_code": errno.SuccessCode,
"status_msg": errno.Success.ErrMsg,
"user_id": userId,
"token": token,
c.JSON(consts.StatusOK, LoginResp{
StatusCode: errno.SuccessCode,
StatusMsg: errno.Success.ErrMsg,
UserId: userId,
Token: token,
})
},
WithNext: func(ctx context.Context, c *app.RequestContext) bool {
Expand Down Expand Up @@ -103,7 +115,7 @@ func InitJwt() {
}
return userId, err
},
TokenLookup: "query: token, form: token, param: token",
TokenLookup: "cookie: token",
TimeFunc: time.Now,
})
}
1 change: 1 addition & 0 deletions applications/api/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion applications/feed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (

func init() {

_, err := configurator.Load(config, "DOUTOK_FEED_", "feed")
_, err := configurator.Load(config, "DOUTOK_FEED", "feed")
if err != nil {
logger.Fatal("could not load env variables", zap.Error(err), zap.Any("config", config))
}
Expand Down
2 changes: 1 addition & 1 deletion applications/publish/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (

func init() {

_, err := configurator.Load(config, "DOUTOK_PUBLISH_", "publish")
_, err := configurator.Load(config, "DOUTOK_PUBLISH", "publish")
if err != nil {
logger.Fatal("could not load env variables", zap.Error(err), zap.Any("config", config))
}
Expand Down
Loading

0 comments on commit 7322940

Please sign in to comment.