controller.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package user
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/astaxie/beego/logs"
  6. "nginx-ui/server/base"
  7. "nginx-ui/server/models"
  8. "nginx-ui/server/modules/settings"
  9. "nginx-ui/server/vo"
  10. )
  11. type Controller struct {
  12. base.Controller
  13. service *Service
  14. }
  15. func NewUserController() *Controller {
  16. return &Controller{
  17. service: NewUserService(),
  18. }
  19. }
  20. // Login 登录
  21. func (c *Controller) Login() {
  22. var user models.User
  23. err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
  24. if err != nil {
  25. logs.Error(err, string(c.Ctx.Input.RequestBody))
  26. c.ErrorJson(err)
  27. return
  28. }
  29. resp := c.service.Login(&user)
  30. if resp.Success() {
  31. c.SetSession("user", user)
  32. }
  33. c.PostJson(resp)
  34. }
  35. // Logout 退出登录
  36. func (c *Controller) Logout() {
  37. user := c.RequiredUser()
  38. if user != nil {
  39. c.DelSession("user")
  40. }
  41. c.Json()
  42. }
  43. func (c *Controller) User() {
  44. user := c.RequiredUser()
  45. if user == nil {
  46. return
  47. }
  48. routes, _ := settings.Route.GetUserRoutes(user)
  49. if routes != nil {
  50. user.Routes = routes
  51. } else {
  52. user.Routes = []models.SettingRoute{}
  53. }
  54. c.SetData(user).Json()
  55. }
  56. // Register 用户注册
  57. func (c *Controller) Register() {
  58. resp := c.service.SignUp(c.Ctx.Input.RequestBody)
  59. c.PostJson(resp)
  60. }
  61. // Users 获取全部用户信息
  62. func (c *Controller) Users() {
  63. user := c.RequiredUser()
  64. if user == nil {
  65. return
  66. }
  67. req := vo.PageReq{}
  68. if !c.ReadBody(&req) {
  69. return
  70. }
  71. resp, err := c.service.Users(&req)
  72. if err != nil {
  73. logs.Warn("Users get fail: %v", err)
  74. c.ErrorJson(err)
  75. return
  76. }
  77. c.SetData(resp).Json()
  78. }
  79. // Update 获取全部用户信息
  80. func (c *Controller) Update() {
  81. user := c.RequiredUser()
  82. if user == nil {
  83. return
  84. }
  85. req := models.User{}
  86. if !c.ReadBody(&req) {
  87. return
  88. }
  89. resp, err := c.service.Update(&req)
  90. if err != nil {
  91. logs.Warn("Users get fail: %v", err)
  92. c.ErrorJson(err)
  93. return
  94. }
  95. c.SetData(resp).Json()
  96. }
  97. // Update 获取全部用户信息
  98. func (c *Controller) UpdateUserInfo() {
  99. user := c.RequiredUser()
  100. if user == nil {
  101. return
  102. }
  103. req := models.User{}
  104. if !c.ReadBody(&req) {
  105. return
  106. }
  107. if req.Id != user.Id {
  108. c.ErrorJson(errors.New("更新失败,无操作权限!"))
  109. return
  110. }
  111. resp, err := c.service.Update(&req)
  112. if err != nil {
  113. logs.Warn("Users get fail: %v", err)
  114. c.ErrorJson(err)
  115. return
  116. }
  117. c.SetSession("user", *resp)
  118. c.SetData(resp).Json()
  119. }
  120. // UpdatePassword Update 获取全部用户信息
  121. func (c *Controller) UpdatePassword() {
  122. user := c.RequiredUser()
  123. if user == nil {
  124. return
  125. }
  126. req := vo.UserUpdatePassword{}
  127. if !c.ReadBody(&req) {
  128. return
  129. }
  130. req.Id = user.Id
  131. err := c.service.UpdatePassword(&req)
  132. if err != nil {
  133. logs.Warn("Users get fail: %v", err)
  134. c.ErrorJson(err)
  135. return
  136. }
  137. c.SetMsg("密码更新成功").Json()
  138. }
  139. // ResetPassword 重置密码
  140. func (c *Controller) ResetPassword() {
  141. req := ResetPasswordReq{}
  142. if !c.ReadBody(&req) {
  143. return
  144. }
  145. err := c.service.ResetPassword(&req)
  146. if err != nil {
  147. logs.Warn("Users get fail: %v", err)
  148. c.ErrorJson(err)
  149. return
  150. }
  151. if req.AuthCode == "" {
  152. c.SetMsg("邮件发送成功!").Json()
  153. } else {
  154. c.SetMsg("密码修改成功!").Json()
  155. }
  156. }
  157. // GetDetail 获取全部用户信息
  158. func (c *Controller) GetDetail() {
  159. user := c.RequiredUser()
  160. if user == nil {
  161. return
  162. }
  163. id, err := c.GetIntQuery("id")
  164. if err != nil {
  165. logs.Warn("Users get fail: %v", err)
  166. c.ErrorJson(err)
  167. return
  168. }
  169. query, err := c.service.GetDetail(id)
  170. if err != nil {
  171. logs.Warn("Users get fail: %v", err)
  172. c.ErrorJson(err)
  173. return
  174. }
  175. c.SetData(query).Json()
  176. }