user.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package controllers
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/astaxie/beego"
  6. "box-gm/models"
  7. "box-gm/utils"
  8. )
  9. type UserController struct {
  10. beego.Controller
  11. }
  12. func TimeToStr(t int64) string {
  13. return time.Unix(t, 0).Format("2006-01-02 15:04:05")
  14. }
  15. func StrToTime(str string) int64 {
  16. loc, _ := time.LoadLocation("Local")
  17. theTime, err := time.ParseInLocation("2006-01-02 15:04:05", str, loc)
  18. if err == nil {
  19. return theTime.Unix()
  20. } else {
  21. return 0
  22. }
  23. }
  24. func (c *UserController) Get() {
  25. userinfo := c.GetSession("user")
  26. if userinfo == nil {
  27. c.Ctx.Redirect(302, "/accountlogin")
  28. return
  29. }
  30. c.Data["username"] = userinfo.(*LoginInfo).Username
  31. c.Data["token"] = models.GetToken()
  32. userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
  33. if (userPermission & (1 << uint(models.ModelUser))) == 0 {
  34. c.Ctx.Redirect(302, "/index")
  35. return
  36. }
  37. users, _ := models.GetUserlist()
  38. permissions, _ := models.GetRolelist()
  39. c.Data["users"] = users
  40. c.Data["permissions"] = permissions
  41. c.Data["token"] = models.GetToken()
  42. c.TplName = "user.tpl"
  43. }
  44. func (c *UserController) Post() {
  45. if !models.CheckToken(c.GetString("token")) {
  46. c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败,请重新刷新界面"}
  47. c.ServeJSON()
  48. return
  49. }
  50. userinfo := c.GetSession("user")
  51. if userinfo == nil {
  52. c.Ctx.Redirect(302, "/accountlogin")
  53. return
  54. }
  55. if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
  56. c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
  57. c.ServeJSON()
  58. return
  59. }
  60. username := c.GetString("username")
  61. user := models.GetUserByUsername(username)
  62. if user != nil {
  63. c.Data["json"] = &map[string]interface{}{"status": false, "info": "用户已存在"}
  64. c.ServeJSON()
  65. return
  66. }
  67. u := new(models.User)
  68. u.Username = c.GetString("username")
  69. permission, _ := c.GetInt64("permission")
  70. u.Role = permission
  71. u.Password = utils.Strtomd5(c.GetString("password"))
  72. u.CreateTs = time.Now().Unix()
  73. u.LastLoginTs = time.Now().Unix()
  74. _, err := models.AddUser(u)
  75. if err != nil {
  76. c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
  77. c.ServeJSON()
  78. return
  79. }
  80. c.Data["json"] = &map[string]interface{}{"status": true, "info": "创建用户成功"}
  81. c.ServeJSON()
  82. go models.SaveLog(username, "创建用户", &map[string]interface{}{"username": u.Username, "permission": permission})
  83. }
  84. func (c *UserController) Put() {
  85. if !models.CheckToken(c.GetString("token")) {
  86. c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败,请重新刷新界面"}
  87. c.ServeJSON()
  88. return
  89. }
  90. userinfo := c.GetSession("user")
  91. if userinfo == nil {
  92. c.Ctx.Redirect(302, "/accountlogin")
  93. return
  94. }
  95. if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
  96. c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
  97. c.ServeJSON()
  98. return
  99. }
  100. id, _ := c.GetInt64("id")
  101. user := models.GetUserById(id)
  102. if user == nil {
  103. c.Data["json"] = &map[string]interface{}{"status": false, "info": "用户不存在"}
  104. c.ServeJSON()
  105. return
  106. }
  107. username := c.GetString("username")
  108. nUser := models.GetUserByUsername(username)
  109. if nUser != nil && nUser.Id != id {
  110. c.Data["json"] = &map[string]interface{}{"status": false, "info": fmt.Sprintf("用户[%s]已存在", username)}
  111. c.ServeJSON()
  112. return
  113. }
  114. password := c.GetString("password")
  115. if password != user.Password {
  116. password = utils.Strtomd5(password)
  117. }
  118. u := new(models.User)
  119. u.Id = user.Id
  120. u.Username = c.GetString("username")
  121. permission, _ := c.GetInt64("permission")
  122. u.Role = permission
  123. u.Password = password
  124. _, err := models.UpdateUser(u)
  125. if err != nil {
  126. c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
  127. c.ServeJSON()
  128. return
  129. }
  130. c.Data["json"] = &map[string]interface{}{"status": true, "info": "更新用户成功"}
  131. c.ServeJSON()
  132. go models.SaveLog(userinfo.(*LoginInfo).Username, "修改用户", &map[string]interface{}{"username": u.Username, "permission": permission})
  133. }
  134. func (c *UserController) Delete() {
  135. if !models.CheckToken(c.GetString("token")) {
  136. c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败,请重新刷新界面"}
  137. c.ServeJSON()
  138. return
  139. }
  140. userinfo := c.GetSession("user")
  141. if userinfo == nil {
  142. c.Ctx.Redirect(302, "/accountlogin")
  143. return
  144. }
  145. if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
  146. c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
  147. c.ServeJSON()
  148. return
  149. }
  150. id, _ := c.GetInt64("id")
  151. user := models.GetUserById(id)
  152. if user == nil {
  153. c.Data["json"] = &map[string]interface{}{"status": false, "info": fmt.Sprintf("用户[%d]不存在", id)}
  154. c.ServeJSON()
  155. return
  156. }
  157. models.DelUserById(id)
  158. c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除用户成功"}
  159. c.ServeJSON()
  160. go models.SaveLog(userinfo.(*LoginInfo).Username, "删除用户", &map[string]interface{}{"usernames": user.Username})
  161. }