69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package user
|
||
|
||
import (
|
||
"fmt"
|
||
"mini_server/internal/dao"
|
||
"mini_server/internal/server/httpreply"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/sirupsen/logrus"
|
||
)
|
||
|
||
type UserInfo struct {
|
||
UserId uint64 `json:"user_id"`
|
||
AvatarId string `json:"avatar_id"`
|
||
Username string `json:"username"`
|
||
CreatedAt string `json:"created_at"`
|
||
Tags string `json:"tags"`
|
||
Persona string `json:"persona"`
|
||
}
|
||
|
||
type QueryURI struct {
|
||
UserId int32 `uri:"user_id" binding:"gt=0"`
|
||
}
|
||
type QueryReply struct {
|
||
UserInfo *UserInfo `json:"user_info"`
|
||
}
|
||
|
||
func Query(c *gin.Context) {
|
||
reply := httpreply.NewDefaultReplyData()
|
||
defer httpreply.Reply(c, reply)
|
||
|
||
var param QueryURI
|
||
if err := c.ShouldBindUri(¶m); err != nil {
|
||
reply.Status = http.StatusInternalServerError
|
||
reply.Message = fmt.Sprintf("invalid uri, err:%v", err)
|
||
return
|
||
}
|
||
|
||
full_lastest_userinfo(reply, param.UserId)
|
||
}
|
||
|
||
func Copy_User_Info(data *dao.User, info *UserInfo) {
|
||
info.UserId = uint64(data.ID)
|
||
info.AvatarId = data.AvatarId
|
||
info.Username = data.Username
|
||
info.CreatedAt = data.CreatedAt.Format(time.DateTime)
|
||
info.Tags = data.Tags
|
||
info.Persona = data.Persona
|
||
}
|
||
|
||
// 查询数据库,并填充最新的用户信息
|
||
func full_lastest_userinfo(reply *httpreply.ReplyData, userId int32) {
|
||
// 查询最新用户信息,并返回
|
||
data, err := dao.User_Query(userId)
|
||
if err != nil || data == nil {
|
||
logrus.Errorf("查询用户最新信息失败,userId:%d,err:%v", userId, err)
|
||
reply.Status = http.StatusInternalServerError
|
||
reply.Message = "查询用户信息失败"
|
||
return
|
||
}
|
||
var queryReply QueryReply
|
||
var info UserInfo
|
||
Copy_User_Info(data, &info)
|
||
queryReply.UserInfo = &info
|
||
reply.Data = queryReply
|
||
}
|