202 lines
6.1 KiB
Go
202 lines
6.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/context"
|
|
"go.uber.org/zap"
|
|
"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"
|
|
"pet-house.com/core/server/zap_server"
|
|
)
|
|
|
|
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"`
|
|
Car models.ServiceCar `json:"car"`
|
|
}
|
|
|
|
var defaultNickName = "微信用户"
|
|
var defaultHeadImgUrl = web.CONFIG.System.Domain + "/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(info.Unionid) == 0 {
|
|
info.Unionid = info.Openid
|
|
}
|
|
zap_server.ZAPLOG.Info("codeLogin", zap.Any("info", info))
|
|
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 = ?", info.Openid).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,
|
|
}
|
|
err := database.Instance().Create(&newUser).Error
|
|
zap_server.ZAPLOG.Info("create", zap.Any("err", err))
|
|
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,
|
|
}
|
|
if userInfo.Role == 1 {
|
|
var serviceCarUser models.ServiceCarUser
|
|
database.Instance().Model(&models.ServiceCarUser{}).Where("uid = ?", userInfo.Id).Find(&serviceCarUser)
|
|
response.Car = CarMap[serviceCarUser.CarId]
|
|
response.Car.ServiceAddr = ServiceAddrMap[response.Car.ServiceAddrId]
|
|
}
|
|
|
|
response.UserPets = GetUserPets(userInfo.Id)
|
|
Success(ctx, loginRequest, response)
|
|
})
|
|
},
|
|
}
|
|
}
|
|
|
|
type UpdateUserInfoRequest struct {
|
|
NickName string `json:"nickName"`
|
|
HeadImgUrl string `json:"headImgUrl"`
|
|
}
|
|
|
|
// 更新用户信息
|
|
func (p DefParty) updateUserInfo() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(AuthBase+"/updateUserInfo", func(ctx *context.Context) {
|
|
headerInfo := GetHeaderBaseInfo(ctx)
|
|
var updateUserInfoRequest UpdateUserInfoRequest
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
json.Unmarshal(body, &updateUserInfoRequest)
|
|
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
|
|
}
|
|
if len(updateUserInfoRequest.NickName) > 0 {
|
|
userInfo.NickName = updateUserInfoRequest.NickName
|
|
}
|
|
if len(updateUserInfoRequest.HeadImgUrl) > 0 {
|
|
userInfo.HeadImgUrl = updateUserInfoRequest.HeadImgUrl
|
|
}
|
|
|
|
updateValues := map[string]interface{}{
|
|
"NickName": userInfo.NickName,
|
|
"HeadImgUrl": userInfo.HeadImgUrl,
|
|
}
|
|
database.Instance().Model(&userInfo).Updates(&updateValues)
|
|
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)
|
|
})
|
|
}}
|
|
}
|
|
|
|
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)
|
|
})
|
|
}}
|
|
}
|
|
|
|
type UserUploadFileResponse struct {
|
|
FileName string `json:"fileName"` //文件名
|
|
}
|
|
|
|
// 获取用户信息
|
|
func (p DefParty) userUploadFile() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(AuthBase+"/userUploadFile", func(ctx *context.Context) {
|
|
headerInfo := GetHeaderBaseInfo(ctx)
|
|
fileName := CtxFileUpload(ctx)
|
|
fileName = web.CONFIG.System.Domain + "/static/uploads/" + fileName
|
|
Success(ctx, headerInfo, UserUploadFileResponse{fileName})
|
|
})
|
|
}}
|
|
}
|