package main import ( "encoding/json" "fmt" "html/template" "io" "net/http" "path/filepath" "strings" "cnphper.com/model" ) func profile_logout(resp http.ResponseWriter, req *http.Request) { sess, ok := checkLogin(resp, req) if ok { SessPoll.Del(sess.Sess) } resp.Header().Set("Location", "/login") resp.WriteHeader(302) } func profile_passwd(resp http.ResponseWriter, req *http.Request) { sess, ok := checkLogin(resp, req) if !ok { return } if req.Method == "GET" { //视图输出 files := []string{ filepath.Join(Cfg.TmplDir, "profile", "passwd.tmpl"), filepath.Join(Cfg.TmplDir, "header.tmpl"), filepath.Join(Cfg.TmplDir, "navbar.tmpl"), } tmpl, err := template.New("passwd.tmpl").Funcs(TmplFuncMap).ParseFiles(files...) if err != nil { io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error())) } else { tmpl.Execute(resp, struct { Sess *Session Req *http.Request Title string }{ sess, req, "修改密码", }) } } else { req.ParseForm() oldpwd := strings.TrimSpace(req.PostForm.Get("oldpwd")) newpwd := strings.TrimSpace(req.PostForm.Get("newpwd")) if oldpwd == "" { json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "旧密码不能为空!"}) resp.Write(json) } else if len(oldpwd) < 6 { json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "旧密码格式不正确空!"}) resp.Write(json) } else if newpwd == "" { json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "新密码不能为空!"}) resp.Write(json) } else if len(newpwd) < 6 { json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "旧密码格式不正确空!"}) resp.Write(json) } else if oldpwd == newpwd { json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "新密码不能与旧密码相同!"}) resp.Write(json) } else { mdlAccounts := model.NewAccounts(Db) row, err := mdlAccounts.Login(sess.Account.Account, oldpwd) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 6, Error: "旧密码不正确!"}) resp.Write(json) } affected, err := mdlAccounts.UpdatePassword(row.Id, newpwd) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 7, Error: err.Error()}) resp.Write(json) } if affected > 0 { json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""}) resp.Write(json) } else { json, _ := json.Marshal(ErrorRet{Errno: 8, Error: "未知错误!"}) resp.Write(json) } } } }