Skip to content

Commit

Permalink
feat:user login
Browse files Browse the repository at this point in the history
  • Loading branch information
CocaineCong committed May 19, 2023
1 parent 848235d commit a18e00f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 61 deletions.
54 changes: 29 additions & 25 deletions app/gateway/internal/handler/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,73 @@ import (
"github.com/CocaineCong/grpc-todolist/idl/task/pb"
"github.com/CocaineCong/grpc-todolist/pkg/e"
"github.com/CocaineCong/grpc-todolist/pkg/res"
"github.com/CocaineCong/grpc-todolist/pkg/util/jwt"
"github.com/CocaineCong/grpc-todolist/pkg/util/ctl"
)

func GetTaskList(ginCtx *gin.Context) {
func GetTaskList(ctx *gin.Context) {
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := jwt.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
PanicIfTaskError(ctx.Bind(&tReq))
user, err := ctl.GetUserInfo(ctx)
PanicIfTaskError(err)
tReq.UserID = user.Id
TaskService := ctx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskShow(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Data: TaskResp,
Status: uint(TaskResp.Code),
Msg: e.GetMsg(uint(TaskResp.Code)),
}
ginCtx.JSON(http.StatusOK, r)
ctx.JSON(http.StatusOK, r)
}

func CreateTask(ginCtx *gin.Context) {
func CreateTask(ctx *gin.Context) {
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := jwt.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
PanicIfTaskError(ctx.Bind(&tReq))
user, err := ctl.GetUserInfo(ctx)
PanicIfTaskError(err)
tReq.UserID = user.Id
TaskService := ctx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskCreate(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Data: TaskResp,
Status: uint(TaskResp.Code),
Msg: e.GetMsg(uint(TaskResp.Code)),
}
ginCtx.JSON(http.StatusOK, r)
ctx.JSON(http.StatusOK, r)
}

func UpdateTask(ginCtx *gin.Context) {
func UpdateTask(ctx *gin.Context) {
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := jwt.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
PanicIfTaskError(ctx.Bind(&tReq))
user, err := ctl.GetUserInfo(ctx)
PanicIfTaskError(err)
tReq.UserID = user.Id
TaskService := ctx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskUpdate(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Data: TaskResp,
Status: uint(TaskResp.Code),
Msg: e.GetMsg(uint(TaskResp.Code)),
}
ginCtx.JSON(http.StatusOK, r)
ctx.JSON(http.StatusOK, r)
}

func DeleteTask(ginCtx *gin.Context) {
func DeleteTask(ctx *gin.Context) {
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := jwt.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
PanicIfTaskError(ctx.Bind(&tReq))
user, err := ctl.GetUserInfo(ctx)
PanicIfTaskError(err)
tReq.UserID = user.Id
TaskService := ctx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskDelete(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Data: TaskResp,
Status: uint(TaskResp.Code),
Msg: e.GetMsg(uint(TaskResp.Code)),
}
ginCtx.JSON(http.StatusOK, r)
ctx.JSON(http.StatusOK, r)
}
22 changes: 11 additions & 11 deletions app/gateway/internal/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ import (
"github.com/CocaineCong/grpc-todolist/pkg/util/jwt"
)

// 用户注册
func UserRegister(ginCtx *gin.Context) {
// UserRegister 用户注册
func UserRegister(ctx *gin.Context) {
var userReq userPb.UserRequest
PanicIfUserError(ginCtx.Bind(&userReq))
PanicIfUserError(ctx.Bind(&userReq))
// 从gin.Key中取出服务实例
userService := ginCtx.Keys["user"].(userPb.UserServiceClient)
userService := ctx.Keys["user"].(userPb.UserServiceClient)
userResp, err := userService.UserRegister(context.Background(), &userReq)
PanicIfUserError(err)
r := res.Response{
Data: userResp,
Status: uint(userResp.Code),
Msg: e.GetMsg(uint(userResp.Code)),
}
ginCtx.JSON(http.StatusOK, r)
ctx.JSON(http.StatusOK, r)
}

// 用户登录
func UserLogin(ginCtx *gin.Context) {
// UserLogin 用户登录
func UserLogin(ctx *gin.Context) {
var userReq userPb.UserRequest
PanicIfUserError(ginCtx.Bind(&userReq))
PanicIfUserError(ctx.Bind(&userReq))
// 从gin.Key中取出服务实例
userService := ginCtx.Keys["user"].(userPb.UserServiceClient)
userService := ctx.Keys["user"].(userPb.UserServiceClient)
userResp, err := userService.UserLogin(context.Background(), &userReq)
PanicIfUserError(err)
token, err := jwt.GenerateToken(uint(userResp.UserDetail.UserID))
token, err := jwt.GenerateToken(userResp.UserDetail.UserId)
r := res.Response{
Data: res.TokenData{User: userResp.UserDetail, Token: token},
Status: uint(userResp.Code),
Msg: e.GetMsg(uint(userResp.Code)),
}
ginCtx.JSON(http.StatusOK, r)
ctx.JSON(http.StatusOK, r)
}
22 changes: 8 additions & 14 deletions app/task/internal/handler/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,30 @@ func (*TaskSrv) TaskShow(ctx context.Context, req *taskPb.TaskRequest) (resp *ta
EndTime: r[i].EndTime,
})
}
resp.TaskDetail = repository.BuildTasks(tRep)
return
}

func (*TaskService) TaskUpdate(ctx context.Context, req *service.TaskRequest) (resp *service.CommonResponse, err error) {
var task repository.Task
resp = new(service.CommonResponse)
resp.Code = e.SUCCESS
err = task.Update(req)
func (*TaskSrv) TaskUpdate(ctx context.Context, req *taskPb.TaskRequest) (resp *taskPb.CommonResponse, err error) {
err = dao.NewTaskDao(ctx).UpdateTask(req)
if err != nil {
resp.Code = e.ERROR
resp.Msg = e.GetMsg(e.ERROR)
resp.Data = err.Error()
return resp, err
return
}
resp.Msg = e.GetMsg(uint(resp.Code))
return resp, nil
return
}

func (*TaskService) TaskDelete(ctx context.Context, req *service.TaskRequest) (resp *service.CommonResponse, err error) {
var task repository.Task
resp = new(service.CommonResponse)
func (*TaskSrv) TaskDelete(ctx context.Context, req *taskPb.TaskRequest) (resp *taskPb.CommonResponse, err error) {
resp.Code = e.SUCCESS
err = task.Delete(req)
err = dao.NewTaskDao(ctx).DeleteTaskById(req.TaskID, req.UserID)
if err != nil {
resp.Code = e.ERROR
resp.Msg = e.GetMsg(e.ERROR)
resp.Data = err.Error()
return resp, err
return
}
resp.Msg = e.GetMsg(uint(resp.Code))
return resp, nil
return
}
16 changes: 8 additions & 8 deletions pkg/res/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (

// Response 基础序列化器
type Response struct {
Status uint `json:"Status"`
Data interface{} `json:"Data"`
Msg string `json:"Msg"`
Error string `json:"Error"`
Status uint `json:"status"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
Error string `json:"error"`
}

// DataList 带有总数的Data结构
type DataList struct {
Item interface{} `json:"Item"`
Total uint `json:"Total"`
Item interface{} `json:"item"`
Total uint `json:"total"`
}

// TokenData 带有token的Data结构
type TokenData struct {
User interface{} `json:"User"`
Token string `json:"Token"`
User interface{} `json:"user"`
Token string `json:"token"`
}

// 返回200 自定义code data
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/ctl/user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type key int
var userKey key

type UserInfo struct {
Id uint `json:"id"`
Id int64 `json:"id"`
}

func GetUserInfo(ctx context.Context) (*UserInfo, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
var jwtSecret = []byte(viper.GetString("server.jwtSecret"))

type Claims struct {
UserID uint `json:"user_id"`
UserID int64 `json:"user_id"`
jwt.StandardClaims
}

// GenerateToken 签发用户Token
func GenerateToken(userID uint) (string, error) {
func GenerateToken(userID int64) (string, error) {
nowTime := time.Now()
expireTime := nowTime.Add(24 * time.Hour)
claims := Claims{
Expand Down

0 comments on commit a18e00f

Please sign in to comment.