Initial commit

This commit is contained in:
yan.y
2024-03-27 23:25:08 +08:00
commit 0884384e91
127 changed files with 9353 additions and 0 deletions
+110
View File
@@ -0,0 +1,110 @@
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"
"pet-house.com/core/server/web/web_iris"
)
type LoginRequest struct {
Code string
}
type LoginResponse struct {
Token string `json:"token"`
Uid int64 `json:"uid"`
NickName string `json:"nickName"`
HeadImgUrl string `json:"headImgUrl"`
Amount int `json:"amount"`
Role int `json:"role"`
UserPets []UserPetInfo `json:"userPets"`
}
var defaultNickName = "微信用户"
var defaultHeadImgUrl = "http://" + web.CONFIG.System.Addr + "/static/img/20240327152925.jpg"
// 登录
func (p DefParty) login() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(AuthBase+"/login", func(ctx *context.Context) {
body, _ := io.ReadAll(ctx.Request().Body)
var loginRequest LoginRequest
json.Unmarshal(body, &loginRequest)
//获取code
/*info, err := utils.GetWxUserInfo(loginRequest.Code)
if err != nil {
utils.LoginError.DefFail(ctx, loginRequest, err.Error())
return
}
if info.ErrCode != 0 {
utils.LoginError.DefFail(ctx, loginRequest, info.ErrMsg)
return
}*/
var userInfo models.User
database.Instance().Model(&models.User{}).Where("open_id = ? or union_id = ?", "1", "2").Find(&userInfo)
if userInfo.Id == 0 {
newUser := models.User{
NickName: defaultNickName,
HeadImgUrl: defaultHeadImgUrl,
Amount: 0,
OpenId: NextId.Generate().String(),
UnionId: NextId.Generate().String(),
UserType: 0,
Mobile: "",
Role: 0,
}
database.Instance().Model(&models.User{}).Create(&newUser)
userInfo = newUser
}
token := genToken(userInfo.Id)
response := LoginResponse{
Token: token,
Uid: userInfo.Id,
NickName: userInfo.NickName,
HeadImgUrl: userInfo.HeadImgUrl,
Amount: userInfo.Amount,
Role: userInfo.Role,
}
response.UserPets = GetUserPets(userInfo.Id)
Success(ctx, loginRequest, response)
})
}}
}
type GetUserInfoResponse struct {
Uid int64 `json:"uid"`
NickName string `json:"nickName"`
HeadImgUrl string `json:"headImgUrl"`
Amount int `json:"amount"`
Role int `json:"role"`
UserPets []UserPetInfo `json:"userPets"`
}
// 获取用户信息
func (p DefParty) getUserInfo() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(AuthBase+"/getUserInfo", func(ctx *context.Context) {
headerInfo := GetHeaderBaseInfo(ctx)
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerInfo.Uid).Find(&userInfo)
if userInfo == nil || userInfo.Id == 0 {
UserNotExistError.Fail(ctx, nil)
return
}
getUserInfoResponse := GetUserInfoResponse{
Uid: userInfo.Id,
NickName: userInfo.NickName,
HeadImgUrl: userInfo.HeadImgUrl,
Amount: userInfo.Amount,
Role: userInfo.Role,
UserPets: GetUserPets(userInfo.Id),
}
Success(ctx, headerInfo, getUserInfoResponse)
})
}}
}
+119
View File
@@ -0,0 +1,119 @@
package api
import (
"crypto/md5"
"encoding/hex"
"github.com/bwmarrin/snowflake"
"github.com/kataras/iris/v12/context"
"go.uber.org/zap"
"pet-house.com/business/models"
"pet-house.com/core/server/cache"
"pet-house.com/core/server/database"
"pet-house.com/core/server/zap_server"
"strconv"
"time"
)
type Response struct {
Code int `json:"code"` //code
Msg string `json:"msg"` //msg
Data any `json:"data"` //数据
}
type ResponseData struct {
Data any `json:"data"` //数据
}
type Error struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
type DefParty struct {
Perfix string
}
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: "宠物基础信息不存在"}
)
func Success(ctx *context.Context, request any, data any) {
response := Response{200, "success", data}
zap_server.ZAPLOG.Info(ctx.Path(), zap.Any("request", request), zap.Any("response", response))
write(ctx, response)
}
func (error *Error) Fail(ctx *context.Context, request any) {
zap_server.ZAPLOG.Info(ctx.Path(), zap.Any("request", request), zap.Any("error", error), zap.Any("err", error.Msg))
write(ctx, Error{error.Code, error.Msg})
}
func (error *Error) DefFail(ctx *context.Context, request any, err any) {
zap_server.ZAPLOG.Info(ctx.Path(), zap.Any("request", request), zap.Any("error", error), zap.Any("err", err))
write(ctx, Error{error.Code, err.(string)})
}
func write(ctx *context.Context, resp any) {
ctx.JSON(resp)
}
var NextId, _ = snowflake.NewNode(1)
func genToken(uid int64) string {
uToken, err := cache.GetCacheString("u_uid_token:" + strconv.FormatInt(uid, 10))
if err == nil && len(uToken) > 0 {
return uToken
}
sId := NextId.Generate()
hash := md5.Sum(sId.Bytes())
token := hex.EncodeToString(hash[:])
cache.SetCache("u_token:"+token, uid, time.Hour*24*30)
cache.SetCache("u_uid_token:"+strconv.FormatInt(uid, 10), token, time.Hour*24*30)
return token
}
func GetTokenInfo(token string) string {
uId, _ := cache.GetCacheString("u_token:" + token)
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
}
func GetHeaderBaseInfo(ctx *context.Context) HeaderBaseInfo {
token := ctx.GetHeader("X-Token")
uid := ctx.GetHeader("X-U-Id")
_uid, _ := strconv.ParseInt(uid, 10, 64)
return HeaderBaseInfo{
token, _uid,
}
}
type FrontExclude struct {
path string
}
var PetBaseInfoMap map[int]models.PetBaseInfo
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))
}
+28
View File
@@ -0,0 +1,28 @@
package api
import "pet-house.com/core/server/web/web_iris"
var AuthBase = "/auth"
var GoodsBase = "/goods"
var OrderBase = "/order"
var ServiceBase = "/service"
var PetBase = "/pet"
func (p DefParty) RegisterList() []web_iris.Party {
ps := []web_iris.Party{
p.login(),
p.getUserInfo(),
p.goodsList(),
p.goodsDetail(),
p.orderCreate(),
p.orderPay(),
p.petList(),
p.petInfo(),
p.petTypeList(),
p.petAddOrEdit(),
p.serviceAddrList(),
p.serviceAddOrEdit(),
p.index(),
}
return ps
}
+25
View File
@@ -0,0 +1,25 @@
package api
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"pet-house.com/core/server/web/web_iris"
)
// 商品列表
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) {
})
}}
}
// 商品详情
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) {
})
}}
}
+77
View File
@@ -0,0 +1,77 @@
package api
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"go.uber.org/zap"
"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"
"strings"
)
var Root = "/pet-house"
var ExcludeBase = "/static"
var ExcludeBase1 = "/debug"
var frontExcludes = [...]string{
Root + AuthBase + "/login",
}
func ModuleInit() {
utils.WechatInit()
_ = database.Instance().AutoMigrate(
&models.User{},
&models.Pet{},
&models.PetBaseInfo{},
&models.ServiceAddr{},
&models.UserServiceAddr{},
&models.Goods{},
&models.SystemConfig{},
&models.OrderMain{},
&models.OrderSub{},
&models.OrderDetail{})
}
func FrontAuth(ctx *context.Context) {
if strings.Contains(ctx.Path(), ExcludeBase) {
ctx.Next()
return
}
if strings.Contains(ctx.Path(), ExcludeBase1) {
ctx.Next()
return
}
frontExcludesStr := strings.Join(frontExcludes[:], ",")
if strings.Contains(frontExcludesStr, ctx.Path()) {
ctx.Next()
return
}
token := ctx.GetHeader("X-Token")
uid := ctx.GetHeader("X-U-Id")
zap_server.ZAPLOG.Info("frontAuth", zap.Any("path", ctx.Path()), zap.Any("token", token), zap.Any("uid", uid))
if len(token) == 0 || len(uid) == 0 {
IllegalError.Fail(ctx, nil)
return
}
tokenToUid := GetTokenInfo(token)
if len(tokenToUid) == 0 {
TokenError.Fail(ctx, nil)
return
}
if uid != tokenToUid {
UserError.Fail(ctx, nil)
return
}
ctx.Next()
}
func (p DefParty) index() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Get("/", func(c *context.Context) {
c.WriteString("successful")
})
}}
}
+25
View File
@@ -0,0 +1,25 @@
package api
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"pet-house.com/core/server/web/web_iris"
)
// 创建
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) {
})
}}
}
// 支付
func (p DefParty) orderPay() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderPay", func(ctx *context.Context) {
})
}}
}
+159
View File
@@ -0,0 +1,159 @@
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"
)
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{Perfix: p.Perfix, 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
}
type PetInfoResponse struct {
UserPet UserPetInfo `json:"userPet"`
}
func (p DefParty) petInfo() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, 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 //宠物描述
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{Perfix: p.Perfix, 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,
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,
"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"`
}
// 宠物基础信息列表
func (p DefParty) petTypeList() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, 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})
})
}}
}
+25
View File
@@ -0,0 +1,25 @@
package api
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"pet-house.com/core/server/web/web_iris"
)
// 服务地址列表
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) {
})
}}
}
// 服务地址添加或编辑
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) {
})
}}
}