User.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package controllers
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/models/Account"
  6. "ColdVerify_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 := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  17. if !User_is {
  18. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  19. c.ServeJSON()
  20. return
  21. }
  22. Id, err := c.GetInt("Id")
  23. if err != nil {
  24. c.Data["json"] = lib.JSONS{Code: 202, 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: 202, 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 := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  42. if !User_is {
  43. c.Data["json"] = lib.JSONS{Code: 201, 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 := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  73. if !User_is {
  74. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  75. c.ServeJSON()
  76. return
  77. }
  78. //T_power, _ := c.GetInt("T_power")
  79. T_name := c.GetString("T_name")
  80. T_pass := c.GetString("T_pass")
  81. T_passstr := c.GetString("T_passstr")
  82. var_ := Account.User{
  83. //T_power: T_power,
  84. T_name: T_name,
  85. T_pass: T_pass,
  86. T_passstr: T_passstr,
  87. T_Show: 1,
  88. T_State: 1,
  89. }
  90. if _, is := Account.Read_UserByT_name(T_name); is {
  91. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司名称重复!"}
  92. c.ServeJSON()
  93. return
  94. }
  95. Id, is := Account.Add_User(var_)
  96. if !is {
  97. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  98. c.ServeJSON()
  99. return
  100. }
  101. System.Add_UserLogs_T(user_r.T_uuid, "用户管理", "添加", var_)
  102. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  103. c.ServeJSON()
  104. return
  105. }
  106. // 修改-
  107. func (c *UserController) Up() {
  108. // 验证登录 User_is, User_r
  109. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  110. if !User_is {
  111. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  112. c.ServeJSON()
  113. return
  114. }
  115. //T_power, T_power_err := c.GetInt("T_power")
  116. T_name := c.GetString("T_name")
  117. T_pass := c.GetString("T_pass")
  118. T_passstr := c.GetString("T_passstr")
  119. T_Show, T_Show_err := c.GetInt("T_Show")
  120. T_uuid := c.GetString("T_uuid")
  121. r, is := Account.Read_User(T_uuid)
  122. if !is {
  123. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  124. c.ServeJSON()
  125. return
  126. }
  127. if _, is := Account.Read_UserByT_name(T_name); is && r.T_name != T_name {
  128. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司名称重复!"}
  129. c.ServeJSON()
  130. return
  131. }
  132. // .......
  133. //if T_power_err == nil {
  134. // r.T_power = T_power
  135. //}
  136. if len(T_name) > 0 {
  137. r.T_name = T_name
  138. }
  139. if len(T_pass) > 0 {
  140. r.T_pass = T_pass
  141. }
  142. if len(T_passstr) > 0 {
  143. r.T_passstr = T_passstr
  144. }
  145. if T_Show_err == nil {
  146. r.T_Show = T_Show
  147. }
  148. // .......
  149. if !Account.Update_User(r, "T_name", "T_pass", "T_passstr", "T_Show") {
  150. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  151. c.ServeJSON()
  152. return
  153. }
  154. System.Add_UserLogs_T(user_r.T_uuid, "用户管理", "修改", r)
  155. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  156. c.ServeJSON()
  157. return
  158. }
  159. // 删除-
  160. func (c *UserController) Del() {
  161. // 验证登录 User_is, User_r
  162. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  163. if !User_is {
  164. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  165. c.ServeJSON()
  166. return
  167. }
  168. T_uuid := c.GetString("T_uuid")
  169. if r, is := Account.Read_User(T_uuid); is {
  170. if !Account.Delete_User_(r) {
  171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  172. c.ServeJSON()
  173. return
  174. }
  175. System.Add_UserLogs_T(user_r.T_uuid, "用户管理", "删除", r)
  176. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  177. c.ServeJSON()
  178. return
  179. }
  180. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  181. c.ServeJSON()
  182. return
  183. }