修改Order的命名域
This commit is contained in:
@@ -57,6 +57,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/user/add-address",
|
||||
Handler: UserAddAddressHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/user/order-delete",
|
||||
Handler: UserOderDeleteHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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 UserOderDeleteHandler(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.RequestOrderId
|
||||
// 如果端点有请求结构体,则使用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.NewUserOderDeleteLogic(r.Context(), svcCtx)
|
||||
resp := l.UserOderDelete(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,27 +31,24 @@ func (l *UserAddAddressLogic) UserAddAddress(req *types.RequestAddAddress, useri
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
if !userinfo.IsUser() {
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误
|
||||
}
|
||||
|
||||
// 确认这个IsDefault的值范围
|
||||
if !auth.CheckValueRange(req.IsDefault, 0, 1) {
|
||||
return resp.SetStatus(basic.CodeSafeValueRangeErr)
|
||||
return resp.SetStatus(basic.CodeSafeValueRangeErr) // IsDefault值超出范围, 返回安全值范围错误
|
||||
}
|
||||
|
||||
m := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
|
||||
var status int64 = 1
|
||||
m := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn) // 创建地址模型
|
||||
var status int64 = 1 // 默认地址状态为1(正常)
|
||||
|
||||
// 如果ID为0, 表示新增地址
|
||||
if req.Id == 0 {
|
||||
// $address->country = 'USA';
|
||||
// $address->status = 1;
|
||||
// $address->user_id = $uid;
|
||||
var (
|
||||
country string = "USA"
|
||||
|
||||
isDefautl int64 = 1
|
||||
country string = "USA" // 国家默认为美国
|
||||
isDefautl int64 = 1 // 默认地址为1
|
||||
)
|
||||
createOne := &gmodel.FsAddress{
|
||||
createOne := &gmodel.FsAddress{ // 构建FsAddress结构体
|
||||
Name: &req.Name,
|
||||
FirstName: &req.FirstName,
|
||||
LastName: &req.LastName,
|
||||
@@ -66,12 +63,12 @@ func (l *UserAddAddressLogic) UserAddAddress(req *types.RequestAddAddress, useri
|
||||
ZipCode: &req.ZipCode,
|
||||
IsDefault: &isDefautl,
|
||||
}
|
||||
_, err := m.CreateOne(l.ctx, createOne)
|
||||
created, err := m.CreateOne(l.ctx, createOne) // 新增地址
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbCreateErr)
|
||||
logx.Error(err) // 日志记录错误
|
||||
return resp.SetStatus(basic.CodeDbCreateErr) // 返回数据库创建错误
|
||||
}
|
||||
return resp.SetStatus(basic.CodeOK, map[string]int64{"id": createOne.Id})
|
||||
return resp.SetStatus(basic.CodeOK, map[string]int64{"id": created.Id}) // 返回成功并返回地址ID
|
||||
}
|
||||
|
||||
address := &gmodel.FsAddress{
|
||||
@@ -90,6 +87,7 @@ func (l *UserAddAddressLogic) UserAddAddress(req *types.RequestAddAddress, useri
|
||||
IsDefault: &req.IsDefault,
|
||||
}
|
||||
|
||||
// 插入数据库 更新地址
|
||||
err := m.UpdateAddAddress(l.ctx, address)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
|
||||
@@ -28,10 +28,13 @@ func NewUserAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U
|
||||
|
||||
func (l *UserAddressListLogic) UserAddressList(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
m := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
|
||||
userinfo.GetIdType()
|
||||
if !userinfo.IsUser() {
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
|
||||
data, err := m.GetUserAllAddress(l.ctx, 22)
|
||||
m := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
|
||||
|
||||
data, err := m.GetUserAllAddress(l.ctx, userinfo.UserId)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
|
||||
|
||||
79
server/home-user-auth/internal/logic/useroderdeletelogic.go
Normal file
79
server/home-user-auth/internal/logic/useroderdeletelogic.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserOderDeleteLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserOderDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOderDeleteLogic {
|
||||
return &UserOderDeleteLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserOderDeleteLogic) UserOderDelete(req *types.RequestOrderId, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
if !userinfo.IsUser() {
|
||||
return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误
|
||||
}
|
||||
|
||||
//订单id
|
||||
orderId := req.OrderId
|
||||
if orderId < 0 {
|
||||
return resp.SetStatus(basic.CodeRequestParamsErr)
|
||||
}
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
||||
func (l *OrderLogic) CancelOrder(req *types.RequestCancelOrder, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
if !userinfo.IsUser() {
|
||||
return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误
|
||||
}
|
||||
//订单id
|
||||
id := req.ReqParam.Get("id")
|
||||
if id == "" {
|
||||
return resp.SetStatus(basic.CodeParamErr)
|
||||
}
|
||||
//验证当前订单是否可以取消
|
||||
order, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).GetOne(l.ctx, map[string]interface{}{"user_id": userinfo.UserId, "id": id})
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeDbQueryErr)
|
||||
}
|
||||
if order == nil {
|
||||
return resp.SetStatus(basic.CodeNotFound)
|
||||
}
|
||||
//那些状态下是不能关闭的? (订单完成,退款完成,关闭订单)
|
||||
if order.Status == gconst.OrderStatusComplete ||
|
||||
order.Status == gconst.OrderStatusRefund ||
|
||||
order.Status == gconst.OrderStatusClose {
|
||||
return resp.SetStatus(basic.CodeCannotOperate)
|
||||
}
|
||||
//订单状态修改
|
||||
order.Status = gconst.OrderStatusDelete
|
||||
order.IsDeleted = 1
|
||||
result, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).UpdateOne(l.ctx, order)
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeDbUpdateErr)
|
||||
}
|
||||
return resp.SetStatus(basic.CodeOK, result)
|
||||
}
|
||||
@@ -31,7 +31,7 @@ type RequestUserLogin struct {
|
||||
|
||||
type RequestAddAddress struct {
|
||||
Id int64 `json:"id"` // address_id 地址id
|
||||
IsDefault int64 `json:"is_default"` //是否默认
|
||||
IsDefault int64 `json:"is_default"` //是否默认
|
||||
Name string `json:"name"` //收货人
|
||||
FirstName string `json:"first_name"` //first_name
|
||||
LastName string `json:"last_name"` //last_name
|
||||
@@ -43,6 +43,10 @@ type RequestAddAddress struct {
|
||||
State string `json:"state"` //州
|
||||
}
|
||||
|
||||
type RequestOrderId struct {
|
||||
OrderId int64 `json:"id"`
|
||||
}
|
||||
|
||||
type DataUserLogin struct {
|
||||
Token string `json:"token"` // 登录jwt token
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user