pet-house/business/api/auth.go

127 lines
3.6 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"
"pet-house.com/core/server/web/web_iris"
)
type LoginRequest struct {
Code string
NickName string
HeadImgUrl 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{
Prefix: p.Prefix,
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)
if len(loginRequest.Code) == 0 {
ParamError.Fail(ctx, nil)
return
}
//获取code
info, err := utils.GetWxUserInfo(loginRequest.Code)
if err != nil {
LoginError.DefFail(ctx, loginRequest, err.Error())
return
}
if info.ErrCode != 0 {
LoginError.DefFail(ctx, loginRequest, info.ErrMsg)
return
}
if len(loginRequest.NickName) == 0 {
loginRequest.NickName = defaultNickName
}
if len(loginRequest.HeadImgUrl) == 0 {
loginRequest.HeadImgUrl = defaultHeadImgUrl
}
var userInfo models.User
database.Instance().Model(&models.User{}).Where("open_id = ? or union_id = ?", info.Openid, info.Unionid).Find(&userInfo)
if userInfo.Id == 0 {
newUser := models.User{
NickName: loginRequest.NickName,
HeadImgUrl: loginRequest.HeadImgUrl,
Amount: 0,
OpenId: info.Openid,
UnionId: info.Unionid,
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{Prefix: p.Prefix, 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)
})
}}
}