新增一系列接口

This commit is contained in:
yan.y
2024-03-29 00:24:07 +08:00
parent 0884384e91
commit c799107d6c
17 changed files with 511 additions and 315 deletions
+4
View File
@@ -35,6 +35,10 @@ func (p DefParty) login() web_iris.Party {
body, _ := io.ReadAll(ctx.Request().Body)
var loginRequest LoginRequest
json.Unmarshal(body, &loginRequest)
if len(loginRequest.Code) == 0 {
ParamError.Fail(ctx, nil)
return
}
//获取code
/*info, err := utils.GetWxUserInfo(loginRequest.Code)
if err != nil {
+57 -16
View File
@@ -8,6 +8,7 @@ import (
"go.uber.org/zap"
"pet-house.com/business/models"
"pet-house.com/core/server/cache"
"pet-house.com/core/server/cron_server"
"pet-house.com/core/server/database"
"pet-house.com/core/server/zap_server"
"strconv"
@@ -15,9 +16,9 @@ import (
)
type Response struct {
Code int `json:"code"` //code
Msg string `json:"msg"` //msg
Data any `json:"data"` //数据
Code int `json:"code"` //code
Msg string `json:"msg"` //msg
Data any `json:"data,omitempty"` //数据
}
type ResponseData struct {
@@ -34,13 +35,17 @@ type DefParty struct {
}
var (
ParamError = Error{Code: 201, Msg: "参数错误"}
IllegalError = Error{Code: 202, Msg: "非法请求"}
UserError = Error{Code: 203, Msg: "用户错误"}
TokenError = Error{Code: 204, Msg: "Token失效,请重新登录"}
UserNotExistError = Error{Code: 205, Msg: "用户不存在"}
PetNotExistError = Error{Code: 206, Msg: "用户宠物不存在"}
PetBaseNotExistError = Error{Code: 207, Msg: "宠物基础信息不存在"}
ParamError = Error{Code: 201, Msg: "参数错误"}
IllegalError = Error{Code: 202, Msg: "非法请求"}
UserError = Error{Code: 203, Msg: "用户错误"}
TokenError = Error{Code: 204, Msg: "Token失效,请重新登录"}
UserNotExistError = Error{Code: 205, Msg: "用户不存在"}
PetNotExistError = Error{Code: 206, Msg: "用户宠物不存在"}
PetBaseNotExistError = Error{Code: 207, Msg: "宠物基础信息不存在"}
UserServiceAddrNotExistError = Error{Code: 207, Msg: "用户服务地址不存在"}
ServiceAddrNotExistError = Error{Code: 208, Msg: "服务地址区域,请重新配置地址"}
NotInServiceExistError = Error{Code: 209, Msg: "不在服务范围"}
OrderCreateError = Error{Code: 210, Msg: "订单创建失败"}
)
func Success(ctx *context.Context, request any, data any) {
@@ -83,11 +88,6 @@ func GetTokenInfo(token string) string {
return uId
}
func checkToken(uid string, token string) bool {
uToken, _ := cache.GetCacheString("u_uid_token:" + uid)
return uToken == token
}
type HeaderBaseInfo struct {
Token string
Uid int64
@@ -107,13 +107,54 @@ type FrontExclude struct {
}
var PetBaseInfoMap map[int]models.PetBaseInfo
var ServiceAddrMap map[int64]models.ServiceAddr
var PetGoodsMap map[string][]models.PetGoods
var GoodsMap map[int64]models.Goods
func DataInit() {
//--------------------------------------------------宠物基础信息数据---------------------------------------------------------
var petBastInfoList []models.PetBaseInfo
database.Instance().Model(&models.PetBaseInfo{}).Find(&petBastInfoList)
PetBaseInfoMap = make(map[int]models.PetBaseInfo)
for _, value := range petBastInfoList {
PetBaseInfoMap[value.Id] = value
}
zap_server.ZAPLOG.Info("dataInit : ", zap.Any("petBastInfoMap", PetBaseInfoMap))
zap_server.ZAPLOG.Info("dataInit petBastInfoMap : ", zap.Any("petBastInfoMap", PetBaseInfoMap))
//--------------------------------------------------服务地址数据---------------------------------------------------------
var serviceAddrList []models.ServiceAddr
database.Instance().Model(&models.ServiceAddr{}).Find(&serviceAddrList)
ServiceAddrMap = make(map[int64]models.ServiceAddr)
for _, value := range serviceAddrList {
ServiceAddrMap[value.Id] = value
}
zap_server.ZAPLOG.Info("dataInit ServiceAddrMap : ", zap.Any("ServiceAddrMap", ServiceAddrMap))
//--------------------------------------------------宠物商品关联数据---------------------------------------------------------
var petGoodsList []models.PetGoods
database.Instance().Model(&models.PetGoods{}).Find(&petGoodsList)
PetGoodsMap = make(map[string][]models.PetGoods)
for _, value := range petGoodsList {
key := strconv.Itoa(value.Assortment) + strconv.Itoa(value.PetType) + strconv.Itoa(value.Size)
if _, ok := PetGoodsMap[key]; ok {
PetGoodsMap[key] = append(PetGoodsMap[key], value)
} else {
PetGoodsMap[key] = []models.PetGoods{value}
}
}
zap_server.ZAPLOG.Info("dataInit petGoodsMap : ", zap.Any("petGoodsMap", PetGoodsMap))
var goodsList []models.Goods
database.Instance().Model(&models.Goods{}).Find(&goodsList)
GoodsMap = make(map[int64]models.Goods)
for _, value := range goodsList {
GoodsMap[value.Id] = value
}
zap_server.ZAPLOG.Info("dataInit GoodsMap : ", zap.Any("GoodsMap", GoodsMap))
}
func DataCacheJob() {
c, err := cron_server.CronInstance().AddFunc("@every 1m", DataInit)
if err != nil {
zap_server.ZAPLOG.Info("DataCacheJob err : ", zap.Any("err", err))
}
zap_server.ZAPLOG.Info("DataCacheJob c : ", zap.Any("c", c), zap.Any("err", err))
cron_server.CronInstance().Start()
}
+4
View File
@@ -15,13 +15,17 @@ func (p DefParty) RegisterList() []web_iris.Party {
p.goodsList(),
p.goodsDetail(),
p.orderCreate(),
p.orderServiceTime(),
p.orderList(),
p.orderPay(),
p.petList(),
p.petInfo(),
p.petTypeList(),
p.petAddOrEdit(),
p.delPet(),
p.serviceAddrList(),
p.serviceAddOrEdit(),
p.serviceAreaAddrList(),
p.index(),
}
return ps
+76 -2
View File
@@ -1,25 +1,99 @@
package api
import (
"encoding/json"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"io"
"pet-house.com/core/server/web/web_iris"
"strconv"
)
type GoodsListRequest struct {
Pid int64
}
type GoodsDetail struct {
Id int64 `json:"id"` //商品ID
Name string `json:"name"` //商品名称
GoodsType int `json:"goodsType"` //商品类型 1基础服务 2附加产品 3附加服务
Time string `json:"time"` //时间
Price int32 `json:"price"` //价格
ParentId int64 `json:"parentId"` //商品父ID
BuyMany int `json:"buyMany"` //同类型是否可购买多个 0否 1是
GoodsDetail string `json:"goodsDetail"` //商品详情 图片URL
GoodsIcon string `json:"goodsIcon"` //iconUrl
GoodsSubList []GoodsDetail `json:"goodsSubList,omitempty"`
}
type GoodsListResponse struct {
GoodsList []GoodsDetail `json:"goodsList"`
}
// 商品列表
func (p DefParty) goodsList() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(GoodsBase+"/goodsList", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
body, _ := io.ReadAll(ctx.Request().Body)
var goodsListRequest GoodsListRequest
json.Unmarshal(body, &goodsListRequest)
userPetInfo := GetUserPet(headerBaseInfo.Uid, goodsListRequest.Pid)
if userPetInfo.PetInfo.Id == 0 {
PetNotExistError.Fail(ctx, goodsListRequest)
return
}
//先取默认的商品
key := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetBaseInfo.Size)
goodsList := getPetGoodsList(key)
key1 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetBaseInfo.Size)
goodsList = append(goodsList, getPetGoodsList(key1)...)
Success(ctx, goodsListRequest, GoodsListResponse{goodsList})
})
}}
}
func getPetGoodsList(key string) []GoodsDetail {
var goodsList []GoodsDetail
baseGoodsList := PetGoodsMap[key]
for _, value := range baseGoodsList {
goods := GoodsMap[value.GoodsId]
goodsDetail := GoodsDetail{
Id: goods.Id,
Name: goods.Name,
GoodsType: goods.GoodsType,
Time: goods.Time,
Price: goods.Price,
BuyMany: goods.BuyMany,
GoodsDetail: goods.GoodsDetail,
GoodsIcon: goods.GoodsIcon,
GoodsSubList: nil,
}
for _, goodsSub := range GoodsMap {
if goodsSub.ParentId == goodsDetail.Id {
goodsDetail.GoodsSubList = append(goodsDetail.GoodsSubList, GoodsDetail{
Id: goodsSub.Id,
Name: goodsSub.Name,
GoodsType: goodsSub.GoodsType,
Time: goodsSub.Time,
Price: goodsSub.Price,
BuyMany: goodsSub.BuyMany,
GoodsDetail: goodsSub.GoodsDetail,
GoodsIcon: goodsSub.GoodsIcon,
})
}
}
goodsList = append(goodsList, goodsDetail)
}
return goodsList
}
// 商品详情
func (p DefParty) goodsDetail() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(GoodsBase+"/goodsDetail", func(ctx *context.Context) {
//暂留
Success(ctx, nil, nil)
})
}}
}
+3
View File
@@ -29,10 +29,13 @@ func ModuleInit() {
&models.ServiceAddr{},
&models.UserServiceAddr{},
&models.Goods{},
&models.PetGoods{},
&models.SystemConfig{},
&models.OrderMain{},
&models.OrderSub{},
&models.OrderDetail{})
DataInit()
DataCacheJob()
}
func FrontAuth(ctx *context.Context) {
+101
View File
@@ -1,15 +1,116 @@
package api
import (
"encoding/json"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"go.uber.org/zap"
"io"
"pet-house.com/business/models"
"pet-house.com/core/server/database"
"pet-house.com/core/server/web/web_iris"
"pet-house.com/core/server/zap_server"
)
type PetGoodsInfo struct {
PetId int64
GoodsIds []int64
}
type OrderCreateRequest struct {
PetGoodsInfos []PetGoodsInfo //宠物商品信息列表
ServiceTime string //预约时间 yyyy-MM-dd HH:mm
ServiceAddrId int64 //预约服务地址
}
// 创建
func (p DefParty) orderCreate() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderCreate", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
body, _ := io.ReadAll(ctx.Request().Body)
var orderCreateRequest OrderCreateRequest
json.Unmarshal(body, &orderCreateRequest)
var userServiceAddr models.UserServiceAddr
database.Instance().Model(&models.UserServiceAddr{}).Where("uid = ? and id = ?", headerBaseInfo.Uid, orderCreateRequest.ServiceAddrId).Find(&userServiceAddr)
if userServiceAddr.Id == 0 {
UserServiceAddrNotExistError.Fail(ctx, nil)
return
}
serviceAddr := ServiceAddrMap[userServiceAddr.AreaId]
if serviceAddr.Id == 0 {
ServiceAddrNotExistError.Fail(ctx, nil)
return
}
orderId := NextId.Generate().String()
orderMain := models.OrderMain{
OrderId: orderId,
Uid: headerBaseInfo.Uid,
Status: 1,
ServiceTime: orderCreateRequest.ServiceTime,
ServiceAddrId: orderCreateRequest.ServiceAddrId,
ServiceRemark: "",
}
var orderSubList []models.OrderSub
var orderSubDetailList []models.OrderDetail
for _, value := range orderCreateRequest.PetGoodsInfos {
subOrderId := NextId.Generate().String()
var totalAmount int32 = 0
for _, value := range value.GoodsIds {
goods := GoodsMap[value]
totalAmount = totalAmount + goods.Price
orderSubDetailList = append(orderSubDetailList, models.OrderDetail{SubOrderId: subOrderId, GoodsId: value, Amount: goods.Price})
}
orderSub := models.OrderSub{
OrderId: subOrderId,
MainOrderId: orderMain.OrderId,
Status: 1,
PetId: value.PetId,
PayType: 1,
Discount: 100,
TotalAmount: totalAmount,
PayAmount: 0,
}
orderSubList = append(orderSubList, orderSub)
}
tx := database.Instance().Begin()
db := tx.Model(&models.OrderDetail{}).CreateInBatches(&orderSubDetailList, len(orderSubDetailList))
db1 := tx.Model(&models.OrderSub{}).CreateInBatches(&orderSubList, len(orderSubList))
db2 := tx.Model(&models.OrderMain{}).Create(&orderMain)
if db.Error != nil || db1.Error != nil || db2.Error != nil {
tx.Callback()
zap_server.ZAPLOG.Error("订单插入失败", zap.Any("db", db.Error), zap.Any("db1", db1.Error), zap.Any("db2", db2.Error))
OrderCreateError.Fail(ctx, orderCreateRequest)
return
}
tx.Commit()
Success(ctx, orderCreateRequest, nil)
})
}}
}
type OrderListRequest struct {
Status int //订单状态
PageNo int //页码
PageSize int //数据数量
}
type OrderListResponse struct {
}
// 订单列表
func (p DefParty) orderList() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderList", func(ctx *context.Context) {
})
}}
}
// 获取订单可预约时间
func (p DefParty) orderServiceTime() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderServiceTime", func(ctx *context.Context) {
})
}}
+23
View File
@@ -157,3 +157,26 @@ func (p DefParty) petTypeList() web_iris.Party {
})
}}
}
type DelPetRequest struct {
Id int64
}
func (p DefParty) delPet() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(PetBase+"/delPet", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
body, _ := io.ReadAll(ctx.Request().Body)
var delPetRequest DelPetRequest
json.Unmarshal(body, &delPetRequest)
var userPetInfo models.Pet
database.Instance().Model(&models.Pet{}).Where("id = ? and uid = ?", delPetRequest.Id, headerBaseInfo.Uid).Find(&userPetInfo)
if userPetInfo.Id == 0 {
PetNotExistError.Fail(ctx, delPetRequest)
return
}
database.Instance().Where("id = ?", userPetInfo.PetId).Delete(&models.Pet{})
Success(ctx, nil, nil)
})
}}
}
+116 -2
View File
@@ -1,25 +1,139 @@
package api
import (
"encoding/json"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
geo "github.com/kellydunn/golang-geo"
"go.uber.org/zap"
"io"
"pet-house.com/business/models"
"pet-house.com/business/utils"
"pet-house.com/core/server/database"
"pet-house.com/core/server/web/web_iris"
"pet-house.com/core/server/zap_server"
strconv "strconv"
)
// 服务地址列表
type ServiceAddrListResponse struct {
UserServiceAddrList []*models.UserServiceAddr `json:"userServiceAddrList"`
}
// 我的服务地址列表
func (p DefParty) serviceAddrList() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(ServiceBase+"/serviceAddrList", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
var userServiceAddrList []*models.UserServiceAddr
database.Instance().Model(&models.UserServiceAddr{}).Where("uid = ?", headerBaseInfo.Uid).Find(&userServiceAddrList)
for _, value := range userServiceAddrList {
value.Area = ServiceAddrMap[value.AreaId].Addr
}
Success(ctx, headerBaseInfo, ServiceAddrListResponse{userServiceAddrList})
})
}}
}
type ServiceAddOrEditRequest struct {
Id int64
Name string
Mobile string
AreaId int64
Addr string
Longitude string
Latitude string
}
type ServiceAddOrEditResponse struct {
ServiceAddrList []*models.UserServiceAddr `json:"serviceAddrList"`
}
// 服务地址添加或编辑
func (p DefParty) serviceAddOrEdit() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(ServiceBase+"/serviceAddOrEdit", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
body, _ := io.ReadAll(ctx.Request().Body)
var serviceAddOrEditRequest ServiceAddOrEditRequest
json.Unmarshal(body, &serviceAddOrEditRequest)
var serviceAddr models.ServiceAddr
serviceAddr = ServiceAddrMap[serviceAddOrEditRequest.AreaId]
if serviceAddr.Id == 0 {
ServiceAddrNotExistError.Fail(ctx, serviceAddOrEditRequest)
return
}
database.Instance().Model(&models.ServiceAddr{}).Where("id = ?", serviceAddOrEditRequest.AreaId).Find(&serviceAddr)
userServiceAddr := models.UserServiceAddr{
Uid: headerBaseInfo.Uid,
Name: serviceAddOrEditRequest.Name,
Mobile: serviceAddOrEditRequest.Mobile,
AreaId: serviceAddr.Id,
Addr: serviceAddOrEditRequest.Addr,
Longitude: serviceAddOrEditRequest.Longitude,
Latitude: serviceAddOrEditRequest.Latitude,
}
if len(serviceAddOrEditRequest.Longitude) == 0 || len(serviceAddOrEditRequest.Latitude) == 0 {
addrGeocoding := utils.GetAddrGeocoding(utils.BmapGeoCodingRequest{Address: serviceAddOrEditRequest.Addr})
userServiceAddr.Longitude = addrGeocoding.Longitude
userServiceAddr.Latitude = addrGeocoding.Latitude
}
serviceAddrLat, _ := strconv.ParseFloat(serviceAddr.Latitude, 64)
serviceAddrLng, _ := strconv.ParseFloat(serviceAddr.Longitude, 64)
servicePoint := geo.NewPoint(serviceAddrLat, serviceAddrLng)
userAddrLat, _ := strconv.ParseFloat(userServiceAddr.Latitude, 64)
userAddrLng, _ := strconv.ParseFloat(userServiceAddr.Longitude, 64)
userPoint := geo.NewPoint(userAddrLat, userAddrLng)
// 计算两个点之间的距离(单位:米)
distance := servicePoint.GreatCircleDistance(userPoint) * 1000
zap_server.ZAPLOG.Info("服务距离", zap.Any("distance", distance), zap.Any("服务经纬度", servicePoint), zap.Any("用户经纬度", userPoint))
//超出服务范围
if distance >= serviceAddr.DistantGapMeters {
NotInServiceExistError.Fail(ctx, serviceAddOrEditRequest)
return
}
if serviceAddOrEditRequest.Id == 0 {
database.Instance().Model(&models.UserServiceAddr{}).Create(&userServiceAddr)
} else {
userServiceAddr.Id = serviceAddOrEditRequest.Id
var findUserServiceAddr models.UserServiceAddr
database.Instance().Model(&models.UserServiceAddr{}).Where("id = ? and uid = ?", serviceAddOrEditRequest.Id, headerBaseInfo.Uid).Find(&findUserServiceAddr)
if findUserServiceAddr.Id == 0 {
UserServiceAddrNotExistError.Fail(ctx, serviceAddOrEditRequest)
return
}
updateValues := map[string]interface{}{
"Uid": headerBaseInfo.Uid,
"Name": serviceAddOrEditRequest.Name,
"Mobile": serviceAddOrEditRequest.Mobile,
"AreaId": serviceAddr.Id,
"Addr": serviceAddOrEditRequest.Addr,
"Longitude": serviceAddOrEditRequest.Longitude,
"Latitude": serviceAddOrEditRequest.Latitude,
}
database.Instance().Model(&userServiceAddr).Updates(&updateValues)
}
var userServiceAddrList []*models.UserServiceAddr
database.Instance().Model(&models.UserServiceAddr{}).Where("uid = ?", headerBaseInfo.Uid).Find(&userServiceAddrList)
for _, value := range userServiceAddrList {
value.Area = ServiceAddrMap[value.AreaId].Addr
}
Success(ctx, serviceAddOrEditRequest, ServiceAddOrEditResponse{userServiceAddrList})
})
}}
}
type ServiceAreaAddrListResponse struct {
ServiceAddrList []models.ServiceAddr `json:"serviceAddrList"`
}
// 服务区域列表
func (p DefParty) serviceAreaAddrList() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(ServiceBase+"/serviceAreaAddrList", func(ctx *context.Context) {
var serviceAddrList []models.ServiceAddr
database.Instance().Model(&models.ServiceAddr{}).Find(&serviceAddrList)
Success(ctx, nil, ServiceAreaAddrListResponse{serviceAddrList})
})
}}
}