120 lines
3.2 KiB
Go
120 lines
3.2 KiB
Go
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))
|
|
}
|