Account.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. uuid "github.com/satori/go.uuid"
  9. "math"
  10. "time"
  11. )
  12. type AccountController struct {
  13. beego.Controller
  14. }
  15. func (c *AccountController) Login_html() {
  16. c.TplName = "login.html"
  17. }
  18. // 验证登录
  19. func (c *AccountController) Login_verification() {
  20. Admin_user := c.GetString("bzd_username")
  21. Admin_pass := c.GetString("bzd_password")
  22. println("Login_verification", Admin_user, Admin_pass)
  23. err, user_r := Account.Read_User_verification(Admin_user, Admin_pass)
  24. if err != nil {
  25. c.Data["json"] = lib.JSONS{Code: 202, Msg: "登录失败-请检查账号密码"}
  26. } else {
  27. User_tokey := Account.Add_Tokey(user_r.T_uuid)
  28. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  29. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}
  30. System.Add_UserLogs(user_r.T_uuid, "登陆", "用户登陆", "")
  31. }
  32. c.ServeJSON()
  33. return
  34. }
  35. // 验证登录
  36. func (c *AccountController) Login_Admin_verification() {
  37. Admin_user := c.GetString("bzd_username")
  38. Admin_pass := c.GetString("bzd_password")
  39. println("Login_Admin_verification", Admin_user, Admin_pass)
  40. err, user_r := Account.Read_Admin_verification(Admin_user, Admin_pass)
  41. if err != nil {
  42. c.Data["json"] = lib.JSONS{Code: 202, Msg: "登录失败-请检查账号密码"}
  43. } else {
  44. User_tokey := Account.Add_Tokey(user_r.T_uuid)
  45. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  46. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}
  47. System.Add_UserLogs(user_r.T_uuid, "登陆", "管理员登陆", "")
  48. }
  49. c.ServeJSON()
  50. return
  51. }
  52. // --------------------------------------------------------------------------------------------------------------
  53. // 管理员-列表-
  54. func (c *AccountController) List() {
  55. // 验证登录 User_is, User_r
  56. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  57. if !User_is {
  58. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  59. c.ServeJSON()
  60. return
  61. }
  62. if User_r.T_power > 2 {
  63. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  64. c.ServeJSON()
  65. return
  66. }
  67. var r_jsons lib.R_JSONS
  68. page, _ := c.GetInt("page")
  69. if page < 1 {
  70. page = 1
  71. }
  72. page_z, _ := c.GetInt("page_z")
  73. if page_z < 1 {
  74. page_z = conf.Page_size
  75. }
  76. T_name := c.GetString("T_name")
  77. var cnt int
  78. PowerList := Account.Read_Power_List_ALL_1()
  79. PowerMap := Account.UserPowerListToPowerMap(PowerList)
  80. List, cnt := Account.Read_Admin_List(T_name, page, page_z, PowerMap)
  81. page_size := math.Ceil(float64(cnt) / float64(page_z))
  82. r_jsons.List = List
  83. r_jsons.Page = page
  84. r_jsons.Page_size = int(page_size)
  85. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  86. r_jsons.Num = cnt
  87. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  88. c.ServeJSON()
  89. return
  90. }
  91. // 管理员-添加-
  92. func (c *AccountController) Add() {
  93. // 验证登录 User_is, User_r
  94. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  95. if !User_is {
  96. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  97. c.ServeJSON()
  98. return
  99. }
  100. if User_r.T_power > 2 {
  101. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. T_uuid := uuid.NewV4().String()
  106. T_power, _ := c.GetInt("T_power")
  107. T_name := c.GetString("T_name")
  108. T_user := c.GetString("T_user")
  109. T_pass := c.GetString("T_pass")
  110. var_ := Account.Admin{
  111. T_uuid: T_uuid,
  112. T_power: T_power,
  113. T_name: T_name,
  114. T_user: T_user,
  115. T_pass: T_pass,
  116. T_State: 1,
  117. }
  118. if err, _ := Account.Read_Admin_ByT_user(T_user); err == nil {
  119. c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名已存在!"}
  120. c.ServeJSON()
  121. return
  122. }
  123. Id, err := Account.Add_Admin(var_)
  124. if err != nil {
  125. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  126. c.ServeJSON()
  127. return
  128. }
  129. System.Add_UserLogs_T(User_r.T_uuid, "管理员用户", "添加", var_)
  130. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  131. c.ServeJSON()
  132. return
  133. }
  134. // 管理员-修改-
  135. func (c *AccountController) Up() {
  136. // 验证登录 User_is, User_r
  137. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  138. if !User_is {
  139. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  140. c.ServeJSON()
  141. return
  142. }
  143. if User_r.T_power > 2 {
  144. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  145. c.ServeJSON()
  146. return
  147. }
  148. T_uuid := c.GetString("T_uuid")
  149. T_power, _ := c.GetInt("T_power")
  150. T_name := c.GetString("T_name")
  151. T_user := c.GetString("T_user")
  152. T_pass := c.GetString("T_pass")
  153. err, r := Account.Read_Admin_ByT_uuid(T_uuid)
  154. if err != nil {
  155. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  156. c.ServeJSON()
  157. return
  158. }
  159. err, r2 := Account.Read_Admin_ByT_user(T_user)
  160. if err == nil && T_uuid != r2.T_uuid {
  161. c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名已存在!"}
  162. c.ServeJSON()
  163. return
  164. }
  165. if T_power > 0 {
  166. r.T_power = T_power
  167. }
  168. if len(T_name) > 0 {
  169. r.T_name = T_name
  170. }
  171. if len(T_user) > 0 {
  172. r.T_user = T_user
  173. }
  174. if len(T_pass) > 0 {
  175. r.T_pass = T_pass
  176. }
  177. if !Account.Update_Admin(r, "T_power", "T_name", "T_user", "T_pass") {
  178. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  179. c.ServeJSON()
  180. return
  181. }
  182. System.Add_UserLogs_T(User_r.T_uuid, "管理员用户", "修改", r)
  183. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  184. c.ServeJSON()
  185. return
  186. }
  187. // 管理员-删除-
  188. func (c *AccountController) Del() {
  189. // 验证登录 User_is, User_r
  190. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  191. if !User_is {
  192. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  193. c.ServeJSON()
  194. return
  195. }
  196. if User_r.T_power > 2 {
  197. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  198. c.ServeJSON()
  199. return
  200. }
  201. T_uuid := c.GetString("T_uuid")
  202. if User_r.T_uuid == T_uuid {
  203. c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除自己!"}
  204. c.ServeJSON()
  205. return
  206. }
  207. if err, r := Account.Read_Admin_ByT_uuid(T_uuid); err == nil {
  208. if !Account.Delete_Admin_(r) {
  209. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  210. c.ServeJSON()
  211. return
  212. }
  213. System.Add_UserLogs_T(User_r.T_uuid, "管理员用户", "删除", r)
  214. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  215. c.ServeJSON()
  216. return
  217. }
  218. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. // 用户权限-所有列表-
  223. func (c *AccountController) UserPower_List_All() {
  224. // 验证登录 User_is, User_r
  225. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  226. if !User_is {
  227. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  228. c.ServeJSON()
  229. return
  230. }
  231. if User_r.T_power > 2 {
  232. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  233. c.ServeJSON()
  234. return
  235. }
  236. var r_jsons lib.R_JSONS
  237. r_jsons.List = Account.Read_Power_List_ALL_Admin_Power(User_r.T_power)
  238. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  239. c.ServeJSON()
  240. return
  241. }
  242. // 管理员-全部列表-
  243. func (c *AccountController) List_All() {
  244. // 验证登录 User_is, User_r
  245. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  246. if !User_is {
  247. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  248. c.ServeJSON()
  249. return
  250. }
  251. if User_r.T_power > 2 {
  252. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  253. c.ServeJSON()
  254. return
  255. }
  256. PowerList := Account.Read_Power_List_ALL_1()
  257. PowerMap := Account.UserPowerListToPowerMap(PowerList)
  258. var r_jsons lib.R_JSONS
  259. r_jsons.List = Account.Read_Admin_List_ALL_Power(PowerMap)
  260. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  261. c.ServeJSON()
  262. return
  263. }
  264. // 修改密码
  265. func (c *AccountController) UpPassword() {
  266. // 验证登录 User_is, User_r
  267. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  268. if !User_is {
  269. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  270. c.ServeJSON()
  271. return
  272. }
  273. T_oldpass := c.GetString("T_oldpass")
  274. T_pass := c.GetString("T_pass")
  275. if T_oldpass != user_r.T_pass {
  276. c.Data["json"] = lib.JSONS{Code: 202, Msg: "旧密码错误!"}
  277. c.ServeJSON()
  278. return
  279. }
  280. if len(T_pass) > 0 {
  281. user_r.T_pass = T_pass
  282. }
  283. if !Account.Update_Admin(user_r, "T_pass") {
  284. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  285. c.ServeJSON()
  286. return
  287. }
  288. user_r.T_pass = "********"
  289. System.Add_UserLogs_T(user_r.T_uuid, "管理员管理", "修改密码", user_r)
  290. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  291. c.ServeJSON()
  292. return
  293. }