新增用户服务连接

This commit is contained in:
zyj
2025-09-02 11:40:46 +08:00
parent 3aae35abb7
commit 7371d04216
11 changed files with 195 additions and 0 deletions

53
api/user.go Normal file
View File

@@ -0,0 +1,53 @@
package api
import (
"context"
"fmt"
grpcClient "go-desk-service/grpc-client"
"go-desk-service/libs"
userpb "go-desk-service/proto/gen"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type User struct {
}
type LoginRequestParams struct {
Username string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"`
Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
}
func (*User) Login(ctx *gin.Context) {
var res *libs.ErrorInfo
var paramsJson LoginRequestParams
if err := ctx.ShouldBindJSON(&paramsJson); err != nil {
res = libs.ErrorCode["ParamsError"]
fmt.Println(err)
ctx.JSON(http.StatusBadRequest, gin.H{
"code": res.Code,
"msg": res.Msg,
"data": res.Data,
})
return
}
// 获取当前的grpc连接
client := grpcClient.GetUserClient()
ctx1, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
loginResp, err := client.Login(ctx1, &userpb.LoginRequest{
Username: paramsJson.Username,
Password: paramsJson.Password,
})
if err != nil {
log.Fatalf("登录失败: %v", err)
}
ctx.JSON(http.StatusBadRequest, gin.H{
"code": 200,
"msg": "登录成功",
"data": loginResp,
})
}