http_syscfg_warn.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "redisdog/model"
  10. )
  11. type SysCfgWarnGetRet struct {
  12. Errno int `json:"errno"`
  13. Error string `json:"error"`
  14. Data map[string]string `json:"data"`
  15. }
  16. func syscfg_warn(resp http.ResponseWriter, req *http.Request) {
  17. sess, ok := checkLogin(resp, req)
  18. if !ok {
  19. return
  20. }
  21. if !sess.Account.IsSuper {
  22. resp.Header().Set("Location", "/index/forbidden")
  23. resp.WriteHeader(302)
  24. return
  25. }
  26. //视图输出
  27. files := []string{
  28. filepath.Join(Cfg.TmplDir, "syscfg", "warn.tmpl"),
  29. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  30. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  31. }
  32. tmpl, err := template.New("warn.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  33. if err != nil {
  34. io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
  35. } else {
  36. tmpl.Execute(resp, struct {
  37. Sess *Session
  38. Req *http.Request
  39. Title string
  40. }{
  41. sess,
  42. req,
  43. "报警配置",
  44. })
  45. }
  46. }
  47. func syscfg_warn_get(resp http.ResponseWriter, req *http.Request) {
  48. sess, ok := checkLogin(resp, req)
  49. if !ok {
  50. return
  51. }
  52. if !sess.Account.IsSuper {
  53. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  54. resp.Write(ret)
  55. return
  56. }
  57. mdl := model.NewSysCfg(Db)
  58. values, err := mdl.GetByKeys([]string{"smtp_host", "smtp_port", "smtp_user", "smtp_pwd", "smtp_sender"})
  59. if err != nil {
  60. ret, _ := json.Marshal(ErrorRet{Errno: 1, Error: err.Error()})
  61. resp.Write(ret)
  62. } else {
  63. ret, _ := json.Marshal(SysCfgWarnGetRet{Errno: 0, Error: "", Data: values})
  64. resp.Write(ret)
  65. }
  66. }
  67. func syscfg_warn_set(resp http.ResponseWriter, req *http.Request) {
  68. sess, ok := checkLogin(resp, req)
  69. if !ok {
  70. return
  71. }
  72. if !sess.Account.IsSuper {
  73. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  74. resp.Write(ret)
  75. return
  76. }
  77. req.ParseForm()
  78. smtpHost := req.PostForm.Get("smtp_host")
  79. if smtpHost == "" {
  80. ret, _ := json.Marshal(ErrorRet{Errno: 1, Error: "SMTP HOST不能为空"})
  81. resp.Write(ret)
  82. return
  83. }
  84. smtpPort := req.PostForm.Get("smtp_port")
  85. if smtpPort == "" {
  86. ret, _ := json.Marshal(ErrorRet{Errno: 2, Error: "SMTP端口不能为空"})
  87. resp.Write(ret)
  88. return
  89. }
  90. smtpUser := req.PostForm.Get("smtp_user")
  91. if smtpUser == "" {
  92. ret, _ := json.Marshal(ErrorRet{Errno: 3, Error: "SMTP用户不能为空"})
  93. resp.Write(ret)
  94. return
  95. }
  96. smtpPwd := req.PostForm.Get("smtp_pwd")
  97. if smtpPwd == "" {
  98. ret, _ := json.Marshal(ErrorRet{Errno: 4, Error: "SMTP密码不能为空"})
  99. resp.Write(ret)
  100. return
  101. }
  102. smtpSender := req.PostForm.Get("smtp_sender")
  103. if smtpSender == "" {
  104. ret, _ := json.Marshal(ErrorRet{Errno: 5, Error: "SMTP署名不能为空"})
  105. resp.Write(ret)
  106. return
  107. }
  108. mdl := model.NewSysCfg(Db)
  109. if _, err := mdl.Set("smtp_host", smtpHost); err != nil {
  110. ret, _ := json.Marshal(ErrorRet{Errno: 6, Error: "操作失败(host)"})
  111. resp.Write(ret)
  112. }
  113. if _, err := mdl.Set("smtp_port", smtpPort); err != nil {
  114. ret, _ := json.Marshal(ErrorRet{Errno: 6, Error: "操作失败(port)"})
  115. resp.Write(ret)
  116. }
  117. if _, err := mdl.Set("smtp_user", smtpUser); err != nil {
  118. ret, _ := json.Marshal(ErrorRet{Errno: 6, Error: "操作失败(user)"})
  119. resp.Write(ret)
  120. }
  121. if _, err := mdl.Set("smtp_pwd", smtpPwd); err != nil {
  122. ret, _ := json.Marshal(ErrorRet{Errno: 6, Error: "操作失败(pwd)"})
  123. resp.Write(ret)
  124. }
  125. if _, err := mdl.Set("smtp_sender", smtpSender); err != nil {
  126. ret, _ := json.Marshal(ErrorRet{Errno: 6, Error: "操作失败(sender)"})
  127. resp.Write(ret)
  128. }
  129. SYSLOG("WARN", fmt.Sprintf("管理员#%d %s %s修改了SMTP配置", sess.Account.Id, sess.Account.Name, sess.Account.Account))
  130. //更新配置缓存
  131. err := SysCfg.Load()
  132. if err != nil {
  133. ret, _ := json.Marshal(ErrorRet{Errno: 7, Error: fmt.Sprintf("配置更新成功,但未能成功刷新:%s", err.Error())})
  134. resp.Write(ret)
  135. } else {
  136. ret, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  137. resp.Write(ret)
  138. }
  139. }