app.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package service
  2. import (
  3. "github.com/go-xweb/xweb"
  4. "go.mongodb.org/mongo-driver/bson"
  5. qu "qfw/util"
  6. "strings"
  7. "time"
  8. "util"
  9. )
  10. type App struct {
  11. *xweb.Action
  12. appList xweb.Mapper `xweb:"/service/app/monitor/list"`
  13. appCreate xweb.Mapper `xweb:"/service/app/monitor/create"`
  14. appEdit xweb.Mapper `xweb:"/service/app/monitor/edit"`
  15. appDel xweb.Mapper `xweb:"/service/app/monitor/del"`
  16. }
  17. func (app *App) AppList() {
  18. if app.Method() == "POST" {
  19. identity := app.GetString("identity")
  20. start, _ := app.GetInteger("start")
  21. limit, _ := app.GetInteger("length")
  22. draw, _ := app.GetInteger("draw")
  23. q := bson.M{"name": bson.M{"$regex": identity}}
  24. q["del"] = false
  25. count := util.Mgo.Count("app_list", q)
  26. info, _ := util.Mgo.Find("app_list", q, nil, nil, false, start, limit)
  27. if len(*info) > 0 {
  28. app.ServeJson(map[string]interface{}{
  29. "draw": draw,
  30. "rep": true,
  31. "data": *info,
  32. "recordsFiltered": count,
  33. "recordsTotal": count,
  34. })
  35. } else {
  36. app.ServeJson(map[string]interface{}{
  37. "rep": false,
  38. "msg": "未查询到数据",
  39. })
  40. }
  41. } else {
  42. _ = app.Render("app/app_list.html")
  43. }
  44. }
  45. func (app *App) AppCreate() {
  46. if app.Method() == "POST" {
  47. data := util.GetPostForm(app.Request)
  48. pg := *qu.ObjToMap(data["data"])
  49. if name := qu.ObjToString(pg["name"]); name != "" {
  50. now := time.Now().Unix()
  51. ip_port := strings.Split(qu.ObjToString(pg["ip_port"]), ":")
  52. leader := qu.ObjToString(pg["leader"])
  53. mail := qu.ObjToString(pg["mail"])
  54. timing := qu.ObjToString(pg["timing"])
  55. save := bson.M{"name": name, "ip": ip_port[0], "port": qu.IntAll(ip_port[1]), "leader": leader, "mail": mail, "timing": timing}
  56. save["del"] = false
  57. save["updatetime"] = now
  58. save["status"] = 1
  59. util.Mgo.Save("app_list", save)
  60. app.ServeJson(map[string]interface{}{
  61. "rep": true,
  62. })
  63. } else {
  64. app.ServeJson(map[string]interface{}{
  65. "rep": false,
  66. })
  67. }
  68. }
  69. }
  70. func (app *App) AppEdit() {
  71. if app.Method() == "POST" {
  72. data := util.GetPostForm(app.Request)
  73. pg := *qu.ObjToMap(data["data"])
  74. id := qu.ObjToString(data["id"])
  75. if name := qu.ObjToString(pg["name"]); name != "" {
  76. now := time.Now().Unix()
  77. ip_port := strings.Split(qu.ObjToString(pg["ip_port"]), ":")
  78. leader := qu.ObjToString(pg["leader"])
  79. mail := qu.ObjToString(pg["mail"])
  80. timing := qu.ObjToString(pg["timing"])
  81. status := qu.IntAll(pg["status"])
  82. save := bson.M{"name": name, "ip": ip_port[0], "port": qu.IntAll(ip_port[1]), "leader": leader, "mail": mail, "timing": timing}
  83. save["updatetime"] = now
  84. save["status"] = status
  85. util.Mgo.UpdateById("app_list", id, bson.M{"$set": save})
  86. app.ServeJson(map[string]interface{}{
  87. "rep": true,
  88. })
  89. } else {
  90. app.ServeJson(map[string]interface{}{
  91. "rep": false,
  92. })
  93. }
  94. }
  95. }
  96. func (app *App) AppDel() {
  97. if app.Method() == "POST" {
  98. id := app.GetString("id")
  99. b := util.Mgo.UpdateById("app_list", id, bson.M{"$set": bson.M{"del": true}})
  100. app.ServeJson(map[string]interface{}{
  101. "rep": b,
  102. })
  103. }
  104. }