mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-09-19 01:58:00 +08:00
1、实现用户注册
2、实现验证码 3、完善文档评论功能
This commit is contained in:
@@ -2,12 +2,16 @@ package controllers
|
||||
|
||||
import (
|
||||
"time"
|
||||
"strings"
|
||||
|
||||
"github.com/lifei6671/godoc/conf"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/lifei6671/godoc/utils"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/lifei6671/gocaptcha"
|
||||
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// AccountController 用户登录与注册.
|
||||
@@ -40,14 +44,32 @@ func (c *AccountController) Login() {
|
||||
if c.Ctx.Input.IsPost() {
|
||||
account := c.GetString("account")
|
||||
password := c.GetString("password")
|
||||
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,"验证码不正确")
|
||||
}
|
||||
}
|
||||
member,err := models.NewMember().Login(account,password)
|
||||
|
||||
//如果没有数据
|
||||
if err == nil {
|
||||
c.SetMember(*member)
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
c.JsonResult(0,"ok")
|
||||
c.StopRun()
|
||||
}else{
|
||||
logs.Error("用户登录 =>",err)
|
||||
c.JsonResult(500,"账号或密码错误",nil)
|
||||
@@ -61,10 +83,61 @@ func (c *AccountController) Login() {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *AccountController) Register() {
|
||||
p.TplName = "account/register.tpl"
|
||||
func (c *AccountController) Register() {
|
||||
c.TplName = "account/register.tpl"
|
||||
|
||||
|
||||
//如果没有开启用户注册
|
||||
if v,ok := c.Option["ENABLED_REGISTER"]; ok && !strings.EqualFold(v,"true") {
|
||||
c.Abort("404")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *AccountController) FindPassword() {
|
||||
@@ -78,5 +151,28 @@ func (c *AccountController) Logout(){
|
||||
}
|
||||
|
||||
func (c *AccountController) Captcha() {
|
||||
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()
|
||||
|
||||
|
||||
captchaImage.SaveImage(c.Ctx.ResponseWriter, gocaptcha.ImageFormatJpeg)
|
||||
c.StopRun()
|
||||
}
|
@@ -414,6 +414,7 @@ func (c *BookController) Create() {
|
||||
book.Version = time.Now().Unix()
|
||||
book.Cover = conf.GetDefaultCover()
|
||||
book.Editor = "markdown"
|
||||
book.Theme = "default"
|
||||
|
||||
err := book.Insert()
|
||||
|
||||
@@ -421,8 +422,11 @@ func (c *BookController) Create() {
|
||||
logs.Error("Insert => ",err)
|
||||
c.JsonResult(6005,"保存项目失败")
|
||||
}
|
||||
bookResult := models.NewBookResult()
|
||||
bookResult.FindByIdentify(book.Identify,c.Member.MemberId)
|
||||
bookResult,err := models.NewBookResult().FindByIdentify(book.Identify,c.Member.MemberId)
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
|
||||
c.JsonResult(0,"ok",bookResult)
|
||||
}
|
||||
|
9
controllers/comment.go
Normal file
9
controllers/comment.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package controllers
|
||||
|
||||
type CommentController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (c *CommentController) Lists() {
|
||||
|
||||
}
|
@@ -21,8 +21,9 @@ type DocumentController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
//判断用户是否可以阅读文档
|
||||
func isReadable (identify,token string,c *DocumentController) *models.BookResult {
|
||||
book,err := models.NewBook().FindByFieldFirst("identify",identify)
|
||||
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
@@ -33,7 +34,7 @@ func isReadable (identify,token string,c *DocumentController) *models.BookResult
|
||||
|
||||
is_ok := false
|
||||
|
||||
if c.Member != nil{
|
||||
if c.Member != nil {
|
||||
_, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId)
|
||||
if err == nil {
|
||||
is_ok = true
|
||||
@@ -46,10 +47,10 @@ func isReadable (identify,token string,c *DocumentController) *models.BookResult
|
||||
if token != "" && strings.EqualFold(token, book.PrivateToken) {
|
||||
c.SetSession(identify, token)
|
||||
|
||||
} else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
|
||||
} else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
|
||||
c.Abort("403")
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
@@ -57,17 +58,30 @@ func isReadable (identify,token string,c *DocumentController) *models.BookResult
|
||||
bookResult := book.ToBookResult()
|
||||
|
||||
if c.Member != nil {
|
||||
rel ,err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId,c.Member.MemberId)
|
||||
rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId)
|
||||
|
||||
if err == nil {
|
||||
bookResult.MemberId = rel.MemberId
|
||||
bookResult.RoleId = rel.RoleId
|
||||
bookResult.RelationshipId = rel.RelationshipId
|
||||
bookResult.MemberId = rel.MemberId
|
||||
bookResult.RoleId = rel.RoleId
|
||||
bookResult.RelationshipId = rel.RelationshipId
|
||||
}
|
||||
|
||||
}
|
||||
//判断是否需要显示评论框
|
||||
if bookResult.CommentStatus == "closed" {
|
||||
bookResult.IsDisplayComment = false
|
||||
} else if bookResult.CommentStatus == "open" {
|
||||
bookResult.IsDisplayComment = true
|
||||
} else if bookResult.CommentStatus == "group_only" {
|
||||
bookResult.IsDisplayComment = bookResult.RelationshipId > 0
|
||||
} else if bookResult.CommentStatus == "registered_only" {
|
||||
bookResult.IsDisplayComment = true
|
||||
}
|
||||
|
||||
return bookResult
|
||||
}
|
||||
|
||||
|
||||
func (c *DocumentController) Index() {
|
||||
c.Prepare()
|
||||
identify := c.Ctx.Input.Param(":key")
|
||||
@@ -78,6 +92,7 @@ func (c *DocumentController) Index() {
|
||||
}
|
||||
bookResult := isReadable(identify,token,c)
|
||||
|
||||
|
||||
c.TplName = "document/" + bookResult.Theme + "_read.tpl"
|
||||
|
||||
tree,err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId,0)
|
||||
|
Reference in New Issue
Block a user