2017-04-21 18:20:35 +08:00
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
2017-05-01 12:15:55 +08:00
|
|
|
|
"strings"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
|
|
"github.com/lifei6671/godoc/conf"
|
|
|
|
|
"github.com/lifei6671/godoc/models"
|
|
|
|
|
"github.com/lifei6671/godoc/utils"
|
|
|
|
|
"github.com/astaxie/beego"
|
2017-04-24 18:25:17 +08:00
|
|
|
|
"github.com/astaxie/beego/logs"
|
2017-05-01 12:15:55 +08:00
|
|
|
|
"github.com/lifei6671/gocaptcha"
|
|
|
|
|
|
|
|
|
|
"regexp"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AccountController 用户登录与注册.
|
|
|
|
|
type AccountController struct {
|
|
|
|
|
BaseController
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Login 用户登录.
|
|
|
|
|
func (c *AccountController) Login() {
|
|
|
|
|
c.Prepare()
|
|
|
|
|
|
|
|
|
|
var remember struct { MemberId int ; Account string; Time time.Time}
|
|
|
|
|
|
|
|
|
|
//如果Cookie中存在登录信息
|
|
|
|
|
if cookie,ok := c.GetSecureCookie(conf.GetAppKey(),"login");ok{
|
|
|
|
|
|
|
|
|
|
if err := utils.Decode(cookie,&remember); err == nil {
|
|
|
|
|
member := models.NewMember()
|
|
|
|
|
member.MemberId = remember.MemberId
|
|
|
|
|
|
|
|
|
|
if err := models.NewMember().Find(remember.MemberId); err == nil {
|
|
|
|
|
c.SetMember(*member)
|
|
|
|
|
|
|
|
|
|
c.Redirect(beego.URLFor("HomeController.Index"), 302)
|
|
|
|
|
c.StopRun()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.Ctx.Input.IsPost() {
|
2017-04-24 18:25:17 +08:00
|
|
|
|
account := c.GetString("account")
|
|
|
|
|
password := c.GetString("password")
|
2017-05-01 12:15:55 +08:00
|
|
|
|
captcha := c.GetString("code")
|
|
|
|
|
is_remember := c.GetString("is_remember")
|
|
|
|
|
|
|
|
|
|
//如果开启了验证码
|
|
|
|
|
if v,ok := c.Option["ENABLED_CAPTCHA"]; ok && strings.EqualFold(v,"true") {
|
|
|
|
|
v,ok := c.GetSession(conf.CaptchaSessionName).(string);
|
|
|
|
|
if !ok || !strings.EqualFold(v,captcha){
|
|
|
|
|
c.JsonResult(6001,"验证码不正确")
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
member,err := models.NewMember().Login(account,password)
|
|
|
|
|
|
|
|
|
|
//如果没有数据
|
|
|
|
|
if err == nil {
|
|
|
|
|
c.SetMember(*member)
|
2017-05-01 12:15:55 +08:00
|
|
|
|
if strings.EqualFold(is_remember,"yes") {
|
|
|
|
|
remember.MemberId = member.MemberId
|
|
|
|
|
remember.Account = member.Account
|
|
|
|
|
remember.Time = time.Now()
|
|
|
|
|
v ,err := utils.Encode(remember)
|
|
|
|
|
if err == nil {
|
|
|
|
|
c.SetSecureCookie(conf.GetAppKey(),"login",v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
c.JsonResult(0,"ok")
|
|
|
|
|
}else{
|
2017-04-24 18:25:17 +08:00
|
|
|
|
logs.Error("用户登录 =>",err)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
c.JsonResult(500,"账号或密码错误",nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
c.Layout = ""
|
|
|
|
|
c.TplName = "account/login.tpl"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 12:15:55 +08:00
|
|
|
|
func (c *AccountController) Register() {
|
|
|
|
|
c.TplName = "account/register.tpl"
|
|
|
|
|
|
|
|
|
|
//如果没有开启用户注册
|
|
|
|
|
if v,ok := c.Option["ENABLED_REGISTER"]; ok && !strings.EqualFold(v,"true") {
|
|
|
|
|
c.Abort("404")
|
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
2017-05-01 12:15:55 +08:00
|
|
|
|
if c.Ctx.Input.IsPost() {
|
|
|
|
|
account := c.GetString("account")
|
|
|
|
|
password1 := c.GetString("password1")
|
|
|
|
|
password2 := c.GetString("password2")
|
|
|
|
|
email := c.GetString("email")
|
|
|
|
|
captcha := c.GetString("code")
|
|
|
|
|
|
|
|
|
|
if ok,err := regexp.MatchString(conf.RegexpAccount,account); account == "" || !ok || err != nil {
|
|
|
|
|
c.JsonResult(6001,"账号只能由英文字母数字组成,且在3-50个字符")
|
|
|
|
|
}
|
|
|
|
|
if l := strings.Count(password1,"") ; password1 == "" || l > 50 || l < 6{
|
|
|
|
|
c.JsonResult(6002,"密码必须在6-50个字符之间")
|
|
|
|
|
}
|
|
|
|
|
if password1 != password2 {
|
|
|
|
|
c.JsonResult(6003,"确认密码不正确")
|
|
|
|
|
}
|
|
|
|
|
if ok,err := regexp.MatchString(conf.RegexpEmail,email); !ok || err != nil || email == "" {
|
|
|
|
|
c.JsonResult(6004,"邮箱格式不正确")
|
|
|
|
|
}
|
|
|
|
|
//如果开启了验证码
|
|
|
|
|
if v,ok := c.Option["ENABLED_CAPTCHA"]; ok && strings.EqualFold(v,"true") {
|
|
|
|
|
v,ok := c.GetSession(conf.CaptchaSessionName).(string);
|
|
|
|
|
if !ok || !strings.EqualFold(v,captcha){
|
|
|
|
|
c.JsonResult(6001,"验证码不正确")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
member := models.NewMember()
|
|
|
|
|
|
|
|
|
|
if _,err := member.FindByAccount(account); err == nil && member.MemberId > 0 {
|
|
|
|
|
c.JsonResult(6005,"账号已存在")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
member.Account = account
|
|
|
|
|
member.Password = password1
|
|
|
|
|
member.Role = conf.MemberGeneralRole
|
|
|
|
|
member.Avatar = conf.GetDefaultAvatar()
|
|
|
|
|
member.CreateAt = 0
|
|
|
|
|
member.Email = email
|
|
|
|
|
member.Status = 0
|
|
|
|
|
if err := member.Add(); err != nil {
|
|
|
|
|
beego.Error(err)
|
|
|
|
|
c.JsonResult(6006,"注册失败,请联系系统管理员处理")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JsonResult(0,"ok",member)
|
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *AccountController) FindPassword() {
|
|
|
|
|
p.TplName = "account/find_password.tpl"
|
|
|
|
|
}
|
|
|
|
|
// Logout 退出登录.
|
|
|
|
|
func (c *AccountController) Logout(){
|
|
|
|
|
c.SetMember(models.Member{});
|
|
|
|
|
|
|
|
|
|
c.Redirect(beego.URLFor("AccountController.Login"),302)
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-30 22:13:12 +08:00
|
|
|
|
func (c *AccountController) Captcha() {
|
2017-05-01 12:15:55 +08:00
|
|
|
|
c.Prepare()
|
|
|
|
|
|
|
|
|
|
captchaImage, err := gocaptcha.NewCaptchaImage(140, 40, gocaptcha.RandLightColor())
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
beego.Error(err)
|
|
|
|
|
c.Abort("500")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
captchaImage.DrawNoise(gocaptcha.CaptchaComplexLower)
|
|
|
|
|
|
|
|
|
|
//captchaImage.DrawTextNoise(gocaptcha.CaptchaComplexHigh)
|
|
|
|
|
txt := gocaptcha.RandText(4)
|
|
|
|
|
|
|
|
|
|
c.SetSession(conf.CaptchaSessionName,txt)
|
|
|
|
|
|
|
|
|
|
captchaImage.DrawText(txt)
|
|
|
|
|
//captchaImage.Drawline(3);
|
|
|
|
|
captchaImage.DrawBorder(gocaptcha.ColorToRGB(0x17A7A7A))
|
|
|
|
|
//captchaImage.DrawHollowLine()
|
|
|
|
|
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
2017-05-01 12:15:55 +08:00
|
|
|
|
captchaImage.SaveImage(c.Ctx.ResponseWriter, gocaptcha.ImageFormatJpeg)
|
|
|
|
|
c.StopRun()
|
2017-04-30 22:13:12 +08:00
|
|
|
|
}
|