156 lines
6.1 KiB
Go
156 lines
6.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/context"
|
|
"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"
|
|
)
|
|
|
|
type ServiceAddrListResponse struct {
|
|
UserServiceAddrList []*models.UserServiceAddr `json:"userServiceAddrList"`
|
|
}
|
|
|
|
// 我的服务地址列表
|
|
func (p DefParty) serviceAddrList() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, 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{Prefix: p.Prefix, 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,
|
|
Status: 1,
|
|
}
|
|
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
|
|
}
|
|
//最远服务距离>0 才计算
|
|
/*if serviceAddr.DistantGapMeters > 0 {
|
|
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
|
|
}
|
|
}*/
|
|
/*recogResponse := utils.GetAddrRecog(utils.BmapRecogRequest{Address: userServiceAddr.Addr})
|
|
if len(serviceAddr.ServiceAreaId) > 0 {
|
|
zap_server.ZAPLOG.Info("serviceArea", zap.Any("response", recogResponse), zap.Any("serviceAddr", serviceAddr.ServiceAreaId))
|
|
if recogResponse.County != serviceAddr.ServiceAreaId {
|
|
NotInServiceExistError.Fail(ctx, serviceAddOrEditRequest)
|
|
return
|
|
}
|
|
}*/
|
|
//userServiceAddr.AddrArea = recogResponse.County
|
|
|
|
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 userInfo *models.User
|
|
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
|
|
updateValues := map[string]interface{}{
|
|
"Mobile": userInfo.Mobile,
|
|
}
|
|
database.Instance().Model(&userInfo).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{Prefix: p.Prefix, 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})
|
|
})
|
|
}}
|
|
}
|