123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package user
- import (
- "encoding/json"
- "errors"
- "github.com/astaxie/beego/logs"
- "nginx-ui/server/base"
- "nginx-ui/server/models"
- "nginx-ui/server/vo"
- )
- type Controller struct {
- base.Controller
- service *UserService
- }
- func NewUserController() *Controller {
- return &Controller{
- service: NewUserService(),
- }
- }
- // Login 登录
- func (c *Controller) Login() {
- var user models.User
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
- if err != nil {
- logs.Error(err, string(c.Ctx.Input.RequestBody))
- c.ErrorJson(err)
- return
- }
- resp := c.service.Login(&user)
- if resp.Success() {
- c.SetSession("user", user)
- }
- c.PostJson(resp)
- }
- // Logout 退出登录
- func (c *Controller) Logout() {
- user := c.RequiredUser()
- if user != nil {
- c.DelSession("user")
- }
- c.Json()
- }
- func (c *Controller) User() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- c.SetData(user).Json()
- }
- // Register 用户注册
- func (c *Controller) Register() {
- resp := c.service.SignUp(c.Ctx.Input.RequestBody)
- c.PostJson(resp)
- }
- // Users 获取全部用户信息
- func (c *Controller) Users() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- req := vo.PageReq{}
- if !c.ReadBody(&req) {
- return
- }
- resp, err := c.service.Users(&req)
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- c.SetData(resp).Json()
- }
- // Update 获取全部用户信息
- func (c *Controller) Update() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- req := models.User{}
- if !c.ReadBody(&req) {
- return
- }
- resp, err := c.service.Update(&req)
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- c.SetData(resp).Json()
- }
- // Update 获取全部用户信息
- func (c *Controller) UpdateUserInfo() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- req := models.User{}
- if !c.ReadBody(&req) {
- return
- }
- if req.Id != user.Id {
- c.ErrorJson(errors.New("更新失败,无操作权限!"))
- return
- }
- resp, err := c.service.Update(&req)
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- c.SetSession("user", *resp)
- c.SetData(resp).Json()
- }
- // UpdatePassword Update 获取全部用户信息
- func (c *Controller) UpdatePassword() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- req := vo.UserUpdatePassword{}
- if !c.ReadBody(&req) {
- return
- }
- req.Id = user.Id
- err := c.service.UpdatePassword(&req)
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- c.SetMsg("密码更新成功").Json()
- }
- // GetDetail 获取全部用户信息
- func (c *Controller) GetDetail() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- id, err := c.GetIntQuery("id")
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- query, err := c.service.GetDetail(id)
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- c.SetData(query).Json()
- }
|