1. router相关代码
1. message
package router
import (
"github.com/gin-gonic/gin"
"mxshop-api/userop-web/api/message"
"mxshop-api/userop-web/middlewares"
)
func InitMessageRouter(Router *gin.RouterGroup) {
MessageRouter := Router.Group("message")
{
MessageRouter.GET("", middlewares.JWTAuth(), message.List) // 轮播图列表页
MessageRouter.POST("", middlewares.JWTAuth(), message.New) //新建轮播图
}
}
2. address
package router
import (
"github.com/gin-gonic/gin"
"mxshop-api/userop-web/api/address"
"mxshop-api/userop-web/middlewares"
)
func InitAddressRouter(Router *gin.RouterGroup) {
AddressRouter := Router.Group("address")
{
AddressRouter.GET("", middlewares.JWTAuth(), address.List) // 轮播图列表页
AddressRouter.DELETE("/:id", middlewares.JWTAuth(), address.Delete) // 删除轮播图
AddressRouter.POST("", middlewares.JWTAuth(), address.New) //新建轮播图
AddressRouter.PUT("/:id",middlewares.JWTAuth(), address.Update) //修改轮播图信息
}
}
3. user_fav
package router
import (
"github.com/gin-gonic/gin"
"mxshop-api/userop-web/api/user_fav"
"mxshop-api/userop-web/middlewares"
)
func InitUserFavRouter(Router *gin.RouterGroup) {
UserFavRouter := Router.Group("userfavs")
{
UserFavRouter.DELETE("/:id", middlewares.JWTAuth(), user_fav.Delete) // 删除收藏记录
UserFavRouter.GET("/:id", middlewares.JWTAuth(), user_fav.Detail) // 获取收藏记录
UserFavRouter.POST("", middlewares.JWTAuth(), user_fav.New) //新建收藏记录
UserFavRouter.GET("", middlewares.JWTAuth(), user_fav.List) //获取当前用户的收藏
}
}
2. handler相关代码
1. message
package message
import (
"context"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"mxshop-api/userop-web/api"
"mxshop-api/userop-web/forms"
"mxshop-api/userop-web/global"
"mxshop-api/userop-web/proto"
"net/http"
)
func List(ctx *gin.Context) {
rsp, err := global.MessageClient.MessageList(context.Background(), &proto.MessageRequest{})
if err != nil {
zap.S().Errorw("获取留言失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
reMap := map[string]interface{}{
"total": rsp.Total,
}
result := make([]interface{}, 0)
for _, value := range rsp.Data {
reMap := make(map[string]interface{})
reMap["id"] = value.Id
reMap["user_id"] = value.UserId
reMap["type"] = value.MessageType
reMap["subject"] = value.Subject
reMap["message"] = value.Message
reMap["file"] = value.File
result = append(result, reMap)
}
reMap["data"] = result
ctx.JSON(http.StatusOK, reMap)
}
func New(ctx *gin.Context) {
messageForm := forms.MessageForm{}
if err := ctx.ShouldBindJSON(&messageForm); err != nil {
api.HandleValidatorError(ctx, err)
return
}
rsp, err := global.MessageClient.CreateMessage(context.Background(), &proto.MessageRequest{
MessageType: messageForm.MessageType,
Subject: messageForm.Subject,
Message: messageForm.Message,
File: messageForm.File,
})
if err != nil {
zap.S().Errorw("添加留言失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
request := make(map[string]interface{})
request["id"] = rsp.Id
ctx.JSON(http.StatusOK, request)
}
2. address
package address
import (
"context"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"mxshop-api/userop-web/api"
"mxshop-api/userop-web/forms"
"mxshop-api/userop-web/global"
"mxshop-api/userop-web/models"
"mxshop-api/userop-web/proto"
"net/http"
"strconv"
)
func List(ctx *gin.Context) {
request := &proto.AddressRequest{}
claims, _ := ctx.Get("claims")
currentUser := claims.(*models.CustomClaims)
if currentUser.AuthorityId != 2 {
userId, _ := ctx.Get("userId")
request.UserId = int32(userId.(uint))
}
rsp, err := global.AddressClient.GetAddressList(context.Background(), request)
if err != nil {
zap.S().Errorw("获取地址列表失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
reMap := map[string]interface{}{
"total": rsp.Total,
}
result := make([]interface{}, 0)
for _, value := range rsp.Data {
reMap := make(map[string]interface{})
reMap["id"] = value.Id
reMap["user_id"] = value.UserId
reMap["province"] = value.Province
reMap["city"] = value.City
reMap["district"] = value.District
reMap["address"] = value.Address
reMap["signer_name"] = value.SignerName
reMap["signer_mobile"] = value.SignerMobile
result = append(result, reMap)
}
reMap["data"] = result
ctx.JSON(http.StatusOK, reMap)
}
func New(ctx *gin.Context) {
addressForm := forms.AddressForm{}
if err := ctx.ShouldBindJSON(&addressForm); err != nil {
api.HandleValidatorError(ctx, err)
return
}
rsp, err := global.AddressClient.CreateAddress(context.Background(), &proto.AddressRequest{
Province: addressForm.Province,
City: addressForm.City,
District: addressForm.District,
Address: addressForm.Address,
SignerName: addressForm.SignerName,
SignerMobile: addressForm.SignerMobile,
})
if err != nil {
zap.S().Errorw("新建地址失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
request := make(map[string]interface{})
request["id"] = rsp.Id
ctx.JSON(http.StatusOK, request)
}
func Delete(ctx *gin.Context) {
id := ctx.Param("id")
i, err := strconv.ParseInt(id, 10, 32)
if err != nil {
ctx.Status(http.StatusNotFound)
return
}
_, err = global.AddressClient.DeleteAddress(context.Background(), &proto.AddressRequest{Id: int32(i)})
if err != nil {
zap.S().Errorw("删除地址失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
ctx.JSON(http.StatusOK, "")
}
func Update(ctx *gin.Context) {
addressForm := forms.AddressForm{}
if err := ctx.ShouldBindJSON(&addressForm); err != nil {
api.HandleValidatorError(ctx, err)
return
}
id := ctx.Param("id")
i, err := strconv.ParseInt(id, 10, 32)
if err != nil {
ctx.Status(http.StatusNotFound)
return
}
_, err = global.AddressClient.UpdateAddress(context.Background(), &proto.AddressRequest{
Id: int32(i),
Province: addressForm.Province,
City: addressForm.City,
District: addressForm.District,
Address: addressForm.Address,
SignerName: addressForm.SignerName,
SignerMobile: addressForm.SignerMobile,
})
if err != nil {
zap.S().Errorw("更新地址失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
request := make(map[string]interface{})
ctx.JSON(http.StatusOK, request)
}
3. userfav
package user_fav
import (
"context"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"mxshop-api/userop-web/api"
"mxshop-api/userop-web/forms"
"mxshop-api/userop-web/global"
"mxshop-api/userop-web/proto"
"net/http"
"strconv"
)
func List(ctx *gin.Context) {
userId, _ := ctx.Get("userId")
userFavRsp, err := global.UserFavClient.GetFavList(context.Background(), &proto.UserFavRequest{
UserId: int32(userId.(uint)),
})
if err != nil {
zap.S().Errorw("获取收藏列表失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
ids := make([]int32, 0)
for _, item := range userFavRsp.Data{
ids = append(ids, item.GoodsId)
}
if len(ids) == 0 {
ctx.JSON(http.StatusOK, gin.H{
"total":0,
})
return
}
//请求商品服务
goods, err := global.GoodsSrvClient.BatchGetGoods(context.Background(), &proto.BatchGoodsIdInfo{
Id: ids,
})
if err != nil {
zap.S().Errorw("[List] 批量查询【商品列表】失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
reMap := map[string]interface{}{
"total": userFavRsp.Total,
}
goodsList := make([]interface{}, 0)
for _, item := range userFavRsp.Data{
data := gin.H{
"id":item.GoodsId,
}
for _, good := range goods.Data {
if item.GoodsId == good.Id {
data["name"] = good.Name
data["shop_price"] = good.ShopPrice
}
}
goodsList = append(goodsList, data)
}
reMap["data"] = goodsList
ctx.JSON(http.StatusOK, reMap)
}
func New(ctx *gin.Context) {
userFavForm := forms.UserFavForm{}
if err := ctx.ShouldBindJSON(&userFavForm); err != nil {
api.HandleValidatorError(ctx, err)
return
}
//缺少一步, 这个时候应该去商品服务查询一下这个是否存在
userId, _ := ctx.Get("userId")
_, err := global.UserFavClient.AddUserFav(context.Background(), &proto.UserFavRequest{
UserId: int32(userId.(uint)),
GoodsId: userFavForm.GoodsId,
})
if err != nil {
zap.S().Errorw("添加收藏记录失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
ctx.JSON(http.StatusOK, gin.H{})
}
func Delete(ctx *gin.Context) {
id := ctx.Param("id")
i, err := strconv.ParseInt(id, 10, 32)
if err != nil {
ctx.Status(http.StatusNotFound)
return
}
userId, _ := ctx.Get("userId")
_, err = global.UserFavClient.DeleteUserFav(context.Background(), &proto.UserFavRequest{
UserId: int32(userId.(uint)),
GoodsId: int32(i),
})
if err != nil {
zap.S().Errorw("删除收藏记录失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg":"删除成功",
})
}
func Detail(ctx *gin.Context) {
goodsId := ctx.Param("id")
goodsIdInt, err := strconv.ParseInt(goodsId, 10, 32)
if err != nil {
ctx.Status(http.StatusNotFound)
return
}
userId, _ := ctx.Get("userId")
_, err = global.UserFavClient.GetUserFavDetail(context.Background(), &proto.UserFavRequest{
UserId: int32(userId.(uint)),
GoodsId: int32(goodsIdInt),
})
if err != nil {
zap.S().Errorw("查询收藏状态失败")
api.HandleGrpcErrorToHttp(err, ctx)
return
}
ctx.Status(http.StatusOK)
}
3. form表单
1. message
package forms
type MessageForm struct {
MessageType int32 `form:"type" json:"type" binding:"required,oneof=1 2 3 4 5"`
Subject string `form:"subject" json:"subject" binding:"required"`
Message string `form:"message" json:"message" binding:"required"`
File string `form:"file" json:"file" binding:"required"`
}
2. address
package forms
type AddressForm struct {
Province string `form:"province" json:"province" binding:"required"`
City string `form:"city" json:"city" binding:"required"`
District string `form:"district" json:"district" binding:"required"`
Address string `form:"address" json:"address" binding:"required"`
SignerName string `form:"signer_name" json:"signer_name" binding:"required"`
SignerMobile string `form:"signer_mobile" json:"signer_mobile" binding:"required"`
}
2. userfav
package forms
type UserFavForm struct {
GoodsId int32 `form:"goods" json:"goods" binding:"required"`
}