2017-04-20 18:19:32 +08:00
|
|
|
package controllers
|
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-04-25 20:05:59 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-12-24 15:23:17 +08:00
|
|
|
"github.com/lifei6671/mindoc/utils/sqltil"
|
2018-01-26 17:17:38 +08:00
|
|
|
"html/template"
|
2017-04-25 20:05:59 +08:00
|
|
|
"os"
|
2018-01-26 17:17:38 +08:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
"net/http"
|
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
"github.com/astaxie/beego"
|
2017-04-23 20:33:21 +08:00
|
|
|
"github.com/astaxie/beego/logs"
|
2018-01-26 17:17:38 +08:00
|
|
|
"github.com/astaxie/beego/orm"
|
2017-06-14 09:23:29 +08:00
|
|
|
"github.com/lifei6671/mindoc/conf"
|
|
|
|
"github.com/lifei6671/mindoc/graphics"
|
2018-01-26 17:17:38 +08:00
|
|
|
"github.com/lifei6671/mindoc/models"
|
|
|
|
"github.com/lifei6671/mindoc/utils"
|
2018-01-26 18:07:55 +08:00
|
|
|
"github.com/lifei6671/mindoc/utils/pagination"
|
2018-03-06 15:51:18 +08:00
|
|
|
"gopkg.in/russross/blackfriday.v2"
|
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)
|
|
|
|
|
2018-01-26 17:17:38 +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 {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("BookController.Index => ", err)
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Abort("500")
|
|
|
|
}
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
for i, book := range books {
|
2018-03-06 13:47:51 +08:00
|
|
|
books[i].Description = utils.StripTags(string(blackfriday.Run([]byte(book.Description))))
|
2018-03-22 20:45:50 +08:00
|
|
|
books[i].ModifyTime = book.ModifyTime.Local()
|
|
|
|
books[i].CreateTime = book.CreateTime.Local()
|
2018-02-28 15:47:00 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
if totalCount > 0 {
|
2018-03-24 17:24:02 +08:00
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
|
2018-01-26 18:07:55 +08:00
|
|
|
c.Data["PageHtml"] = pager.HtmlPages()
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-04-24 18:25:17 +08:00
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
b, err := json.Marshal(books)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if err != nil || len(books) <= 0 {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Data["Result"] = template.JS("[]")
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Data["Result"] = template.JS(string(b))
|
|
|
|
}
|
2018-11-21 18:15:48 +08:00
|
|
|
if itemsets, err := models.NewItemsets().First(1); err == nil {
|
|
|
|
c.Data["Item"] = itemsets
|
|
|
|
}
|
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")
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if key == "" {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Abort("404")
|
|
|
|
}
|
2018-11-14 15:57:55 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
|
2017-04-22 17:24:17 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
c.Abort("500")
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:47:51 +08:00
|
|
|
c.Data["Description"] = template.HTML(blackfriday.Run([]byte(book.Description)))
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Data["Model"] = *book
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setting 项目设置 .
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookController) Setting() {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "book/setting.tpl"
|
|
|
|
|
|
|
|
key := c.Ctx.Input.Param(":key")
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if key == "" {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Abort("404")
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
|
2017-04-22 17:24:17 +08:00
|
|
|
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 != "" {
|
2018-03-13 19:20:50 +08:00
|
|
|
book.PrivateToken = conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
c.Data["Model"] = book
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//保存项目信息
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookController) SaveBook() {
|
|
|
|
bookResult, err := c.IsPermission()
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6001, err.Error())
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBook().Find(bookResult.BookId)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("SaveBook => ", err)
|
|
|
|
c.JsonResult(6002, err.Error())
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 13:21:27 +08:00
|
|
|
bookName := strings.TrimSpace(c.GetString("book_name"))
|
2018-01-26 17:17:38 +08:00
|
|
|
description := strings.TrimSpace(c.GetString("description", ""))
|
2018-01-30 13:21:27 +08:00
|
|
|
commentStatus := c.GetString("comment_status")
|
2019-01-15 19:36:11 +08:00
|
|
|
//tag := strings.TrimSpace(c.GetString("label"))
|
2017-04-26 18:17:38 +08:00
|
|
|
editor := strings.TrimSpace(c.GetString("editor"))
|
2018-01-30 13:21:27 +08:00
|
|
|
autoRelease := strings.TrimSpace(c.GetString("auto_release")) == "on"
|
|
|
|
publisher := strings.TrimSpace(c.GetString("publisher"))
|
2018-03-24 17:24:02 +08:00
|
|
|
historyCount, _ := c.GetInt("history_count", 0)
|
2018-02-28 15:47:00 +08:00
|
|
|
isDownload := strings.TrimSpace(c.GetString("is_download")) == "on"
|
2018-03-13 19:20:50 +08:00
|
|
|
enableShare := strings.TrimSpace(c.GetString("enable_share")) == "on"
|
2018-03-24 17:24:02 +08:00
|
|
|
isUseFirstDocument := strings.TrimSpace(c.GetString("is_use_first_document")) == "on"
|
2018-08-17 17:33:45 +08:00
|
|
|
autoSave := strings.TrimSpace(c.GetString("auto_save")) == "on"
|
2018-11-21 18:15:48 +08:00
|
|
|
itemId, _ := c.GetInt("itemId")
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.Count(description, "") > 500 {
|
|
|
|
c.JsonResult(6004, "项目描述不能大于500字")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-30 13:21:27 +08:00
|
|
|
if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
|
|
|
|
commentStatus = "closed"
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2019-01-15 19:36:11 +08:00
|
|
|
|
2018-11-20 20:36:14 +08:00
|
|
|
if !models.NewItemsets().Exist(itemId) {
|
2018-11-21 18:15:48 +08:00
|
|
|
c.JsonResult(6006, "项目空间不存在")
|
2018-11-20 20:36:14 +08:00
|
|
|
}
|
2017-04-26 18:17:38 +08:00
|
|
|
if editor != "markdown" && editor != "html" {
|
|
|
|
editor = "markdown"
|
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-01-30 13:21:27 +08:00
|
|
|
book.BookName = bookName
|
2018-01-26 17:17:38 +08:00
|
|
|
book.Description = description
|
2018-01-30 13:21:27 +08:00
|
|
|
book.CommentStatus = commentStatus
|
|
|
|
book.Publisher = publisher
|
2019-01-15 19:36:11 +08:00
|
|
|
//book.Label = tag
|
2018-01-26 17:17:38 +08:00
|
|
|
book.Editor = editor
|
2018-02-02 23:12:29 +08:00
|
|
|
book.HistoryCount = historyCount
|
2018-02-28 15:47:00 +08:00
|
|
|
book.IsDownload = 0
|
2018-11-20 20:55:04 +08:00
|
|
|
book.BookPassword = strings.TrimSpace(c.GetString("bPassword"))
|
2018-11-20 20:36:14 +08:00
|
|
|
book.ItemId = itemId
|
2018-02-02 23:12:29 +08:00
|
|
|
|
2018-01-30 13:21:27 +08:00
|
|
|
if autoRelease {
|
2018-01-19 20:10:54 +08:00
|
|
|
book.AutoRelease = 1
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2018-01-19 20:10:54 +08:00
|
|
|
book.AutoRelease = 0
|
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
if isDownload {
|
|
|
|
book.IsDownload = 0
|
2018-03-24 17:24:02 +08:00
|
|
|
} else {
|
2018-02-28 15:47:00 +08:00
|
|
|
book.IsDownload = 1
|
|
|
|
}
|
2018-03-13 19:20:50 +08:00
|
|
|
if enableShare {
|
|
|
|
book.IsEnableShare = 0
|
2018-03-24 17:24:02 +08:00
|
|
|
} else {
|
2018-03-13 19:20:50 +08:00
|
|
|
book.IsEnableShare = 1
|
|
|
|
}
|
2018-03-23 10:00:36 +08:00
|
|
|
if isUseFirstDocument {
|
|
|
|
book.IsUseFirstDocument = 1
|
2018-03-24 17:24:02 +08:00
|
|
|
} else {
|
2018-03-23 10:00:36 +08:00
|
|
|
book.IsUseFirstDocument = 0
|
|
|
|
}
|
2018-08-17 17:33:45 +08:00
|
|
|
if autoSave {
|
|
|
|
book.AutoSave = 1
|
2018-11-06 18:38:18 +08:00
|
|
|
} else {
|
2018-08-17 17:33:45 +08:00
|
|
|
book.AutoSave = 0
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if err := book.Update(); err != nil {
|
|
|
|
c.JsonResult(6006, "保存失败")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-30 13:21:27 +08:00
|
|
|
bookResult.BookName = bookName
|
2017-04-25 20:05:59 +08:00
|
|
|
bookResult.Description = description
|
2018-01-30 13:21:27 +08:00
|
|
|
bookResult.CommentStatus = commentStatus
|
2018-08-13 19:05:49 +08:00
|
|
|
|
2018-08-14 15:57:52 +08:00
|
|
|
beego.Info("用户 [", c.Member.Account, "] 修改了项目 ->", book)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok", bookResult)
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
//设置项目私有状态.
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookController) PrivatelyOwned() {
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
status := c.GetString("status")
|
|
|
|
|
|
|
|
if status != "open" && status != "close" {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6003, "参数错误")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
state := 0
|
|
|
|
if status == "open" {
|
|
|
|
state = 0
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-04-25 20:05:59 +08:00
|
|
|
state = 1
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
bookResult, err := c.IsPermission()
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6001, err.Error())
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
//只有创始人才能变更私有状态
|
|
|
|
if bookResult.RoleId != conf.BookFounder {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6002, "权限不足")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBook().Find(bookResult.BookId)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6005, "项目不存在")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
book.PrivatelyOwned = state
|
|
|
|
|
|
|
|
err = book.Update()
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("PrivatelyOwned => ", err)
|
|
|
|
c.JsonResult(6004, "保存失败")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-08-14 15:57:52 +08:00
|
|
|
beego.Info("用户 【", c.Member.Account, "]修改了项目权限 ->", state)
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer 转让项目.
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookController) Transfer() {
|
2017-04-25 20:05:59 +08:00
|
|
|
c.Prepare()
|
|
|
|
account := c.GetString("account")
|
|
|
|
|
|
|
|
if account == "" {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6004, "接受者账号不能为空")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
member, err := models.NewMember().FindByAccount(account)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("FindByAccount => ", err)
|
|
|
|
c.JsonResult(6005, "接受用户不存在")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
if member.Status != 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6006, "接受用户已被禁用")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
if member.MemberId == c.Member.MemberId {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6007, "不能转让给自己")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
bookResult, err := c.IsPermission()
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6001, err.Error())
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err = models.NewRelationship().Transfer(bookResult.BookId, c.Member.MemberId, member.MemberId)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-11-14 15:57:55 +08:00
|
|
|
logs.Error("转让项目失败 -> ", err)
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6008, err.Error())
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
//上传项目封面.
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookController) UploadCover() {
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
bookResult, err := c.IsPermission()
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6001, err.Error())
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBook().Find(bookResult.BookId)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("SaveBook => ", err)
|
|
|
|
c.JsonResult(6002, err.Error())
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
file, moreFile, err := c.GetFile("image-file")
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-08-14 15:57:52 +08:00
|
|
|
logs.Error("获取上传文件失败 ->", err.Error())
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(500, "读取文件异常")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-08-14 15:57:52 +08:00
|
|
|
defer file.Close()
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
ext := filepath.Ext(moreFile.Filename)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") {
|
|
|
|
c.JsonResult(500, "不支持的图片格式")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
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)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
x := int(x1)
|
|
|
|
y := int(y1)
|
|
|
|
width := int(w1)
|
|
|
|
height := int(h1)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-08-14 15:57:52 +08:00
|
|
|
//附件路径按照项目组织
|
2018-11-06 18:38:18 +08:00
|
|
|
filePath := filepath.Join("uploads", book.Identify, "images", fileName+ext)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
path := filepath.Dir(filePath)
|
|
|
|
|
|
|
|
os.MkdirAll(path, os.ModePerm)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err = c.SaveToFile("image-file", filePath)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("", err)
|
|
|
|
c.JsonResult(500, "图片保存失败")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2017-06-05 10:52:14 +08:00
|
|
|
defer func(filePath string) {
|
|
|
|
os.Remove(filePath)
|
|
|
|
}(filePath)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
//剪切图片
|
2018-01-26 17:17:38 +08:00
|
|
|
subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if err != nil {
|
|
|
|
logs.Error("graphics.ImageCopyFromFile => ", err)
|
|
|
|
c.JsonResult(500, "图片剪切")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2017-06-05 10:52:14 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
|
2017-06-05 10:52:14 +08:00
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
//生成缩略图并保存到磁盘
|
2018-08-13 19:05:49 +08:00
|
|
|
err = graphics.ImageResizeSaveFile(subImg, 350, 460, filePath)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("ImageResizeSaveFile => ", err.Error())
|
|
|
|
c.JsonResult(500, "保存图片失败")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
|
2017-06-05 13:38:06 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.HasPrefix(url, "//") {
|
2017-06-05 13:38:06 +08:00
|
|
|
url = string(url[1:])
|
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-02-22 16:34:23 +08:00
|
|
|
oldCover := book.Cover
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-03-13 19:20:50 +08:00
|
|
|
book.Cover = conf.URLForWithCdnImage(url)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if err := book.Update(); err != nil {
|
|
|
|
c.JsonResult(6001, "保存图片失败")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
//如果原封面不是默认封面则删除
|
2018-02-22 16:34:23 +08:00
|
|
|
if oldCover != conf.GetDefaultCover() {
|
|
|
|
os.Remove("." + oldCover)
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-08-14 15:57:52 +08:00
|
|
|
beego.Info("用户[", c.Member.Account, "]上传了项目封面 ->", book.BookName, book.BookId, book.Cover)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
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")
|
2018-01-26 17:17:38 +08:00
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if key == "" {
|
2018-11-06 18:38:18 +08:00
|
|
|
c.ShowErrorPage(404, "项目不存在或已删除")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
|
2017-04-22 17:24:17 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
c.Abort("500")
|
|
|
|
}
|
2018-11-26 18:54:50 +08:00
|
|
|
//如果不是创始人也不是管理员则不能操作
|
|
|
|
if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Data["Model"] = *book
|
|
|
|
|
2018-11-06 18:38:18 +08:00
|
|
|
members, totalCount, err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId, pageIndex, conf.PageSize)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
if totalCount > 0 {
|
2018-03-24 17:24:02 +08:00
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
|
2018-01-26 18:07:55 +08:00
|
|
|
c.Data["PageHtml"] = pager.HtmlPages()
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-04-24 18:25:17 +08:00
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
b, err := json.Marshal(members)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Data["Result"] = template.JS("[]")
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-04-22 17:24:17 +08:00
|
|
|
c.Data["Result"] = template.JS(string(b))
|
|
|
|
}
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
// Create 创建项目.
|
2017-04-21 18:20:35 +08:00
|
|
|
func (c *BookController) Create() {
|
|
|
|
|
|
|
|
if c.Ctx.Input.IsPost() {
|
2018-07-10 18:53:41 +08:00
|
|
|
bookName := strings.TrimSpace(c.GetString("book_name", ""))
|
2018-01-26 17:17:38 +08:00
|
|
|
identify := strings.TrimSpace(c.GetString("identify", ""))
|
|
|
|
description := strings.TrimSpace(c.GetString("description", ""))
|
2018-02-28 15:47:00 +08:00
|
|
|
privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned"))
|
2018-07-10 18:53:41 +08:00
|
|
|
commentStatus := c.GetString("comment_status")
|
2018-11-20 20:36:14 +08:00
|
|
|
itemId, _ := c.GetInt("itemId")
|
2017-04-21 18:24:31 +08:00
|
|
|
|
2018-07-10 18:53:41 +08:00
|
|
|
if bookName == "" {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6001, "项目名称不能为空")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
|
|
|
if identify == "" {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6002, "项目标识不能为空")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, identify); !ok || err != nil {
|
|
|
|
c.JsonResult(6003, "项目标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.Count(identify, "") > 50 {
|
|
|
|
c.JsonResult(6004, "文档标识不能超过50字")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.Count(description, "") > 500 {
|
|
|
|
c.JsonResult(6004, "项目描述不能大于500字")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
if privatelyOwned != 0 && privatelyOwned != 1 {
|
|
|
|
privatelyOwned = 1
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
2018-11-20 20:36:14 +08:00
|
|
|
if !models.NewItemsets().Exist(itemId) {
|
2018-11-21 11:03:16 +08:00
|
|
|
c.JsonResult(6005, "项目空间不存在")
|
2018-11-20 20:36:14 +08:00
|
|
|
}
|
2018-07-10 18:53:41 +08:00
|
|
|
if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
|
|
|
|
commentStatus = "closed"
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
|
|
|
book := models.NewBook()
|
2018-02-22 16:34:23 +08:00
|
|
|
book.Cover = conf.GetDefaultCover()
|
|
|
|
|
|
|
|
//如果客户端上传了项目封面则直接保存
|
2018-03-24 17:24:02 +08:00
|
|
|
if file, moreFile, err := c.GetFile("image-file"); err == nil {
|
2018-02-22 16:34:23 +08:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
ext := filepath.Ext(moreFile.Filename)
|
|
|
|
|
|
|
|
//如果上传的是图片
|
|
|
|
if strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".gif") || strings.EqualFold(ext, ".jpeg") {
|
|
|
|
|
|
|
|
fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
filePath := filepath.Join("uploads", time.Now().Format("200601"), fileName+ext)
|
2018-02-22 16:34:23 +08:00
|
|
|
|
|
|
|
path := filepath.Dir(filePath)
|
|
|
|
|
|
|
|
os.MkdirAll(path, os.ModePerm)
|
|
|
|
|
|
|
|
if err := c.SaveToFile("image-file", filePath); err == nil {
|
|
|
|
url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
|
|
|
|
|
|
|
|
if strings.HasPrefix(url, "//") {
|
|
|
|
url = string(url[1:])
|
|
|
|
}
|
|
|
|
book.Cover = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:05:49 +08:00
|
|
|
if books, _ := book.FindByField("identify", identify, "book_id"); len(books) > 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6006, "项目标识已存在")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 18:53:41 +08:00
|
|
|
book.BookName = bookName
|
2017-04-22 17:24:17 +08:00
|
|
|
book.Description = description
|
|
|
|
book.CommentCount = 0
|
2018-02-28 15:47:00 +08:00
|
|
|
book.PrivatelyOwned = privatelyOwned
|
2018-07-10 18:53:41 +08:00
|
|
|
book.CommentStatus = commentStatus
|
2018-01-26 17:17:38 +08:00
|
|
|
book.Identify = identify
|
|
|
|
book.DocCount = 0
|
|
|
|
book.MemberId = c.Member.MemberId
|
2017-04-22 17:24:17 +08:00
|
|
|
book.CommentCount = 0
|
2018-01-26 17:17:38 +08:00
|
|
|
book.Version = time.Now().Unix()
|
2018-04-03 11:20:34 +08:00
|
|
|
book.IsEnableShare = 0
|
|
|
|
book.IsUseFirstDocument = 1
|
|
|
|
book.IsDownload = 1
|
|
|
|
book.AutoRelease = 0
|
2018-11-20 20:36:14 +08:00
|
|
|
book.ItemId = itemId
|
2018-02-22 16:34:23 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
book.Editor = "markdown"
|
|
|
|
book.Theme = "default"
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
if err := book.Insert(); err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("Insert => ", err)
|
|
|
|
c.JsonResult(6005, "保存项目失败")
|
2017-04-22 17:24:17 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
|
2017-05-01 12:15:55 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
beego.Error(err)
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
2018-08-14 15:57:52 +08:00
|
|
|
beego.Info("用户[", c.Member.Account, "]创建了项目 ->", book)
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok", bookResult)
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6001, "error")
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
2018-08-13 19:05:49 +08:00
|
|
|
|
2018-07-10 18:53:41 +08:00
|
|
|
//复制项目
|
2018-08-13 19:05:49 +08:00
|
|
|
func (c *BookController) Copy() {
|
2018-07-10 18:53:41 +08:00
|
|
|
if c.Ctx.Input.IsPost() {
|
2018-07-13 10:40:42 +08:00
|
|
|
//检查是否有复制项目的权限
|
2018-08-13 19:05:49 +08:00
|
|
|
if _, err := c.IsPermission(); err != nil {
|
|
|
|
c.JsonResult(500, err.Error())
|
2018-07-13 10:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 18:53:41 +08:00
|
|
|
identify := strings.TrimSpace(c.GetString("identify", ""))
|
|
|
|
if identify == "" {
|
2018-08-13 19:05:49 +08:00
|
|
|
c.JsonResult(6001, "参数错误")
|
2018-07-10 18:53:41 +08:00
|
|
|
}
|
|
|
|
book := models.NewBook()
|
|
|
|
err := book.Copy(identify)
|
|
|
|
if err != nil {
|
2018-08-13 19:05:49 +08:00
|
|
|
c.JsonResult(6002, "复制项目出错")
|
|
|
|
} else {
|
2018-07-10 18:53:41 +08:00
|
|
|
bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("查询失败")
|
|
|
|
}
|
2018-08-13 19:05:49 +08:00
|
|
|
c.JsonResult(0, "ok", bookResult)
|
2018-07-10 18:53:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 17:46:47 +08:00
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
//导入zip压缩包
|
2018-01-30 18:13:25 +08:00
|
|
|
func (c *BookController) Import() {
|
|
|
|
|
|
|
|
file, moreFile, err := c.GetFile("import-file")
|
|
|
|
if err == http.ErrMissingFile {
|
|
|
|
c.JsonResult(6003, "没有发现需要上传的文件")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
bookName := strings.TrimSpace(c.GetString("book_name"))
|
|
|
|
identify := strings.TrimSpace(c.GetString("identify"))
|
2018-03-24 22:36:35 +08:00
|
|
|
description := strings.TrimSpace(c.GetString("description", ""))
|
|
|
|
privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned"))
|
2018-11-20 20:36:14 +08:00
|
|
|
itemId, _ := c.GetInt("itemId")
|
2018-03-24 17:24:02 +08:00
|
|
|
|
|
|
|
if bookName == "" {
|
|
|
|
c.JsonResult(6001, "项目名称不能为空")
|
|
|
|
}
|
|
|
|
if len([]rune(bookName)) > 500 {
|
|
|
|
c.JsonResult(6002, "项目名称不能大于500字")
|
|
|
|
}
|
|
|
|
if identify == "" {
|
|
|
|
c.JsonResult(6002, "项目标识不能为空")
|
|
|
|
}
|
|
|
|
if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, identify); !ok || err != nil {
|
|
|
|
c.JsonResult(6003, "项目标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
|
|
|
|
}
|
2018-11-20 20:36:14 +08:00
|
|
|
if !models.NewItemsets().Exist(itemId) {
|
2018-11-21 11:03:16 +08:00
|
|
|
c.JsonResult(6007, "项目空间不存在")
|
2018-11-20 20:36:14 +08:00
|
|
|
}
|
2018-03-24 17:24:02 +08:00
|
|
|
if strings.Count(identify, "") > 50 {
|
|
|
|
c.JsonResult(6004, "文档标识不能超过50字")
|
|
|
|
}
|
|
|
|
|
2018-03-19 17:46:47 +08:00
|
|
|
ext := filepath.Ext(moreFile.Filename)
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
if !strings.EqualFold(ext, ".zip") {
|
|
|
|
c.JsonResult(6004, "不支持的文件类型")
|
2018-03-19 17:46:47 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:05:49 +08:00
|
|
|
if books, _ := models.NewBook().FindByField("identify", identify, "book_id"); len(books) > 0 {
|
2018-03-25 14:59:42 +08:00
|
|
|
c.JsonResult(6006, "项目标识已存在")
|
|
|
|
}
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
tempPath := filepath.Join(os.TempDir(), c.CruSession.SessionID())
|
2018-01-30 18:13:25 +08:00
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
os.MkdirAll(tempPath, 0766)
|
2018-01-30 18:13:25 +08:00
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
tempPath = filepath.Join(tempPath, moreFile.Filename)
|
2018-01-30 18:13:25 +08:00
|
|
|
|
|
|
|
err = c.SaveToFile("import-file", tempPath)
|
|
|
|
|
2018-03-24 22:36:35 +08:00
|
|
|
book := models.NewBook()
|
|
|
|
|
|
|
|
book.MemberId = c.Member.MemberId
|
|
|
|
book.Cover = conf.GetDefaultCover()
|
|
|
|
book.BookName = bookName
|
|
|
|
book.Description = description
|
|
|
|
book.CommentCount = 0
|
|
|
|
book.PrivatelyOwned = privatelyOwned
|
|
|
|
book.CommentStatus = "closed"
|
|
|
|
book.Identify = identify
|
|
|
|
book.DocCount = 0
|
|
|
|
book.MemberId = c.Member.MemberId
|
|
|
|
book.CommentCount = 0
|
|
|
|
book.Version = time.Now().Unix()
|
2018-11-20 20:36:14 +08:00
|
|
|
book.ItemId = itemId
|
2018-03-24 22:36:35 +08:00
|
|
|
|
|
|
|
book.Editor = "markdown"
|
|
|
|
book.Theme = "default"
|
|
|
|
|
|
|
|
go book.ImportBook(tempPath)
|
2018-01-30 18:13:25 +08:00
|
|
|
|
2018-08-14 15:57:52 +08:00
|
|
|
beego.Info("用户[", c.Member.Account, "]导入了项目 ->", book)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
c.JsonResult(0, "项目正在后台转换中,请稍后查看")
|
2018-01-30 18:13:25 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
// CreateToken 创建访问来令牌.
|
2019-01-15 19:36:11 +08:00
|
|
|
//func (c *BookController) CreateToken() {
|
|
|
|
//
|
|
|
|
// action := c.GetString("action")
|
|
|
|
//
|
|
|
|
// bookResult, err := c.IsPermission()
|
|
|
|
//
|
|
|
|
// 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())
|
|
|
|
// }
|
|
|
|
// book := models.NewBook()
|
|
|
|
//
|
|
|
|
// if _, err := book.Find(bookResult.BookId); err != nil {
|
|
|
|
// c.JsonResult(6001, "项目不存在")
|
|
|
|
// }
|
|
|
|
// if action == "create" {
|
|
|
|
// if bookResult.PrivatelyOwned == 0 {
|
|
|
|
// c.JsonResult(6001, "公开项目不能创建阅读令牌")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
|
|
|
|
// if err := book.Update(); err != nil {
|
|
|
|
// logs.Error("生成阅读令牌失败 => ", err)
|
|
|
|
// c.JsonResult(6003, "生成阅读令牌失败")
|
|
|
|
// }
|
|
|
|
// beego.Info("用户[", c.Member.Account, "]创建项目令牌 ->", book.PrivateToken)
|
|
|
|
// c.JsonResult(0, "ok", conf.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, "删除令牌失败")
|
|
|
|
// }
|
|
|
|
// beego.Info("用户[", c.Member.Account, "]创建项目令牌 ->", book.PrivateToken)
|
|
|
|
// c.JsonResult(0, "ok", "")
|
|
|
|
// }
|
|
|
|
//}
|
2017-04-20 18:19:32 +08:00
|
|
|
|
|
|
|
// Delete 删除项目.
|
2017-04-23 20:33:21 +08:00
|
|
|
func (c *BookController) Delete() {
|
|
|
|
c.Prepare()
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
bookResult, err := c.IsPermission()
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +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 {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6002, "只有创始人才能删除项目")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
err = models.NewBook().ThoroughDeleteBook(bookResult.BookId)
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
if err == orm.ErrNoRows {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6002, "项目不存在")
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("删除项目 => ", err)
|
|
|
|
c.JsonResult(6003, "删除失败")
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
2018-08-14 15:57:52 +08:00
|
|
|
beego.Info("用户[", c.Member.Account, "]删除了项目 ->", bookResult)
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok")
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 10:00:21 +08:00
|
|
|
//发布项目.
|
2017-04-29 21:28:09 +08:00
|
|
|
func (c *BookController) Release() {
|
2017-04-30 22:13:12 +08:00
|
|
|
c.Prepare()
|
|
|
|
|
|
|
|
identify := c.GetString("identify")
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
bookId := 0
|
2017-05-12 10:45:40 +08:00
|
|
|
|
2017-05-26 14:19:27 +08:00
|
|
|
if c.Member.IsAdministrator() {
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
2017-05-12 10:45:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
bookId = book.BookId
|
|
|
|
} else {
|
2017-05-12 10:45:40 +08:00
|
|
|
book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.JsonResult(6001, "权限不足")
|
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.JsonResult(6002, "项目不存在")
|
|
|
|
}
|
|
|
|
beego.Error(err)
|
|
|
|
c.JsonResult(6003, "未知错误")
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
2017-05-12 10:45:40 +08:00
|
|
|
if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder && book.RoleId != conf.BookEditor {
|
|
|
|
c.JsonResult(6003, "权限不足")
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
bookId = book.BookId
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
2017-05-10 18:06:54 +08:00
|
|
|
go func(identify string) {
|
2018-03-25 14:59:42 +08:00
|
|
|
models.NewBook().ReleaseContent(bookId)
|
2017-05-10 18:06:54 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
//当文档发布后,需要删除已缓存的转换项目
|
2018-07-11 11:30:48 +08:00
|
|
|
outputPath := filepath.Join(conf.GetExportOutputPath(), strconv.Itoa(bookId))
|
2018-01-26 17:17:38 +08:00
|
|
|
os.RemoveAll(outputPath)
|
2017-05-10 18:06:54 +08:00
|
|
|
|
|
|
|
}(identify)
|
2017-04-30 22:13:12 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "发布任务已推送到任务队列,稍后将在后台执行。")
|
2017-04-29 21:28:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 10:00:21 +08:00
|
|
|
//文档排序.
|
2017-04-29 21:28:09 +08:00
|
|
|
func (c *BookController) SaveSort() {
|
|
|
|
c.Prepare()
|
|
|
|
|
|
|
|
identify := c.Ctx.Input.Param(":key")
|
|
|
|
if identify == "" {
|
|
|
|
c.Abort("404")
|
|
|
|
}
|
|
|
|
|
2017-05-26 14:19:27 +08:00
|
|
|
book_id := 0
|
|
|
|
if c.Member.IsAdministrator() {
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
2017-05-26 14:19:27 +08:00
|
|
|
if err != nil {
|
2017-04-29 21:28:09 +08:00
|
|
|
|
2017-05-26 14:19:27 +08:00
|
|
|
}
|
|
|
|
book_id = book.BookId
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
|
|
|
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
|
2017-05-26 14:19:27 +08:00
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
beego.Error("DocumentController.Edit => ", err)
|
2017-04-29 21:28:09 +08:00
|
|
|
|
2017-05-26 14:19:27 +08:00
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
if bookResult.RoleId == conf.BookObserver {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6002, "项目不存在或权限不足")
|
2017-05-26 14:19:27 +08:00
|
|
|
}
|
|
|
|
book_id = bookResult.BookId
|
2017-04-29 21:28:09 +08:00
|
|
|
}
|
2017-05-26 14:19:27 +08:00
|
|
|
|
2017-04-29 21:28:09 +08:00
|
|
|
content := c.Ctx.Input.RequestBody
|
|
|
|
|
|
|
|
var docs []map[string]interface{}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err := json.Unmarshal(content, &docs)
|
2017-04-29 21:28:09 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
beego.Error(err)
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6003, "数据错误")
|
2017-04-29 21:28:09 +08:00
|
|
|
}
|
2017-05-10 13:43:36 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
for _, item := range docs {
|
|
|
|
if doc_id, ok := item["id"].(float64); ok {
|
|
|
|
doc, err := models.NewDocument().Find(int(doc_id))
|
2017-04-29 21:28:09 +08:00
|
|
|
if err != nil {
|
|
|
|
beego.Error(err)
|
2018-01-26 17:17:38 +08:00
|
|
|
continue
|
2017-04-29 21:28:09 +08:00
|
|
|
}
|
2017-05-26 14:19:27 +08:00
|
|
|
if doc.BookId != book_id {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Info("%s", "权限错误")
|
|
|
|
continue
|
2017-04-29 21:28:09 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
sort, ok := item["sort"].(float64)
|
2017-04-29 21:28:09 +08:00
|
|
|
if !ok {
|
2018-01-26 17:17:38 +08:00
|
|
|
beego.Info("排序数字转换失败 => ", item)
|
2017-04-29 21:28:09 +08:00
|
|
|
continue
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
parent_id, ok := item["parent"].(float64)
|
2017-04-29 21:28:09 +08:00
|
|
|
if !ok {
|
2018-01-26 17:17:38 +08:00
|
|
|
beego.Info("父分类转换失败 => ", item)
|
2017-04-29 21:28:09 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parent_id > 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
if parent, err := models.NewDocument().Find(int(parent_id)); err != nil || parent.BookId != book_id {
|
2017-04-29 21:28:09 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doc.OrderSort = int(sort)
|
|
|
|
doc.ParentId = int(parent_id)
|
|
|
|
if err := doc.InsertOrUpdate(); err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
fmt.Printf("%s", err.Error())
|
2017-04-29 21:28:09 +08:00
|
|
|
beego.Error(err)
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
|
|
|
fmt.Printf("文档ID转换失败 => %+v", item)
|
2017-04-29 21:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok")
|
2017-04-29 21:28:09 +08:00
|
|
|
}
|
|
|
|
|
2018-11-06 18:38:18 +08:00
|
|
|
func (c *BookController) Team() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "book/team.tpl"
|
|
|
|
|
|
|
|
key := c.Ctx.Input.Param(":key")
|
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
|
|
|
|
|
|
|
if key == "" {
|
|
|
|
c.ShowErrorPage(404, "项目不存在或已删除")
|
|
|
|
}
|
|
|
|
|
|
|
|
book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
|
|
|
c.ShowErrorPage(403, "权限不足")
|
|
|
|
}
|
|
|
|
c.ShowErrorPage(500, "系统错误")
|
|
|
|
}
|
2018-11-26 18:54:50 +08:00
|
|
|
//如果不是创始人也不是管理员则不能操作
|
|
|
|
if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
2018-11-06 18:38:18 +08:00
|
|
|
c.Data["Model"] = book
|
|
|
|
|
|
|
|
members, totalCount, err := models.NewTeamRelationship().FindByBookToPager(book.BookId, pageIndex, conf.PageSize)
|
|
|
|
|
|
|
|
if totalCount > 0 {
|
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
|
|
|
|
c.Data["PageHtml"] = pager.HtmlPages()
|
|
|
|
} else {
|
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(members)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Data["Result"] = template.JS("[]")
|
|
|
|
} else {
|
|
|
|
c.Data["Result"] = template.JS(string(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BookController) TeamAdd() {
|
|
|
|
c.Prepare()
|
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
teamId, _ := c.GetInt("teamId")
|
|
|
|
|
|
|
|
book, err := c.IsPermission()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(500, err.Error())
|
|
|
|
}
|
2018-11-26 18:54:50 +08:00
|
|
|
//如果不是创始人也不是管理员则不能操作
|
|
|
|
if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
2018-11-12 21:01:59 +08:00
|
|
|
_, err = models.NewTeam().First(teamId, "team_id")
|
|
|
|
if err != nil {
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.JsonResult(500, "团队不存在")
|
|
|
|
}
|
|
|
|
c.JsonResult(5002, err.Error())
|
|
|
|
}
|
|
|
|
if _, err := models.NewTeamRelationship().FindByBookId(book.BookId, teamId); err == nil {
|
|
|
|
c.JsonResult(5003, "团队已加入当前项目")
|
|
|
|
}
|
|
|
|
teamRel := models.NewTeamRelationship()
|
|
|
|
teamRel.BookId = book.BookId
|
|
|
|
teamRel.TeamId = teamId
|
|
|
|
err = teamRel.Save()
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(5004, "加入项目失败")
|
|
|
|
}
|
|
|
|
teamRel.Include()
|
|
|
|
|
|
|
|
c.JsonResult(0, "OK", teamRel)
|
2018-11-06 18:38:18 +08:00
|
|
|
}
|
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
//删除项目的团队.
|
2018-11-06 18:38:18 +08:00
|
|
|
func (c *BookController) TeamDelete() {
|
|
|
|
c.Prepare()
|
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
teamId, _ := c.GetInt("teamId")
|
|
|
|
|
2018-11-15 18:31:02 +08:00
|
|
|
if teamId <= 0 {
|
2018-11-20 20:36:14 +08:00
|
|
|
c.JsonResult(5001, "参数错误")
|
2018-11-15 18:31:02 +08:00
|
|
|
}
|
2018-11-12 21:01:59 +08:00
|
|
|
book, err := c.IsPermission()
|
|
|
|
|
|
|
|
if err != nil {
|
2018-11-15 18:31:02 +08:00
|
|
|
c.JsonResult(5002, err.Error())
|
2018-11-12 21:01:59 +08:00
|
|
|
}
|
2018-11-26 18:54:50 +08:00
|
|
|
//如果不是创始人也不是管理员则不能操作
|
|
|
|
if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
|
|
|
|
c.Abort("403")
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
err = models.NewTeamRelationship().DeleteByBookId(book.BookId, teamId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == orm.ErrNoRows {
|
2018-11-15 18:31:02 +08:00
|
|
|
c.JsonResult(5003, "团队未加入项目")
|
2018-11-12 21:01:59 +08:00
|
|
|
}
|
2018-11-15 18:31:02 +08:00
|
|
|
c.JsonResult(5004, err.Error())
|
2018-11-12 21:01:59 +08:00
|
|
|
}
|
2018-11-06 18:38:18 +08:00
|
|
|
c.JsonResult(0, "OK")
|
|
|
|
}
|
|
|
|
|
2018-11-20 20:36:14 +08:00
|
|
|
//团队搜索.
|
2018-11-06 18:38:18 +08:00
|
|
|
func (c *BookController) TeamSearch() {
|
|
|
|
c.Prepare()
|
|
|
|
|
2018-11-08 20:08:30 +08:00
|
|
|
keyword := strings.TrimSpace(c.GetString("q"))
|
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
book, err := c.IsPermission()
|
2018-11-08 20:08:30 +08:00
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(500, err.Error())
|
|
|
|
}
|
2018-12-24 15:23:17 +08:00
|
|
|
keyword = sqltil.EscapeLike(keyword)
|
2018-11-12 21:01:59 +08:00
|
|
|
searchResult, err := models.NewTeamRelationship().FindNotJoinBookByBookIdentify(book.BookId, keyword, 10)
|
2018-11-08 20:08:30 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-11-20 20:36:14 +08:00
|
|
|
c.JsonResult(500, err.Error(), searchResult)
|
|
|
|
}
|
|
|
|
c.JsonResult(0, "OK", searchResult)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:03:16 +08:00
|
|
|
//项目空间搜索.
|
2018-11-20 20:36:14 +08:00
|
|
|
func (c *BookController) ItemsetsSearch() {
|
|
|
|
c.Prepare()
|
|
|
|
|
|
|
|
keyword := strings.TrimSpace(c.GetString("q"))
|
2018-12-24 15:23:17 +08:00
|
|
|
keyword = sqltil.EscapeLike(keyword)
|
2018-11-20 20:36:14 +08:00
|
|
|
|
|
|
|
searchResult, err := models.NewItemsets().FindItemsetsByName(keyword, 10)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonResult(500, err.Error(), searchResult)
|
2018-11-08 20:08:30 +08:00
|
|
|
}
|
|
|
|
c.JsonResult(0, "OK", searchResult)
|
|
|
|
|
2018-11-06 18:38:18 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookController) IsPermission() (*models.BookResult, error) {
|
2017-04-25 20:05:59 +08:00
|
|
|
identify := c.GetString("identify")
|
2017-06-09 13:25:39 +08:00
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
if identify == "" {
|
|
|
|
return nil, errors.New("参数错误")
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
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 {
|
2018-01-26 17:17:38 +08:00
|
|
|
return book, errors.New("权限不足")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
2018-01-26 17:17:38 +08:00
|
|
|
return book, errors.New("项目不存在")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
return book, err
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
|
|
|
if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
|
2018-01-26 17:17:38 +08:00
|
|
|
return book, errors.New("权限不足")
|
2017-04-25 20:05:59 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
return book, nil
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|