TODO: 反射

This commit is contained in:
eson
2023-06-28 19:32:41 +08:00
parent 0851c922ef
commit d4e75b1efc
27 changed files with 527 additions and 186 deletions

View File

@@ -57,6 +57,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/user/add-address",
Handler: UserAddAddressHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/user/contact-service",
Handler: UserContactServiceHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/user/order-delete",

View File

@@ -0,0 +1,78 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserContactServiceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.UserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
if claims != nil {
// 从token中获取对应的用户信息
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
} else {
// 如果claims为nil,则认为用户身份为白板用户
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
}
var req types.RequestContactService
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUserContactServiceLogic(r.Context(), svcCtx)
resp := l.UserContactService(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,66 @@
package logic
import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/collect"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type UserContactServiceLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserContactServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserContactServiceLogic {
return &UserContactServiceLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserContactServiceLogic) UserContactService(req *types.RequestContactService, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if !userinfo.IsUser() {
return resp.SetStatus(basic.CodeUnAuth)
}
cs := gmodel.FsContactService{}
collect.LoadJsonTag(cs, req)
switch req.Type {
case "order":
_, err := l.svcCtx.AllModels.FsOrder.FindOneAndCreateServiceContact(l.ctx, userinfo.UserId, req.RelationID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return resp.SetStatus(basic.CodeOrderNotFoundErr)
}
return resp.SetStatus(basic.CodeDbSqlErr)
}
case "cloud":
_, err := l.svcCtx.AllModels.FsCloudPickUp.GetCloudPickUpByIDAndUserID(l.ctx, userinfo.UserId, req.RelationID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return resp.SetStatus(basic.CodeCloudOrderNotFoundErr)
}
return resp.SetStatus(basic.CodeDbSqlErr)
}
default:
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "type is unknown")
}
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -5,6 +5,15 @@ import (
"fusenapi/utils/basic"
)
type RequestContactService struct {
Type string `json:"type"` // 类型
RelationID int64 `json:"relation_id"` // 关系id
Name string `json:"name"` // 名字
Email string `json:"email"` // email
Phone string `json:"phone,optional"` // phone
Remark string `json:"remark,optional"` // remark标记
}
type RequestBasicInfoForm struct {
FirstName string `json:"first_name"` // FirstName
LastName string `json:"last_name"` // LastName
@@ -123,6 +132,13 @@ type Auth struct {
RefreshAfter int64 `json:"refreshAfter"`
}
type Pagnation struct {
TotalCount int64 `json:"total_count"`
TotalPage int64 `json:"total_page"`
CurPage int64 `json:"cur_page"`
PageSize int64 `json:"page_size"`
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{