http_profile.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "io"
  6. "net/http"
  7. "path/filepath"
  8. "strings"
  9. "redisdog/model"
  10. )
  11. func profile_logout(resp http.ResponseWriter, req *http.Request) {
  12. sess, ok := checkLogin(resp, req)
  13. if ok {
  14. SessPoll.Del(sess.Sess)
  15. }
  16. resp.Header().Set("Location", "/login")
  17. resp.WriteHeader(302)
  18. }
  19. func profile_passwd(resp http.ResponseWriter, req *http.Request) {
  20. sess, ok := checkLogin(resp, req)
  21. if !ok {
  22. return
  23. }
  24. if req.Method == "GET" {
  25. //视图输出
  26. files := []string{
  27. filepath.Join(Cfg.TmplDir, "profile", "passwd.tmpl"),
  28. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  29. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  30. }
  31. tmpl, err := template.New("passwd.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  32. if err != nil {
  33. io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
  34. } else {
  35. tmpl.Execute(resp, struct {
  36. Sess *Session
  37. Req *http.Request
  38. Title string
  39. }{
  40. sess,
  41. req,
  42. "修改密码",
  43. })
  44. }
  45. } else {
  46. req.ParseForm()
  47. oldpwd := strings.TrimSpace(req.PostForm.Get("oldpwd"))
  48. newpwd := strings.TrimSpace(req.PostForm.Get("newpwd"))
  49. if oldpwd == "" {
  50. resp.Write(ERROR_RET(1, "旧密码不能为空!"))
  51. } else if len(oldpwd) < 6 {
  52. resp.Write(ERROR_RET(2, "旧密码格式不正确空!"))
  53. } else if newpwd == "" {
  54. resp.Write(ERROR_RET(3, "新密码不能为空!"))
  55. } else if len(newpwd) < 6 {
  56. resp.Write(ERROR_RET(4, "旧密码格式不正确空!"))
  57. } else if oldpwd == newpwd {
  58. resp.Write(ERROR_RET(5, "新密码不能与旧密码相同!"))
  59. } else {
  60. mdlAccounts := model.NewAccounts(Db)
  61. row, err := mdlAccounts.Login(sess.Account.Account, oldpwd)
  62. if err != nil {
  63. resp.Write(ERROR_RET(6, "旧密码不正确!"))
  64. } else {
  65. affected, err := mdlAccounts.UpdatePassword(row.Id, newpwd)
  66. if err != nil {
  67. resp.Write(ERROR_RET(7, err.Error()))
  68. } else if affected > 0 {
  69. resp.Write(ERROR_RET(0, ""))
  70. } else {
  71. resp.Write(ERROR_RET(7, "未知错误!"))
  72. }
  73. }
  74. }
  75. }
  76. }