package main
import (
"fmt"
"html/template"
"io"
"net/http"
"path/filepath"
"strings"
"redisdog/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 == "" {
resp.Write(ERROR_RET(1, "旧密码不能为空!"))
} else if len(oldpwd) < 6 {
resp.Write(ERROR_RET(2, "旧密码格式不正确空!"))
} else if newpwd == "" {
resp.Write(ERROR_RET(3, "新密码不能为空!"))
} else if len(newpwd) < 6 {
resp.Write(ERROR_RET(4, "旧密码格式不正确空!"))
} else if oldpwd == newpwd {
resp.Write(ERROR_RET(5, "新密码不能与旧密码相同!"))
} else {
mdlAccounts := model.NewAccounts(Db)
row, err := mdlAccounts.Login(sess.Account.Account, oldpwd)
if err != nil {
resp.Write(ERROR_RET(6, "旧密码不正确!"))
} else {
affected, err := mdlAccounts.UpdatePassword(row.Id, newpwd)
if err != nil {
resp.Write(ERROR_RET(7, err.Error()))
} else if affected > 0 {
resp.Write(ERROR_RET(0, ""))
} else {
resp.Write(ERROR_RET(7, "未知错误!"))
}
}
}
}
}