http_syscfg_warn.go 3.5 KB

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