2017-04-20 18:19:32 +08:00
|
|
|
package controllers
|
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
"encoding/json"
|
|
|
|
"html/template"
|
2017-04-25 20:05:59 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"os"
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
|
|
"github.com/lifei6671/godoc/models"
|
|
|
|
"github.com/lifei6671/godoc/utils"
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/astaxie/beego/orm"
|
2017-04-23 20:33:21 +08:00
|
|
|
"github.com/astaxie/beego/logs"
|
2017-04-24 18:25:17 +08:00
|
|
|
"github.com/lifei6671/godoc/conf"
|
2017-04-25 20:05:59 +08:00
|
|
|
"github.com/lifei6671/godoc/graphics"
|
2017-04-22 17:24:17 +08:00
|
|
|
)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2017-04-20 18:19:32 +08:00
|
|
|
type BookController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
func (c *BookController) Index() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "book/index.tpl"
|
|
|
|
|
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
books,totalCount,err := models.NewBook().FindToPager(pageIndex,conf.PageSize,c.Member.MemberId)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Abort("500")
|
|
|
|
}
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
if totalCount > 0 {
|
|
|
|
html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
c.Data["PageHtml"] = html
|
|
|
|
}else {
|
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
b,err := json.Marshal(books)
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
if err != nil || len(books) <= 0{
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Data["Result"] = template.JS("[]")
|
|
|
|
}else{
|
|
|
|
c.Data["Result"] = template.JS(string(b))
|
|
|
|
}
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dashboard 项目概要 .
|
2017-04-22 17:24:17 +08:00
|
|
|
func (c *BookController) Dashboard() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "book/dashboard.tpl"
|
|
|
|
|
|
|
|
key := c.Ctx.Input.Param(":key")
|
|
|
|
|
|
|
|
if key == ""{
|
|
|
|
c.Abort("404")
|
|
|
|
}
|
|
|
|
|
|
|
|
book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
c.Abort("500")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["Model"] = *book
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setting 项目设置 .
|
2017-04-22 17:24:17 +08:00
|
|
|
func (c *BookController) Setting() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "book/setting.tpl"
|
|
|
|
|
|
|
|
key := c.Ctx.Input.Param(":key")
|
|
|
|
|
|
|
|
if key == ""{
|
|
|
|
c.Abort("404")
|
|
|
|
}
|
|
|
|
|
|
|
|
book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
|
|
|
|
if err != nil {
|
2017-04-25 20:05:59 +08:00
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.Abort("404")
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
c.Abort("500")
|
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
//如果不是创始人也不是管理员则不能操作
|
|
|
|
if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
if book.PrivateToken != "" {
|
|
|
|
book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index",":key",book.Identify,"token",book.PrivateToken)
|
|
|
|
}
|
|
|
|
c.Data["Model"] = book
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//保存项目信息
|
|
|
|
func (c *BookController) SaveBook() {
|
|
|
|
bookResult,err := c.IsPermission()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(6001,err.Error())
|
|
|
|
}
|
|
|
|
book,err := models.NewBook().Find(bookResult.BookId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("SaveBook => ",err)
|
|
|
|
c.JsonResult(6002,err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
book_name := strings.TrimSpace(c.GetString("book_name"))
|
|
|
|
description := strings.TrimSpace(c.GetString("description",""))
|
|
|
|
comment_status := c.GetString("comment_status")
|
|
|
|
tag := strings.TrimSpace(c.GetString("label"))
|
|
|
|
|
|
|
|
if strings.Count(description,"") > 500 {
|
|
|
|
c.JsonResult(6004,"项目描述不能大于500字")
|
|
|
|
}
|
|
|
|
if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
|
|
|
|
comment_status = "closed"
|
|
|
|
}
|
|
|
|
if tag != ""{
|
|
|
|
tags := strings.Split(tag,";")
|
|
|
|
if len(tags) > 10 {
|
|
|
|
c.JsonResult(6005,"最多允许添加10个标签")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
book.BookName = book_name
|
|
|
|
book.Description = description
|
|
|
|
book.CommentStatus = comment_status
|
|
|
|
book.Label = tag
|
|
|
|
|
|
|
|
if err := book.Update();err != nil {
|
|
|
|
c.JsonResult(6006,"保存失败")
|
|
|
|
}
|
|
|
|
bookResult.BookName = book_name
|
|
|
|
bookResult.Description = description
|
|
|
|
bookResult.CommentStatus = comment_status
|
|
|
|
bookResult.Label = tag
|
|
|
|
c.JsonResult(0,"ok",bookResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BookController) PrivatelyOwned() {
|
|
|
|
|
|
|
|
status := c.GetString("status")
|
|
|
|
|
|
|
|
if status != "open" && status != "close" {
|
|
|
|
c.JsonResult(6003,"参数错误")
|
|
|
|
}
|
|
|
|
state := 0
|
|
|
|
if status == "open" {
|
|
|
|
state = 0
|
|
|
|
}else{
|
|
|
|
state = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
bookResult,err := c.IsPermission()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(6001,err.Error())
|
|
|
|
}
|
|
|
|
//只有创始人才能变更私有状态
|
|
|
|
if bookResult.RoleId != conf.BookFounder {
|
|
|
|
c.JsonResult(6002,"权限不足")
|
|
|
|
}
|
|
|
|
fmt.Printf("%+v",bookResult)
|
|
|
|
|
|
|
|
book,err := models.NewBook().Find(bookResult.BookId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(6005,"项目不存在")
|
|
|
|
}
|
|
|
|
book.PrivatelyOwned = state
|
|
|
|
|
|
|
|
logs.Info("",state,status)
|
|
|
|
|
|
|
|
err = book.Update()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("PrivatelyOwned => ",err)
|
|
|
|
c.JsonResult(6004,"保存失败")
|
|
|
|
}
|
|
|
|
c.JsonResult(0,"ok")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer 转让项目.
|
|
|
|
func (c *BookController) Transfer() {
|
|
|
|
c.Prepare()
|
|
|
|
account := c.GetString("account")
|
|
|
|
|
|
|
|
if account == "" {
|
|
|
|
c.JsonResult(6004,"接受者账号不能为空")
|
|
|
|
}
|
|
|
|
member,err := models.NewMember().FindByAccount(account)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("FindByAccount => ",err)
|
|
|
|
c.JsonResult(6005,"接受用户不存在")
|
|
|
|
}
|
|
|
|
if member.Status != 0 {
|
|
|
|
c.JsonResult(6006,"接受用户已被禁用")
|
|
|
|
}
|
|
|
|
if member.MemberId == c.Member.MemberId {
|
|
|
|
c.JsonResult(6007,"不能转让给自己")
|
|
|
|
}
|
|
|
|
bookResult,err := c.IsPermission()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(6001,err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = models.NewRelationship().Transfer(bookResult.BookId,c.Member.MemberId,member.MemberId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("Transfer => ",err)
|
|
|
|
c.JsonResult(6008,err.Error())
|
|
|
|
}
|
|
|
|
c.JsonResult(0,"ok")
|
|
|
|
}
|
|
|
|
//上传项目封面.
|
|
|
|
func (c *BookController) UploadCover() {
|
|
|
|
|
|
|
|
bookResult,err := c.IsPermission()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(6001,err.Error())
|
|
|
|
}
|
|
|
|
book,err := models.NewBook().Find(bookResult.BookId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("SaveBook => ",err)
|
|
|
|
c.JsonResult(6002,err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
file,moreFile,err := c.GetFile("image-file")
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("",err.Error())
|
|
|
|
c.JsonResult(500,"读取文件异常")
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
ext := filepath.Ext(moreFile.Filename)
|
|
|
|
|
|
|
|
if !strings.EqualFold(ext,".png") && !strings.EqualFold(ext,".jpg") && !strings.EqualFold(ext,".gif") && !strings.EqualFold(ext,".jpeg") {
|
|
|
|
c.JsonResult(500,"不支持的图片格式")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
x1 ,_ := strconv.ParseFloat(c.GetString("x"),10)
|
|
|
|
y1 ,_ := strconv.ParseFloat(c.GetString("y"),10)
|
|
|
|
w1 ,_ := strconv.ParseFloat(c.GetString("width"),10)
|
|
|
|
h1 ,_ := strconv.ParseFloat(c.GetString("height"),10)
|
|
|
|
|
|
|
|
x := int(x1)
|
|
|
|
y := int(y1)
|
|
|
|
width := int(w1)
|
|
|
|
height := int(h1)
|
|
|
|
|
|
|
|
fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
|
|
|
|
|
|
|
|
filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext
|
|
|
|
|
|
|
|
path := filepath.Dir(filePath)
|
|
|
|
|
|
|
|
os.MkdirAll(path, os.ModePerm)
|
|
|
|
|
|
|
|
err = c.SaveToFile("image-file",filePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("",err)
|
|
|
|
c.JsonResult(500,"图片保存失败")
|
|
|
|
}
|
|
|
|
|
|
|
|
//剪切图片
|
|
|
|
subImg,err := graphics.ImageCopyFromFile(filePath,x,y,width,height)
|
|
|
|
|
|
|
|
if err != nil{
|
|
|
|
logs.Error("graphics.ImageCopyFromFile => ",err)
|
|
|
|
c.JsonResult(500,"图片剪切")
|
|
|
|
}
|
|
|
|
//生成缩略图并保存到磁盘
|
|
|
|
err = graphics.ImageResizeSaveFile(subImg,175,230,filePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("ImageResizeSaveFile => ",err.Error())
|
|
|
|
c.JsonResult(500,"保存图片失败")
|
|
|
|
}
|
|
|
|
|
|
|
|
url := "/" + filePath
|
|
|
|
|
|
|
|
old_cover := book.Cover
|
|
|
|
|
|
|
|
book.Cover = url
|
|
|
|
|
|
|
|
if err := book.Update() ; err != nil {
|
|
|
|
c.JsonResult(6001,"保存图片失败")
|
|
|
|
}
|
|
|
|
//如果原封面不是默认封面则删除
|
|
|
|
if old_cover != conf.GetDefaultCover() {
|
|
|
|
os.Remove("." + old_cover)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JsonResult(0,"ok",url)
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
// Users 用户列表.
|
2017-04-22 17:24:17 +08:00
|
|
|
func (c *BookController) Users() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "book/users.tpl"
|
|
|
|
|
|
|
|
key := c.Ctx.Input.Param(":key")
|
|
|
|
pageIndex,_ := c.GetInt("page",1)
|
|
|
|
|
|
|
|
if key == ""{
|
|
|
|
c.Abort("404")
|
|
|
|
}
|
|
|
|
|
|
|
|
book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
c.Abort("500")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["Model"] = *book
|
|
|
|
|
|
|
|
members,totalCount,err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId,pageIndex,15)
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
if totalCount > 0 {
|
|
|
|
html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 10, totalCount)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
c.Data["PageHtml"] = html
|
|
|
|
}else{
|
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
b,err := json.Marshal(members)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Data["Result"] = template.JS("[]")
|
|
|
|
}else{
|
|
|
|
c.Data["Result"] = template.JS(string(b))
|
|
|
|
}
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
// AddMember 参加参与用户.
|
2017-04-22 17:24:17 +08:00
|
|
|
func (c *BookController) AddMember() {
|
|
|
|
identify := c.GetString("identify")
|
|
|
|
account := c.GetString("account")
|
|
|
|
role_id,_ := c.GetInt("role_id",3)
|
|
|
|
|
|
|
|
if identify == "" || account == ""{
|
|
|
|
c.JsonResult(6001,"参数错误")
|
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
book ,err := c.IsPermission()
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2017-04-25 20:05:59 +08:00
|
|
|
c.JsonResult(6001,err.Error())
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
member := models.NewMember()
|
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
if _,err := member.FindByAccount(account) ; err != nil {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.JsonResult(404,"用户不存在")
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
if member.Status == 1 {
|
|
|
|
c.JsonResult(6003,"用户已被禁用")
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
if _,err := models.NewRelationship().FindForRoleId(book.BookId,member.MemberId);err == nil {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.JsonResult(6003,"用户已存在该项目中")
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship := models.NewRelationship()
|
|
|
|
relationship.BookId = book.BookId
|
|
|
|
relationship.MemberId = member.MemberId
|
|
|
|
relationship.RoleId = role_id
|
|
|
|
|
|
|
|
if err := relationship.Insert(); err == nil {
|
2017-04-24 18:25:17 +08:00
|
|
|
memberRelationshipResult := models.NewMemberRelationshipResult().FromMember(member)
|
|
|
|
memberRelationshipResult.RoleId = role_id
|
|
|
|
memberRelationshipResult.RelationshipId = relationship.RelationshipId
|
|
|
|
memberRelationshipResult.BookId = book.BookId
|
|
|
|
memberRelationshipResult.ResolveRoleName()
|
|
|
|
|
|
|
|
|
|
|
|
c.JsonResult(0,"ok",memberRelationshipResult)
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
|
|
|
c.JsonResult(500,err.Error())
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
// 变更指定用户在指定项目中的权限
|
|
|
|
func (c *BookController) ChangeRole() {
|
|
|
|
identify := c.GetString("identify")
|
|
|
|
member_id,_ := c.GetInt("member_id",0)
|
|
|
|
role,_ := c.GetInt("role_id",0)
|
|
|
|
|
|
|
|
if identify == "" || member_id <=0 {
|
|
|
|
c.JsonResult(6001,"参数错误")
|
|
|
|
}
|
|
|
|
if member_id == c.Member.MemberId {
|
|
|
|
c.JsonResult(6006,"不能变更自己的权限")
|
|
|
|
}
|
|
|
|
book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.JsonResult(403,"权限不足")
|
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.JsonResult(404,"项目不存在")
|
|
|
|
}
|
|
|
|
c.JsonResult(6002,err.Error())
|
|
|
|
}
|
|
|
|
if book.RoleId != 0 && book.RoleId != 1 {
|
|
|
|
c.JsonResult(403,"权限不足")
|
|
|
|
}
|
|
|
|
|
|
|
|
member := models.NewMember()
|
|
|
|
|
|
|
|
if err := member.Find(member_id); err != nil {
|
|
|
|
c.JsonResult(6003,"用户不存在")
|
|
|
|
}
|
|
|
|
if member.Status == 1 {
|
|
|
|
c.JsonResult(6004,"用户已被禁用")
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship,err := models.NewRelationship().UpdateRoleId(book.BookId,member_id,role);
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("变更用户在项目中的权限 => ",err)
|
|
|
|
c.JsonResult(6005,err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
memberRelationshipResult := models.NewMemberRelationshipResult().FromMember(member)
|
|
|
|
memberRelationshipResult.RoleId = relationship.RoleId
|
|
|
|
memberRelationshipResult.RelationshipId = relationship.RelationshipId
|
|
|
|
memberRelationshipResult.BookId = book.BookId
|
|
|
|
memberRelationshipResult.ResolveRoleName()
|
|
|
|
|
|
|
|
c.JsonResult(0,"ok",memberRelationshipResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除参与者.
|
|
|
|
func (c *BookController) RemoveMember() {
|
|
|
|
identify := c.GetString("identify")
|
|
|
|
member_id,_ := c.GetInt("member_id",0)
|
|
|
|
|
|
|
|
if identify == "" || member_id <=0 {
|
|
|
|
c.JsonResult(6001,"参数错误")
|
|
|
|
}
|
|
|
|
if member_id == c.Member.MemberId {
|
2017-04-25 20:05:59 +08:00
|
|
|
c.JsonResult(6006,"不能删除自己")
|
2017-04-24 18:25:17 +08:00
|
|
|
}
|
|
|
|
book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.JsonResult(403,"权限不足")
|
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.JsonResult(404,"项目不存在")
|
|
|
|
}
|
|
|
|
c.JsonResult(6002,err.Error())
|
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
//如果不是创始人也不是管理员则不能操作
|
|
|
|
if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
|
2017-04-24 18:25:17 +08:00
|
|
|
c.JsonResult(403,"权限不足")
|
|
|
|
}
|
|
|
|
err = models.NewRelationship().DeleteByBookIdAndMemberId(book.BookId,member_id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(6007,err.Error())
|
|
|
|
}
|
|
|
|
c.JsonResult(0,"ok")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 创建项目.
|
2017-04-21 18:20:35 +08:00
|
|
|
func (c *BookController) Create() {
|
|
|
|
|
|
|
|
if c.Ctx.Input.IsPost() {
|
|
|
|
book_name := strings.TrimSpace(c.GetString("book_name",""))
|
|
|
|
identify := strings.TrimSpace(c.GetString("identify",""))
|
|
|
|
description := strings.TrimSpace(c.GetString("description",""))
|
2017-04-22 17:24:17 +08:00
|
|
|
privately_owned,_ := strconv.Atoi(c.GetString("privately_owned"))
|
2017-04-21 18:20:35 +08:00
|
|
|
comment_status := c.GetString("comment_status")
|
2017-04-21 18:24:31 +08:00
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
if book_name == "" {
|
|
|
|
c.JsonResult(6001,"项目名称不能为空")
|
|
|
|
}
|
|
|
|
if identify == "" {
|
|
|
|
c.JsonResult(6002,"项目标识不能为空")
|
|
|
|
}
|
|
|
|
if ok,err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`,identify); !ok || err != nil {
|
|
|
|
c.JsonResult(6003,"文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
|
|
|
|
}
|
|
|
|
if strings.Count(identify,"") > 50 {
|
|
|
|
c.JsonResult(6004,"文档标识不能超过50字")
|
|
|
|
}
|
|
|
|
if strings.Count(description,"") > 500 {
|
|
|
|
c.JsonResult(6004,"项目描述不能大于500字")
|
|
|
|
}
|
|
|
|
if privately_owned !=0 && privately_owned != 1 {
|
|
|
|
privately_owned = 1
|
|
|
|
}
|
|
|
|
if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
|
|
|
|
comment_status = "closed"
|
|
|
|
}
|
|
|
|
|
|
|
|
book := models.NewBook()
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
if books,_ := book.FindByField("identify",identify); len(books) > 0 {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.JsonResult(6006,"项目标识已存在")
|
|
|
|
}
|
|
|
|
|
|
|
|
book.BookName = book_name
|
|
|
|
book.Description = description
|
|
|
|
book.CommentCount = 0
|
|
|
|
book.PrivatelyOwned = privately_owned
|
|
|
|
book.CommentStatus = comment_status
|
|
|
|
book.Identify = identify
|
|
|
|
book.DocCount = 0
|
|
|
|
book.MemberId = c.Member.MemberId
|
|
|
|
book.CommentCount = 0
|
|
|
|
book.Version = time.Now().Unix()
|
|
|
|
book.Cover = beego.AppConfig.String("cover")
|
|
|
|
|
|
|
|
err := book.Insert()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(6005,err.Error())
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
bookResult := models.NewBookResult()
|
|
|
|
bookResult.FindByIdentify(book.Identify,c.Member.MemberId)
|
|
|
|
|
|
|
|
c.JsonResult(0,"ok",bookResult)
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
c.JsonResult(6001,"error")
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Edit 编辑项目.
|
|
|
|
func (p *BookController) Edit() {
|
|
|
|
p.TplName = "book/edit.tpl"
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
// CreateToken 创建访问来令牌.
|
2017-04-23 20:33:21 +08:00
|
|
|
func (c *BookController) CreateToken() {
|
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
action := c.GetString("action")
|
2017-04-23 20:33:21 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
bookResult ,err := c.IsPermission()
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.JsonResult(403,"权限不足")
|
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.JsonResult(404,"项目不存在")
|
|
|
|
}
|
|
|
|
logs.Error("生成阅读令牌失败 =>",err)
|
|
|
|
c.JsonResult(6002,err.Error())
|
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
book := models.NewBook()
|
|
|
|
|
|
|
|
if _,err := book.Find(bookResult.BookId);err != nil {
|
|
|
|
c.JsonResult(6001,"项目不存在")
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
if action == "create" {
|
|
|
|
if bookResult.PrivatelyOwned == 0 {
|
|
|
|
c.JsonResult(6001, "公开项目不能创建阅读令牌")
|
|
|
|
}
|
2017-04-23 20:33:21 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
|
|
|
|
if err := book.Update(); err != nil {
|
|
|
|
logs.Error("生成阅读令牌失败 => ", err)
|
|
|
|
c.JsonResult(6003, "生成阅读令牌失败")
|
|
|
|
}
|
|
|
|
c.JsonResult(0, "ok", c.BaseUrl() + beego.URLFor("DocumentController.Index",":key",book.Identify,"token",book.PrivateToken))
|
|
|
|
}else{
|
|
|
|
book.PrivateToken = ""
|
|
|
|
if err := book.Update();err != nil {
|
|
|
|
logs.Error("CreateToken => ",err)
|
|
|
|
c.JsonResult(6004,"删除令牌失败")
|
|
|
|
}
|
|
|
|
c.JsonResult(0,"ok","")
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete 删除项目.
|
2017-04-23 20:33:21 +08:00
|
|
|
func (c *BookController) Delete() {
|
|
|
|
c.Prepare()
|
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
bookResult ,err := c.IsPermission()
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2017-04-25 20:05:59 +08:00
|
|
|
c.JsonResult(6001,err.Error())
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
if bookResult.RoleId != conf.BookFounder {
|
|
|
|
c.JsonResult(6002,"只有创始人才能删除项目")
|
|
|
|
}
|
|
|
|
err = models.NewBook().ThoroughDeleteBook(bookResult.BookId)
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.JsonResult(6002,"项目不存在")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("删除项目 => ",err)
|
|
|
|
c.JsonResult(6003,"删除失败")
|
|
|
|
}
|
|
|
|
c.JsonResult(0,"ok")
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
func (c *BookController) IsPermission() (*models.BookResult,error) {
|
|
|
|
identify := c.GetString("identify")
|
|
|
|
book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
|
2017-04-20 18:19:32 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
return book,errors.New("权限不足")
|
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
return book,errors.New("项目不存在")
|
|
|
|
}
|
|
|
|
return book,err
|
|
|
|
}
|
|
|
|
if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
|
|
|
|
return book,errors.New("权限不足")
|
|
|
|
}
|
|
|
|
return book,nil
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|