91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"yintai-api/app/admin/request"
|
|
"yintai-api/app/admin/service"
|
|
"yintai-api/app/common/dao"
|
|
err2 "yintai-api/app/common/err"
|
|
"yintai-api/app/common/response"
|
|
"yintai-api/base/config"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
type User struct{}
|
|
|
|
func (a *User) Create(ctx *gin.Context) {
|
|
var req request.UserCreate
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
err2.ParamErr("<user create>", err)
|
|
}
|
|
|
|
id := new(service.User).Create(req)
|
|
ctx.JSON(http.StatusOK, response.NewRsp(map[string]any{"id": id}))
|
|
}
|
|
|
|
func (a *User) Update(ctx *gin.Context) {
|
|
var req request.UserUpdate
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
err2.ParamErr("<user update>", err)
|
|
}
|
|
// 获取当前登录用户的账号
|
|
userinfo := service.NewToken().UserInfo(ctx.GetHeader("token"))
|
|
if userId := cast.ToInt64(req.UserId); userId != 0 {
|
|
userinfo, err = dao.NewUser().GetUserId(userId)
|
|
if userinfo == nil {
|
|
panic(config.ErrNotExists.New().Append())
|
|
}
|
|
}
|
|
|
|
userService := service.NewUser(userinfo)
|
|
userService.Update(req)
|
|
ctx.JSON(http.StatusOK, response.NewRspOk())
|
|
}
|
|
|
|
func (a *User) Delete(ctx *gin.Context) {
|
|
var req request.UserDelete
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
err2.ParamErr("<user delete>", err)
|
|
}
|
|
|
|
userinfo := service.NewToken().UserInfo(ctx.GetHeader("token"))
|
|
if userinfo.ID == cast.ToInt64(req.ID) {
|
|
err2.PriErr("you can't delete yourself")
|
|
}
|
|
|
|
new(service.User).Delete(req)
|
|
ctx.JSON(http.StatusOK, response.NewRspOk())
|
|
}
|
|
|
|
func (a *User) List(ctx *gin.Context) {
|
|
var req request.UserList
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
err2.ParamErr("<user list>", err)
|
|
}
|
|
res, count := new(service.User).List(req)
|
|
ctx.JSON(http.StatusOK, response.NewListRsp(count, res))
|
|
}
|
|
|
|
func (a *User) Login(ctx *gin.Context) {
|
|
var req request.UserLogin
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
err2.ParamErr("<user login>", err)
|
|
}
|
|
res := new(service.User).Login(req)
|
|
ctx.JSON(http.StatusOK, response.NewRsp(res))
|
|
return
|
|
}
|
|
|
|
func (a *User) Logout(ctx *gin.Context) {
|
|
var req request.UserLogout
|
|
if err := ctx.ShouldBindHeader(&req); err != nil {
|
|
err2.ParamErr("<user logout>", err)
|
|
}
|
|
|
|
new(service.User).Logout(req)
|
|
ctx.JSON(http.StatusOK, response.NewRspOk())
|
|
}
|