Example #1
0
//修改密码
func LoginChangePasswd(w http.ResponseWriter, r *http.Request) {
	data := make(map[string]interface{})
	user := new(models.User)
	t, err := template.ParseFiles("views/login_passwd_change.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	l, err := template.ParseFiles("views/login.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	if r.Method == "GET" {
		emailCode := r.FormValue("emailCode")
		user.Email_change = emailCode
		has, _, _ := models.SelectUser(user)
		if has {
			data["emailCode"] = emailCode
			data["user"] = user
			data["error"] = ""
			t.Execute(w, data)
			return
		} else {
			data["error"] = "密码修改失败,请重试"
			l.Execute(w, data)
			return
		}

	} else {
		pwd := r.PostFormValue("pwd")
		user.Email_change = r.PostFormValue("emailCode")
		has, userInfo, _ := models.SelectUser(user)
		if has {
			id := userInfo.Id
			user = new(models.User)
			user.Passwd = GetMd5String(pwd)
			user.Email_change = GetMd5String(userInfo.Uname + userInfo.Email + userInfo.Passwd)
			err := models.UpdateUser(id, user)
			if CheckError("密码修改失败", err) {
				data["error"] = "密码修改失败,请重试"
				l.Execute(w, data)
				return
			} else {
				data["success"] = "密码修改成功,请登录"
				l.Execute(w, data)
				return
			}
		} else {
			data["error"] = "密码修改失败,请重试"
			l.Execute(w, data)
			return
		}
	}
}
Example #2
0
//发送邮件
func SendToMail(toEmail, name string) error {
	b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")

	email, _ := conf.Cfg.GetValue("email", "email")
	password, _ := conf.Cfg.GetValue("email", "passwd")
	host, _ := conf.Cfg.GetValue("email", "host")
	subject, _ := conf.Cfg.GetValue("email", "subject")
	sendName, _ := conf.Cfg.GetValue("email", "sendName")

	from := mail.Address{sendName, email}
	to := mail.Address{name, toEmail}

	hp := strings.Split(host, ":")
	auth := smtp.PlainAuth("", email, password, hp[0])

	header := make(map[string]string)
	header["From"] = from.String()
	header["To"] = to.String()
	header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject)))
	header["MIME-Version"] = "1.0"
	header["Content-Type"] = "text/html; charset=UTF-8"
	header["Content-Transfer-Encoding"] = "base64"

	//获取用户信息
	userInfo, _, _ := models.GetUserInfo(name)
	EmailMd5 := GetMd5String(toEmail + name + userInfo.Passwd)
	newUser := new(models.User)
	newUser.Email_change = EmailMd5
	err := models.UpdateUser(userInfo.Id, newUser)
	if CheckError("数据库读取失败", err) {
		return err
	}
	body := `<strong>修改密码</strong>
	<p>访问以下网址:<a href="http://` + conf.HttpWeb + `/login/changePasswd?emailCode=` + EmailMd5 + `">http://` + conf.HttpWeb + `/login/changePasswd?emailCode=` + EmailMd5 + `<a><p>
	<p>如果以上链接无法访问,请将该网址复制并粘贴至新的浏览器窗口中。</p>`

	message := ""
	for k, v := range header {
		message += fmt.Sprintf("%s: %s\r\n", k, v)
	}
	message += "\r\n" + b64.EncodeToString([]byte(body))

	send_to := strings.Split(toEmail, ";")
	err = smtp.SendMail(host, auth, email, send_to, []byte(message))
	return err
}
Example #3
0
//发送邮件,找回密码
func LoginForget(w http.ResponseWriter, r *http.Request) {
	data := make(map[string]interface{})
	user := new(models.User)
	t, err := template.ParseFiles("views/login_forget.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	l, err := template.ParseFiles("views/login.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	if r.Method == "GET" {
		data["user"] = user
		data["error"] = ""
		t.Execute(w, data)
		return
	} else {
		user.Uname = r.PostFormValue("uname")
		user.Email = r.PostFormValue("email")
		s := models.GetUserNameAndEmail(user.Uname, user.Email)
		if len(s) > 0 {
			data["error"] = s
			data["user"] = user
			t.Execute(w, data)
			return
		}

		//发送邮件
		err = SendToMail(user.Email, user.Uname)
		if CheckError("邮件发送失败:", err) {
			data["error"] = "邮件发送失败,稍后再试"
			data["user"] = user
			t.Execute(w, data)
			return
		}
		data["success"] = "请登录邮箱,修改密码"
		l.Execute(w, data)
		return
	}
}
Example #4
0
//注册
func LoginInfo(w http.ResponseWriter, r *http.Request) {
	data := make(map[string]interface{})
	user := new(models.User)
	t, err := template.ParseFiles("views/login_info.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	l, err := template.ParseFiles("views/login.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	if r.Method == "GET" {
		data["user"] = user
		data["error"] = ""
		t.Execute(w, data)
		return
	} else {
		user.Uname = r.PostFormValue("uname")
		user.Email = r.PostFormValue("email")
		user.Passwd = GetMd5String(r.PostFormValue("pwd"))
		user.Status = 1
		user.Subtime = time.Now()
		s := models.GetUserNameOrEmail(user.Uname, user.Email)
		if len(s) > 0 {
			data["error"] = s
			data["user"] = user
			t.Execute(w, data)
			return
		}
		err = models.InsertUser(user)
		if CheckError("数据库连接失败:", err) {
			data["user"] = user
			data["error"] = "网站连接失败,请稍后重试"
			t.Execute(w, data)
			return
		}
		data["success"] = "注册成功,请登录"
		l.Execute(w, data)
		return
	}

}