CommonsController.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package controllers
  2. import (
  3. "ColdP_server/controllers/lib"
  4. "ColdP_server/models/Account"
  5. "fmt"
  6. beego "github.com/beego/beego/v2/server/web"
  7. "net/http"
  8. "strings"
  9. "time"
  10. )
  11. type AdminController struct {
  12. beego.Controller
  13. }
  14. func (c *AdminController) Login() {
  15. c.TplName = "login.html"
  16. }
  17. func (c *AdminController) Login_verification() {
  18. Admin_user := c.GetString("bzd_username")
  19. Admin_pass := c.GetString("bzd_password")
  20. companyId := c.GetString("company_id")
  21. println("Login_verification", Admin_user, Admin_pass)
  22. err, admin_r := Account.Read_AdminLogin_verification(Admin_user, Admin_pass)
  23. if err != nil {
  24. c.Data["json"] = lib.JSONS{Code: 202, Msg: "账号密码错误!"}
  25. c.ServeJSON()
  26. return
  27. }
  28. if admin_r.T_pids == "*" {
  29. User_tokey := Account.Add_Tokey_Set(admin_r.T_uuid, companyId)
  30. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  31. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}
  32. c.ServeJSON()
  33. return
  34. }
  35. //如果自定义了公司ID,则标识是管理员,判断是否pids是否存在要操作的pid,并且要求登录用户的PID一定为0
  36. if companyId != "" && admin_r.T_pid == 0 {
  37. pids := strings.Split(admin_r.T_pids, "|")
  38. for _, v := range pids {
  39. newV := strings.Replace(v, "P", "", -1)
  40. if newV == companyId {
  41. User_tokey := Account.Add_Tokey_Set(admin_r.T_uuid, companyId)
  42. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  43. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}
  44. c.ServeJSON()
  45. return
  46. }
  47. }
  48. //不存在该公司的Pid
  49. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有操作该公司的权限!"}
  50. c.ServeJSON()
  51. return
  52. } else {
  53. User_tokey := Account.Add_Tokey_Set(admin_r.T_uuid, fmt.Sprintf("%d", admin_r.T_pid))
  54. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  55. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}
  56. c.ServeJSON()
  57. return
  58. }
  59. }
  60. func (c *AdminController) Info() {
  61. // 验证登录
  62. b_, user_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  63. if !b_ {
  64. c.Data["json"] = lib.JSONS{Code: 202, Msg: "no"}
  65. c.ServeJSON()
  66. return
  67. }
  68. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok", Data: Account.AdminToAdmin_R(user_r)}
  69. c.ServeJSON()
  70. return
  71. }
  72. func (c *AdminController) Index() {
  73. // 验证登录
  74. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  75. if !b_ {
  76. c.Ctx.Redirect(302, "Login")
  77. return
  78. }
  79. // 基本信息
  80. c.Data["Admin_name"] = admin_r.T_name
  81. c.Data["Admin_root"] = admin_r.Id
  82. c.TplName = "index.html"
  83. }
  84. func (c *AdminController) Home() {
  85. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  86. if !b_ {
  87. fmt.Println("当前未登录,请先登录!")
  88. c.Ctx.Redirect(http.StatusFound, "Login")
  89. }
  90. c.Data["T_name"] = admin_r.T_name
  91. c.TplName = "home.html"
  92. }