mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-12-20 04:00:05 +08:00
1、增加了登录后重定向至原始请求 URL 的功能;2、格式化了相关文件中的代码。
This commit is contained in:
@@ -1,40 +1,52 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"time"
|
||||
"strings"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"net/smtp"
|
||||
"github.com/lifei6671/mindoc/conf"
|
||||
"github.com/lifei6671/mindoc/models"
|
||||
"github.com/lifei6671/mindoc/utils"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/lifei6671/gocaptcha"
|
||||
"strconv"
|
||||
"github.com/lifei6671/mindoc/conf"
|
||||
"github.com/lifei6671/mindoc/models"
|
||||
"github.com/lifei6671/mindoc/utils"
|
||||
)
|
||||
|
||||
// AccountController 用户登录与注册.
|
||||
// AccountController 用户登录与注册
|
||||
type AccountController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
// Login 用户登录.
|
||||
// Login 用户登录
|
||||
func (c *AccountController) Login() {
|
||||
c.Prepare()
|
||||
c.TplName = "account/login.tpl"
|
||||
|
||||
var remember struct { MemberId int ; Account string; Time time.Time}
|
||||
var remember struct {
|
||||
MemberId int
|
||||
Account string
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
// 显式指定的 URL 参数优先;为了统一处理,将之更新到 Session 中
|
||||
turl := c.GetString("turl", "")
|
||||
if turl != "" {
|
||||
c.SetSession("turl", turl)
|
||||
}
|
||||
|
||||
beego.Info("AccountController.Login(): turl is: " + turl)
|
||||
|
||||
// 如果 Cookie 中存在登录信息
|
||||
if cookie, ok := c.GetSecureCookie(conf.GetAppKey(), "login"); ok {
|
||||
|
||||
if err := utils.Decode(cookie, &remember); err == nil {
|
||||
if member, err := models.NewMember().Find(remember.MemberId); err == nil {
|
||||
c.SetMember(*member)
|
||||
|
||||
c.Redirect(beego.URLFor("HomeController.Index"), 302)
|
||||
c.LoggedIn(false)
|
||||
c.StopRun()
|
||||
}
|
||||
}
|
||||
@@ -48,14 +60,13 @@ func (c *AccountController) Login() {
|
||||
|
||||
// 如果开启了验证码
|
||||
if v, ok := c.Option["ENABLED_CAPTCHA"]; ok && strings.EqualFold(v, "true") {
|
||||
v,ok := c.GetSession(conf.CaptchaSessionName).(string);
|
||||
v, ok := c.GetSession(conf.CaptchaSessionName).(string)
|
||||
if !ok || !strings.EqualFold(v, captcha) {
|
||||
c.JsonResult(6001, "验证码不正确")
|
||||
}
|
||||
}
|
||||
member,err := models.NewMember().Login(account,password)
|
||||
|
||||
//如果没有数据
|
||||
member, err := models.NewMember().Login(account, password)
|
||||
if err == nil {
|
||||
member.LastLoginTime = time.Now()
|
||||
member.Update()
|
||||
@@ -69,20 +80,45 @@ func (c *AccountController) Login() {
|
||||
if err == nil {
|
||||
c.SetSecureCookie(conf.GetAppKey(), "login", v)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
c.JsonResult(0,"ok")
|
||||
data := c.LoggedIn(true)
|
||||
c.JsonResult(0, "ok", data)
|
||||
} else {
|
||||
logs.Error("用户登录 =>", err)
|
||||
c.JsonResult(500, "账号或密码错误", nil)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//用户注册.
|
||||
// 登录成功后的操作,如重定向到原始请求页面
|
||||
func (c *AccountController) LoggedIn(isPost bool) interface{} {
|
||||
turl := ""
|
||||
value := c.GetSession("turl")
|
||||
if value != nil {
|
||||
turl = value.(string)
|
||||
}
|
||||
c.DelSession("turl")
|
||||
|
||||
beego.Info("AccountController.LoggedIn(): turl is: " + turl)
|
||||
|
||||
if !isPost {
|
||||
// 检查是否存在 turl 参数,如果有则重定向至 turl 处,否则进入 Home 页面
|
||||
if turl == "" {
|
||||
turl = beego.URLFor("HomeController.Index")
|
||||
}
|
||||
c.Redirect(turl, 302)
|
||||
return nil
|
||||
} else {
|
||||
var data struct {
|
||||
TURL string `json:"turl"`
|
||||
}
|
||||
data.TURL = turl
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
func (c *AccountController) Register() {
|
||||
c.TplName = "account/register.tpl"
|
||||
|
||||
@@ -112,7 +148,7 @@ func (c *AccountController) Register() {
|
||||
}
|
||||
// 如果开启了验证码
|
||||
if v, ok := c.Option["ENABLED_CAPTCHA"]; ok && strings.EqualFold(v, "true") {
|
||||
v,ok := c.GetSession(conf.CaptchaSessionName).(string);
|
||||
v, ok := c.GetSession(conf.CaptchaSessionName).(string)
|
||||
if !ok || !strings.EqualFold(v, captcha) {
|
||||
c.JsonResult(6001, "验证码不正确")
|
||||
}
|
||||
@@ -140,7 +176,7 @@ func (c *AccountController) Register() {
|
||||
}
|
||||
}
|
||||
|
||||
//找回密码.
|
||||
// 找回密码
|
||||
func (c *AccountController) FindPassword() {
|
||||
c.TplName = "account/find_password_setp1.tpl"
|
||||
mail_conf := conf.GetMailConfig()
|
||||
@@ -159,7 +195,7 @@ func (c *AccountController) FindPassword() {
|
||||
|
||||
// 如果开启了验证码
|
||||
if v, ok := c.Option["ENABLED_CAPTCHA"]; ok && strings.EqualFold(v, "true") {
|
||||
v,ok := c.GetSession(conf.CaptchaSessionName).(string);
|
||||
v, ok := c.GetSession(conf.CaptchaSessionName).(string)
|
||||
if !ok || !strings.EqualFold(v, captcha) {
|
||||
c.JsonResult(6001, "验证码不正确")
|
||||
}
|
||||
@@ -215,7 +251,7 @@ func (c *AccountController) FindPassword() {
|
||||
mail_conf.SmtpHost,
|
||||
)
|
||||
|
||||
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n";
|
||||
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
|
||||
subject := "Subject: 找回密码!\n"
|
||||
|
||||
err = smtp.SendMail(
|
||||
@@ -230,7 +266,6 @@ func (c *AccountController) FindPassword() {
|
||||
}
|
||||
}(mail_conf, email, body)
|
||||
|
||||
|
||||
c.JsonResult(0, "ok", c.BaseUrl()+beego.URLFor("AccountController.Login"))
|
||||
}
|
||||
|
||||
@@ -260,7 +295,7 @@ func (c *AccountController) FindPassword() {
|
||||
}
|
||||
}
|
||||
|
||||
//校验邮件并修改密码.
|
||||
// 校验邮件并修改密码
|
||||
func (c *AccountController) ValidEmail() {
|
||||
c.Prepare()
|
||||
password1 := c.GetString("password1")
|
||||
@@ -284,7 +319,7 @@ func (c *AccountController) ValidEmail() {
|
||||
if captcha == "" {
|
||||
c.JsonResult(6004, "验证码不能为空")
|
||||
}
|
||||
v,ok := c.GetSession(conf.CaptchaSessionName).(string);
|
||||
v, ok := c.GetSession(conf.CaptchaSessionName).(string)
|
||||
if !ok || !strings.EqualFold(v, captcha) {
|
||||
c.JsonResult(6001, "验证码不正确")
|
||||
}
|
||||
@@ -307,7 +342,7 @@ func (c *AccountController) ValidEmail() {
|
||||
beego.Error(err)
|
||||
c.JsonResult(6005, "用户不存在")
|
||||
}
|
||||
hash ,err := utils.PasswordHash(password1);
|
||||
hash, err := utils.PasswordHash(password1)
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
@@ -328,17 +363,16 @@ func (c *AccountController) ValidEmail() {
|
||||
c.JsonResult(0, "ok", c.BaseUrl()+beego.URLFor("AccountController.Login"))
|
||||
}
|
||||
|
||||
|
||||
// Logout 退出登录.
|
||||
// Logout 退出登录
|
||||
func (c *AccountController) Logout() {
|
||||
c.SetMember(models.Member{});
|
||||
c.SetMember(models.Member{})
|
||||
|
||||
c.SetSecureCookie(conf.GetAppKey(), "login", "", -3600)
|
||||
|
||||
c.Redirect(beego.URLFor("AccountController.Login"), 302)
|
||||
}
|
||||
|
||||
//验证码.
|
||||
// 验证码
|
||||
func (c *AccountController) Captcha() {
|
||||
c.Prepare()
|
||||
|
||||
@@ -361,7 +395,6 @@ func (c *AccountController) Captcha() {
|
||||
captchaImage.DrawBorder(gocaptcha.ColorToRGB(0x17A7A7A))
|
||||
// captchaImage.DrawHollowLine()
|
||||
|
||||
|
||||
captchaImage.SaveImage(c.Ctx.ResponseWriter, gocaptcha.ImageFormatJpeg)
|
||||
c.StopRun()
|
||||
}
|
||||
@@ -99,6 +99,11 @@ func isUserLoggedIn(c *DocumentController) bool {
|
||||
}
|
||||
|
||||
func promptUserToLogIn(c *DocumentController) {
|
||||
beego.Info("Access " + c.Ctx.Request.URL.RequestURI() + " not permitted.")
|
||||
beego.Info(" Access will be redirected to login page(SessionId: " + c.CruSession.SessionID() + ").")
|
||||
|
||||
c.SetSession("turl", c.Ctx.Request.URL.RequestURI())
|
||||
|
||||
if c.IsAjax() {
|
||||
c.JsonResult(6000, "需要[重]登录。")
|
||||
} else {
|
||||
|
||||
@@ -93,7 +93,7 @@
|
||||
<script type="text/javascript">
|
||||
$(function () {
|
||||
$("#account,#passwd,#code").on('focus', function () {
|
||||
$(this).tooltip('destroy').parents('.form-group').removeClass('has-error');;
|
||||
$(this).tooltip('destroy').parents('.form-group').removeClass('has-error');
|
||||
});
|
||||
|
||||
$(document).keydown(function (e) {
|
||||
@@ -102,19 +102,20 @@
|
||||
$("#btn-login").click();
|
||||
}
|
||||
});
|
||||
|
||||
$("#btn-login").on('click', function () {
|
||||
var $btn = $(this).button('loading');
|
||||
|
||||
var account = $.trim($("#account").val());
|
||||
var password = $.trim($("#password").val());
|
||||
var code = $("#code").val();
|
||||
|
||||
if (account === "") {
|
||||
$("#account").tooltip({ placement: "auto", title: "账号不能为空", trigger: 'manual' })
|
||||
.tooltip('show')
|
||||
.parents('.form-group').addClass('has-error');
|
||||
$btn.button('reset');
|
||||
return false;
|
||||
|
||||
} else if (password === "") {
|
||||
$("#password").tooltip({ title: '密码不能为空', trigger: 'manual' })
|
||||
.tooltip('show')
|
||||
@@ -134,16 +135,18 @@
|
||||
dataType: "json",
|
||||
type: "POST",
|
||||
success: function (res) {
|
||||
|
||||
if (res.errcode !== 0) {
|
||||
$("#captcha-img").click();
|
||||
$("#code").val('');
|
||||
layer.msg(res.message);
|
||||
$btn.button('reset');
|
||||
} else {
|
||||
window.location = "/";
|
||||
turl = res.data.turl;
|
||||
if (turl === "") {
|
||||
turl = "/";
|
||||
}
|
||||
window.location = turl;
|
||||
}
|
||||
|
||||
},
|
||||
error: function () {
|
||||
$("#captcha-img").click();
|
||||
@@ -154,7 +157,6 @@
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user