profile.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "strings"
  10. "cnphper.com/model"
  11. )
  12. func profile_logout(resp http.ResponseWriter, req *http.Request) {
  13. sess, ok := checkLogin(resp, req)
  14. if ok {
  15. SessPoll.Del(sess.Sess)
  16. }
  17. resp.Header().Set("Location", "/login")
  18. resp.WriteHeader(302)
  19. }
  20. func profile_passwd(resp http.ResponseWriter, req *http.Request) {
  21. sess, ok := checkLogin(resp, req)
  22. if !ok {
  23. return
  24. }
  25. if req.Method == "GET" {
  26. //视图输出
  27. files := []string{
  28. filepath.Join(Cfg.TmplDir, "profile", "passwd.tmpl"),
  29. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  30. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  31. }
  32. tmpl, err := template.New("passwd.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. } else {
  47. req.ParseForm()
  48. oldpwd := strings.TrimSpace(req.PostForm.Get("oldpwd"))
  49. newpwd := strings.TrimSpace(req.PostForm.Get("newpwd"))
  50. if oldpwd == "" {
  51. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "旧密码不能为空!"})
  52. resp.Write(json)
  53. } else if len(oldpwd) < 6 {
  54. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "旧密码格式不正确空!"})
  55. resp.Write(json)
  56. } else if newpwd == "" {
  57. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "新密码不能为空!"})
  58. resp.Write(json)
  59. } else if len(newpwd) < 6 {
  60. json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "旧密码格式不正确空!"})
  61. resp.Write(json)
  62. } else if oldpwd == newpwd {
  63. json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "新密码不能与旧密码相同!"})
  64. resp.Write(json)
  65. } else {
  66. mdlAccounts := model.NewAccounts(Db)
  67. row, err := mdlAccounts.Login(sess.Account.Account, oldpwd)
  68. if err != nil {
  69. json, _ := json.Marshal(ErrorRet{Errno: 6, Error: "旧密码不正确!"})
  70. resp.Write(json)
  71. }
  72. affected, err := mdlAccounts.UpdatePassword(row.Id, newpwd)
  73. if err != nil {
  74. json, _ := json.Marshal(ErrorRet{Errno: 7, Error: err.Error()})
  75. resp.Write(json)
  76. }
  77. if affected > 0 {
  78. json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  79. resp.Write(json)
  80. } else {
  81. json, _ := json.Marshal(ErrorRet{Errno: 8, Error: "未知错误!"})
  82. resp.Write(json)
  83. }
  84. }
  85. }
  86. }