206 lines
6.1 KiB
Go
206 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/core/server/database"
|
|
"pet-house.com/core/server/web/web_iris"
|
|
)
|
|
|
|
var CatWeightMap = map[int]string{
|
|
1: "0-1.5kg",
|
|
2: "1.5-3kg",
|
|
3: "3-5kg",
|
|
4: "5-8kg",
|
|
//5: "8kg以上",
|
|
}
|
|
var DogWeightMap = map[int]string{
|
|
1: "0-3kg",
|
|
2: "3-6kg",
|
|
3: "6-9kg",
|
|
4: "9-13kg",
|
|
5: "13-18kg",
|
|
//6: "18-22kg",
|
|
//7: "22-30kg",
|
|
}
|
|
|
|
type UserPetInfo struct {
|
|
PetInfo models.Pet `json:"petInfo"`
|
|
PetBaseInfo models.PetBaseInfo `json:"petBaseInfo"`
|
|
}
|
|
|
|
func GetUserPets(uId int64) []UserPetInfo {
|
|
var userPetList []models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("uid = ?", uId).Find(&userPetList)
|
|
var userPets []UserPetInfo
|
|
if len(userPetList) > 0 {
|
|
for _, pet := range userPetList {
|
|
userPets = append(userPets, UserPetInfo{pet, PetBaseInfoMap[pet.PetId]})
|
|
}
|
|
}
|
|
return userPets
|
|
}
|
|
|
|
func GetUserPet(uId int64, pId int64) UserPetInfo {
|
|
var userPet models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("uid = ? and id = ?", uId, pId).Find(&userPet)
|
|
return UserPetInfo{userPet, PetBaseInfoMap[userPet.PetId]}
|
|
}
|
|
|
|
type PetListResponse struct {
|
|
UserPets []UserPetInfo `json:"userPets"`
|
|
}
|
|
|
|
// 宠物列表
|
|
func (p DefParty) petList() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petList", func(ctx *context.Context) {
|
|
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
|
userPets := GetUserPets(headerBaseInfo.Uid)
|
|
Success(ctx, headerBaseInfo, PetListResponse{
|
|
userPets,
|
|
})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type PetInfoRequest struct {
|
|
Id int64 `json:"id"`
|
|
}
|
|
|
|
type PetInfoResponse struct {
|
|
UserPet UserPetInfo `json:"userPet"`
|
|
}
|
|
|
|
func (p DefParty) petInfo() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petInfo", func(ctx *context.Context) {
|
|
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
var petInfoRequest PetInfoRequest
|
|
json.Unmarshal(body, &petInfoRequest)
|
|
userPet := GetUserPet(headerBaseInfo.Uid, petInfoRequest.Id)
|
|
Success(ctx, petInfoRequest, PetInfoResponse{userPet})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type PetAddOrEditRequest struct {
|
|
Id int64 //Id 不为0表示修改
|
|
NickName string //宠物昵称
|
|
HeadImgType int //头像类型 0远程头像 1本地头像
|
|
HeadImgUrl string //宠物头像
|
|
Desc string //宠物描述
|
|
Weight int //宠物体重
|
|
Precaution string //注意事项
|
|
Gender int //性别 0男 1女
|
|
Birthday string //生日
|
|
PetId int //宠物类型
|
|
Eunuch int //是否绝育 0否 1是 2未知
|
|
}
|
|
|
|
type PetAddOrEditResponse struct {
|
|
UserPets []UserPetInfo `json:"userPets"`
|
|
}
|
|
|
|
// 宠物添加或编辑
|
|
func (p DefParty) petAddOrEdit() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petAddOrEdit", func(ctx *context.Context) {
|
|
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
var petAddOrEditRequest PetAddOrEditRequest
|
|
json.Unmarshal(body, &petAddOrEditRequest)
|
|
if PetBaseInfoMap[petAddOrEditRequest.PetId].Id == 0 {
|
|
PetBaseNotExistError.Fail(ctx, petAddOrEditRequest)
|
|
return
|
|
}
|
|
pet := models.Pet{
|
|
Uid: headerBaseInfo.Uid,
|
|
NickName: petAddOrEditRequest.NickName,
|
|
HeadImgType: petAddOrEditRequest.HeadImgType,
|
|
HeadImgUrl: petAddOrEditRequest.HeadImgUrl,
|
|
Desc: petAddOrEditRequest.Desc,
|
|
Weight: petAddOrEditRequest.Weight,
|
|
Precaution: petAddOrEditRequest.Precaution,
|
|
Gender: petAddOrEditRequest.Gender,
|
|
Birthday: petAddOrEditRequest.Birthday,
|
|
PetId: petAddOrEditRequest.PetId,
|
|
Eunuch: petAddOrEditRequest.Eunuch,
|
|
}
|
|
if petAddOrEditRequest.Id == 0 {
|
|
database.Instance().Model(&models.Pet{}).Create(&pet)
|
|
} else {
|
|
pet.Id = petAddOrEditRequest.Id
|
|
var userPetInfo models.Pet
|
|
database.Instance().Model(&models.Pet{}).Where("id = ? and uid = ?", pet.Id, headerBaseInfo.Uid).Find(&userPetInfo)
|
|
if userPetInfo.Id == 0 {
|
|
PetNotExistError.Fail(ctx, petAddOrEditRequest)
|
|
return
|
|
}
|
|
updateValues := map[string]interface{}{
|
|
"NickName": pet.NickName,
|
|
"HeadImgType": pet.HeadImgType,
|
|
"HeadImgUrl": pet.HeadImgUrl,
|
|
"Desc": pet.Desc,
|
|
"Weight": pet.Weight,
|
|
"Precaution": pet.Precaution,
|
|
"Gender": pet.Gender,
|
|
"Birthday": pet.Birthday,
|
|
"PetId": pet.PetId,
|
|
"Eunuch": pet.Eunuch,
|
|
}
|
|
database.Instance().Model(&pet).Updates(&updateValues)
|
|
}
|
|
userPets := GetUserPets(headerBaseInfo.Uid)
|
|
Success(ctx, petAddOrEditRequest, PetAddOrEditResponse{userPets})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type PetTypeListResponse struct {
|
|
PetBaseInfoList []models.PetBaseInfo `json:"petBaseInfoList"`
|
|
CatWeights map[int]string `json:"catWeights"`
|
|
DogWeights map[int]string `json:"dogWeights"`
|
|
}
|
|
|
|
// 宠物基础信息列表
|
|
func (p DefParty) petTypeList() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(PetBase+"/petTypeList", func(ctx *context.Context) {
|
|
var petBaseInfoList []models.PetBaseInfo
|
|
for _, petBaseInfo := range PetBaseInfoMap {
|
|
petBaseInfoList = append(petBaseInfoList, petBaseInfo)
|
|
}
|
|
|
|
Success(ctx, nil, PetTypeListResponse{petBaseInfoList, CatWeightMap, DogWeightMap})
|
|
})
|
|
}}
|
|
}
|
|
|
|
type DelPetRequest struct {
|
|
Id int64
|
|
}
|
|
|
|
func (p DefParty) delPet() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, 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)
|
|
})
|
|
}}
|
|
}
|