119 lines
3.4 KiB
Go
119 lines
3.4 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
|
|
}
|
|
|
|
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)
|
|
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
|
|
}
|
|
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)
|
|
})
|
|
}}
|
|
}
|