User.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package controllers
  2. import (
  3. "bzd_server/conf"
  4. "bzd_server/lib"
  5. "bzd_server/models/Account"
  6. "bzd_server/models/System"
  7. beego "github.com/beego/beego/v2/server/web"
  8. "math"
  9. )
  10. type UserController struct {
  11. beego.Controller
  12. }
  13. // 获取-
  14. func (c *UserController) Get() {
  15. // 验证登录 User_is, User_r
  16. _,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  17. if !User_is {
  18. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  19. c.ServeJSON()
  20. return
  21. }
  22. Id, err := c.GetInt("Id")
  23. if err != nil {
  24. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  25. c.ServeJSON()
  26. return
  27. }
  28. r, is := Account.Read_User_ById(Id);
  29. if !is {
  30. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  31. c.ServeJSON()
  32. return
  33. }
  34. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!",Data: r}
  35. c.ServeJSON()
  36. return
  37. }
  38. // 列表 -
  39. func (c *UserController) List() {
  40. // 验证登录 User_is, User_r
  41. _,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  42. if !User_is {
  43. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  44. c.ServeJSON()
  45. return
  46. }
  47. var r_jsons lib.R_JSONS
  48. page, _ := c.GetInt("page")
  49. if page < 1 {
  50. page = 1
  51. }
  52. page_z, _ := c.GetInt("page_z")
  53. if page_z < 1 {
  54. page_z = conf.Page_size
  55. }
  56. T_name := c.GetString("T_name")
  57. var cnt int64
  58. List, cnt := Account.Read_User_List(T_name, page, page_z)
  59. page_size := math.Ceil(float64(cnt) / float64(page_z))
  60. r_jsons.List = List
  61. r_jsons.Page = page
  62. r_jsons.Page_size = int(page_size)
  63. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  64. r_jsons.Num = int(cnt)
  65. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  66. c.ServeJSON()
  67. return
  68. }
  69. // 添加-
  70. func (c *UserController) Add() {
  71. // 验证登录 User_is, User_r
  72. user_r,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  73. if !User_is {
  74. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  75. c.ServeJSON()
  76. return
  77. }
  78. T_name := c.GetString("T_name")
  79. T_user := c.GetString("T_user")
  80. T_pass := c.GetString("T_pass")
  81. T_passstr := c.GetString("T_passstr")
  82. var_ := Account.User{
  83. T_user: T_user,
  84. T_name: T_name,
  85. T_pass: T_pass,
  86. T_passstr: T_passstr,
  87. T_State: 1,
  88. }
  89. Id, is := Account.Add_User(var_)
  90. if !is {
  91. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  92. c.ServeJSON()
  93. return
  94. }
  95. System.Add_UserLogs_T(user_r.T_uuid,"用户管理", "添加", var_)
  96. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!",Data: Id}
  97. c.ServeJSON()
  98. return
  99. }
  100. // 修改-
  101. func (c *UserController) Up() {
  102. // 验证登录 User_is, User_r
  103. user_r,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  104. if !User_is {
  105. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  106. c.ServeJSON()
  107. return
  108. }
  109. T_name := c.GetString("T_name")
  110. T_pass := c.GetString("T_pass")
  111. T_passstr := c.GetString("T_passstr")
  112. T_State,T_State_err := c.GetInt("T_State")
  113. T_uuid := c.GetString("T_uuid")
  114. r, is := Account.Read_User(T_uuid);
  115. if !is {
  116. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  117. c.ServeJSON()
  118. return
  119. }
  120. // .......
  121. if len(T_name) > 0{
  122. r.T_name = T_name
  123. }
  124. if len(T_pass) > 0{
  125. r.T_pass = T_pass
  126. }
  127. if len(T_passstr) > 0{
  128. r.T_passstr = T_passstr
  129. }
  130. if T_State_err == nil {
  131. r.T_State = T_State
  132. }
  133. // .......
  134. if !Account.Update_User(r,"T_name","T_pass","T_passstr","T_State") {
  135. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  136. c.ServeJSON()
  137. return
  138. }
  139. System.Add_UserLogs_T(user_r.T_uuid,"用户管理", "修改", r)
  140. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  141. c.ServeJSON()
  142. return
  143. }
  144. // 删除-
  145. func (c *UserController) Del() {
  146. // 验证登录 User_is, User_r
  147. user_r,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  148. if !User_is {
  149. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  150. c.ServeJSON()
  151. return
  152. }
  153. T_uuid := c.GetString("T_uuid")
  154. if r, is := Account.Read_User(T_uuid); is {
  155. if !Account.Delete_User(r) {
  156. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  157. c.ServeJSON()
  158. return
  159. }
  160. System.Add_UserLogs_T(user_r.T_uuid,"用户管理", "删除", r)
  161. }
  162. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  163. c.ServeJSON()
  164. return
  165. }