BookController.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "html/template"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/mindoc-org/mindoc/utils/sqltil"
  14. "net/http"
  15. "github.com/astaxie/beego"
  16. "github.com/astaxie/beego/logs"
  17. "github.com/astaxie/beego/orm"
  18. "github.com/mindoc-org/mindoc/conf"
  19. "github.com/mindoc-org/mindoc/graphics"
  20. "github.com/mindoc-org/mindoc/models"
  21. "github.com/mindoc-org/mindoc/utils"
  22. "github.com/mindoc-org/mindoc/utils/pagination"
  23. "gopkg.in/russross/blackfriday.v2"
  24. )
  25. type BookController struct {
  26. BaseController
  27. }
  28. func (c *BookController) Index() {
  29. c.Prepare()
  30. c.TplName = "book/index.tpl"
  31. pageIndex, _ := c.GetInt("page", 1)
  32. books, totalCount, err := models.NewBook().FindToPager(pageIndex, conf.PageSize, c.Member.MemberId)
  33. if err != nil {
  34. logs.Error("BookController.Index => ", err)
  35. c.Abort("500")
  36. }
  37. for i, book := range books {
  38. books[i].Description = utils.StripTags(string(blackfriday.Run([]byte(book.Description))))
  39. books[i].ModifyTime = book.ModifyTime.Local()
  40. books[i].CreateTime = book.CreateTime.Local()
  41. }
  42. if totalCount > 0 {
  43. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  44. c.Data["PageHtml"] = pager.HtmlPages()
  45. } else {
  46. c.Data["PageHtml"] = ""
  47. }
  48. b, err := json.Marshal(books)
  49. if err != nil || len(books) <= 0 {
  50. c.Data["Result"] = template.JS("[]")
  51. } else {
  52. c.Data["Result"] = template.JS(string(b))
  53. }
  54. if itemsets, err := models.NewItemsets().First(1); err == nil {
  55. c.Data["Item"] = itemsets
  56. }
  57. }
  58. // Dashboard 项目概要 .
  59. func (c *BookController) Dashboard() {
  60. c.Prepare()
  61. c.TplName = "book/dashboard.tpl"
  62. key := c.Ctx.Input.Param(":key")
  63. if key == "" {
  64. c.Abort("404")
  65. }
  66. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  67. if err != nil {
  68. if err == models.ErrPermissionDenied {
  69. c.Abort("403")
  70. }
  71. c.Abort("500")
  72. return
  73. }
  74. c.Data["Description"] = template.HTML(blackfriday.Run([]byte(book.Description)))
  75. c.Data["Model"] = *book
  76. }
  77. // Setting 项目设置 .
  78. func (c *BookController) Setting() {
  79. c.Prepare()
  80. c.TplName = "book/setting.tpl"
  81. key := c.Ctx.Input.Param(":key")
  82. if key == "" {
  83. c.Abort("404")
  84. }
  85. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  86. if err != nil {
  87. if err == orm.ErrNoRows {
  88. c.Abort("404")
  89. }
  90. if err == models.ErrPermissionDenied {
  91. c.Abort("403")
  92. }
  93. c.Abort("500")
  94. return
  95. }
  96. //如果不是创始人也不是管理员则不能操作
  97. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  98. c.Abort("403")
  99. }
  100. if book.PrivateToken != "" {
  101. book.PrivateToken = conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  102. }
  103. c.Data["Model"] = book
  104. }
  105. //保存项目信息
  106. func (c *BookController) SaveBook() {
  107. bookResult, err := c.IsPermission()
  108. if err != nil {
  109. c.JsonResult(6001, err.Error())
  110. }
  111. book, err := models.NewBook().Find(bookResult.BookId)
  112. if err != nil {
  113. logs.Error("SaveBook => ", err)
  114. c.JsonResult(6002, err.Error())
  115. }
  116. bookName := strings.TrimSpace(c.GetString("book_name"))
  117. description := strings.TrimSpace(c.GetString("description", ""))
  118. commentStatus := c.GetString("comment_status")
  119. //tag := strings.TrimSpace(c.GetString("label"))
  120. editor := strings.TrimSpace(c.GetString("editor"))
  121. autoRelease := strings.TrimSpace(c.GetString("auto_release")) == "on"
  122. publisher := strings.TrimSpace(c.GetString("publisher"))
  123. historyCount, _ := c.GetInt("history_count", 0)
  124. isDownload := strings.TrimSpace(c.GetString("is_download")) == "on"
  125. enableShare := strings.TrimSpace(c.GetString("enable_share")) == "on"
  126. isUseFirstDocument := strings.TrimSpace(c.GetString("is_use_first_document")) == "on"
  127. autoSave := strings.TrimSpace(c.GetString("auto_save")) == "on"
  128. itemId, _ := c.GetInt("itemId")
  129. if strings.Count(description, "") > 500 {
  130. c.JsonResult(6004, "项目描述不能大于500字")
  131. }
  132. if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
  133. commentStatus = "closed"
  134. }
  135. if !models.NewItemsets().Exist(itemId) {
  136. c.JsonResult(6006, "项目空间不存在")
  137. }
  138. if editor != "markdown" && editor != "html" {
  139. editor = "markdown"
  140. }
  141. book.BookName = bookName
  142. book.Description = description
  143. book.CommentStatus = commentStatus
  144. book.Publisher = publisher
  145. //book.Label = tag
  146. book.Editor = editor
  147. book.HistoryCount = historyCount
  148. book.IsDownload = 0
  149. book.BookPassword = strings.TrimSpace(c.GetString("bPassword"))
  150. book.ItemId = itemId
  151. if autoRelease {
  152. book.AutoRelease = 1
  153. } else {
  154. book.AutoRelease = 0
  155. }
  156. if isDownload {
  157. book.IsDownload = 0
  158. } else {
  159. book.IsDownload = 1
  160. }
  161. if enableShare {
  162. book.IsEnableShare = 0
  163. } else {
  164. book.IsEnableShare = 1
  165. }
  166. if isUseFirstDocument {
  167. book.IsUseFirstDocument = 1
  168. } else {
  169. book.IsUseFirstDocument = 0
  170. }
  171. if autoSave {
  172. book.AutoSave = 1
  173. } else {
  174. book.AutoSave = 0
  175. }
  176. if err := book.Update(); err != nil {
  177. c.JsonResult(6006, "保存失败")
  178. }
  179. bookResult.BookName = bookName
  180. bookResult.Description = description
  181. bookResult.CommentStatus = commentStatus
  182. beego.Info("用户 [", c.Member.Account, "] 修改了项目 ->", book)
  183. c.JsonResult(0, "ok", bookResult)
  184. }
  185. //设置项目私有状态.
  186. func (c *BookController) PrivatelyOwned() {
  187. status := c.GetString("status")
  188. if status != "open" && status != "close" {
  189. c.JsonResult(6003, "参数错误")
  190. }
  191. state := 0
  192. if status == "open" {
  193. state = 0
  194. } else {
  195. state = 1
  196. }
  197. bookResult, err := c.IsPermission()
  198. if err != nil {
  199. c.JsonResult(6001, err.Error())
  200. return
  201. }
  202. //只有创始人才能变更私有状态
  203. if bookResult.RoleId != conf.BookFounder {
  204. c.JsonResult(6002, "权限不足")
  205. }
  206. book, err := models.NewBook().Find(bookResult.BookId)
  207. if err != nil {
  208. c.JsonResult(6005, "项目不存在")
  209. return
  210. }
  211. book.PrivatelyOwned = state
  212. err = book.Update()
  213. if err != nil {
  214. logs.Error("PrivatelyOwned => ", err)
  215. c.JsonResult(6004, "保存失败")
  216. }
  217. beego.Info("用户 【", c.Member.Account, "]修改了项目权限 ->", state)
  218. c.JsonResult(0, "ok")
  219. }
  220. // Transfer 转让项目.
  221. func (c *BookController) Transfer() {
  222. c.Prepare()
  223. account := c.GetString("account")
  224. if account == "" {
  225. c.JsonResult(6004, "接受者账号不能为空")
  226. }
  227. member, err := models.NewMember().FindByAccount(account)
  228. if err != nil {
  229. logs.Error("FindByAccount => ", err)
  230. c.JsonResult(6005, "接受用户不存在")
  231. }
  232. if member.Status != 0 {
  233. c.JsonResult(6006, "接受用户已被禁用")
  234. }
  235. if member.MemberId == c.Member.MemberId {
  236. c.JsonResult(6007, "不能转让给自己")
  237. }
  238. bookResult, err := c.IsPermission()
  239. if err != nil {
  240. c.JsonResult(6001, err.Error())
  241. return
  242. }
  243. err = models.NewRelationship().Transfer(bookResult.BookId, c.Member.MemberId, member.MemberId)
  244. if err != nil {
  245. logs.Error("转让项目失败 -> ", err)
  246. c.JsonResult(6008, err.Error())
  247. }
  248. c.JsonResult(0, "ok")
  249. }
  250. //上传项目封面.
  251. func (c *BookController) UploadCover() {
  252. bookResult, err := c.IsPermission()
  253. if err != nil {
  254. c.JsonResult(6001, err.Error())
  255. return
  256. }
  257. book, err := models.NewBook().Find(bookResult.BookId)
  258. if err != nil {
  259. logs.Error("SaveBook => ", err)
  260. c.JsonResult(6002, err.Error())
  261. }
  262. file, moreFile, err := c.GetFile("image-file")
  263. if err != nil {
  264. logs.Error("获取上传文件失败 ->", err.Error())
  265. c.JsonResult(500, "读取文件异常")
  266. return
  267. }
  268. defer file.Close()
  269. ext := filepath.Ext(moreFile.Filename)
  270. if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") {
  271. c.JsonResult(500, "不支持的图片格式")
  272. }
  273. x1, _ := strconv.ParseFloat(c.GetString("x"), 10)
  274. y1, _ := strconv.ParseFloat(c.GetString("y"), 10)
  275. w1, _ := strconv.ParseFloat(c.GetString("width"), 10)
  276. h1, _ := strconv.ParseFloat(c.GetString("height"), 10)
  277. x := int(x1)
  278. y := int(y1)
  279. width := int(w1)
  280. height := int(h1)
  281. fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  282. //附件路径按照项目组织
  283. filePath := filepath.Join("uploads", book.Identify, "images", fileName+ext)
  284. path := filepath.Dir(filePath)
  285. os.MkdirAll(path, os.ModePerm)
  286. err = c.SaveToFile("image-file", filePath)
  287. if err != nil {
  288. logs.Error("", err)
  289. c.JsonResult(500, "图片保存失败")
  290. }
  291. defer func(filePath string) {
  292. os.Remove(filePath)
  293. }(filePath)
  294. //剪切图片
  295. subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height)
  296. if err != nil {
  297. logs.Error("graphics.ImageCopyFromFile => ", err)
  298. c.JsonResult(500, "图片剪切")
  299. }
  300. filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
  301. //生成缩略图并保存到磁盘
  302. err = graphics.ImageResizeSaveFile(subImg, 350, 460, filePath)
  303. if err != nil {
  304. logs.Error("ImageResizeSaveFile => ", err.Error())
  305. c.JsonResult(500, "保存图片失败")
  306. }
  307. url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  308. if strings.HasPrefix(url, "//") {
  309. url = string(url[1:])
  310. }
  311. oldCover := book.Cover
  312. book.Cover = conf.URLForWithCdnImage(url)
  313. if err := book.Update(); err != nil {
  314. c.JsonResult(6001, "保存图片失败")
  315. }
  316. //如果原封面不是默认封面则删除
  317. if oldCover != conf.GetDefaultCover() {
  318. os.Remove("." + oldCover)
  319. }
  320. beego.Info("用户[", c.Member.Account, "]上传了项目封面 ->", book.BookName, book.BookId, book.Cover)
  321. c.JsonResult(0, "ok", url)
  322. }
  323. // Users 用户列表.
  324. func (c *BookController) Users() {
  325. c.Prepare()
  326. c.TplName = "book/users.tpl"
  327. key := c.Ctx.Input.Param(":key")
  328. pageIndex, _ := c.GetInt("page", 1)
  329. if key == "" {
  330. c.ShowErrorPage(404, "项目不存在或已删除")
  331. }
  332. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  333. if err != nil {
  334. if err == models.ErrPermissionDenied {
  335. c.Abort("403")
  336. }
  337. c.Abort("500")
  338. return
  339. }
  340. //如果不是创始人也不是管理员则不能操作
  341. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  342. c.Abort("403")
  343. }
  344. c.Data["Model"] = *book
  345. members, totalCount, err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId, pageIndex, conf.PageSize)
  346. if totalCount > 0 {
  347. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  348. c.Data["PageHtml"] = pager.HtmlPages()
  349. } else {
  350. c.Data["PageHtml"] = ""
  351. }
  352. b, err := json.Marshal(members)
  353. if err != nil {
  354. c.Data["Result"] = template.JS("[]")
  355. } else {
  356. c.Data["Result"] = template.JS(string(b))
  357. }
  358. }
  359. // Create 创建项目.
  360. func (c *BookController) Create() {
  361. if c.Ctx.Input.IsPost() {
  362. bookName := strings.TrimSpace(c.GetString("book_name", ""))
  363. identify := strings.TrimSpace(c.GetString("identify", ""))
  364. description := strings.TrimSpace(c.GetString("description", ""))
  365. privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned"))
  366. commentStatus := c.GetString("comment_status")
  367. itemId, _ := c.GetInt("itemId")
  368. if bookName == "" {
  369. c.JsonResult(6001, "项目名称不能为空")
  370. }
  371. if identify == "" {
  372. c.JsonResult(6002, "项目标识不能为空")
  373. }
  374. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, identify); !ok || err != nil {
  375. c.JsonResult(6003, "项目标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  376. }
  377. if strings.Count(identify, "") > 50 {
  378. c.JsonResult(6004, "文档标识不能超过50字")
  379. }
  380. if strings.Count(description, "") > 500 {
  381. c.JsonResult(6004, "项目描述不能大于500字")
  382. }
  383. if privatelyOwned != 0 && privatelyOwned != 1 {
  384. privatelyOwned = 1
  385. }
  386. if !models.NewItemsets().Exist(itemId) {
  387. c.JsonResult(6005, "项目空间不存在")
  388. }
  389. if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
  390. commentStatus = "closed"
  391. }
  392. book := models.NewBook()
  393. book.Cover = conf.GetDefaultCover()
  394. //如果客户端上传了项目封面则直接保存
  395. if file, moreFile, err := c.GetFile("image-file"); err == nil {
  396. defer file.Close()
  397. ext := filepath.Ext(moreFile.Filename)
  398. //如果上传的是图片
  399. if strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".gif") || strings.EqualFold(ext, ".jpeg") {
  400. fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  401. filePath := filepath.Join("uploads", time.Now().Format("200601"), fileName+ext)
  402. path := filepath.Dir(filePath)
  403. os.MkdirAll(path, os.ModePerm)
  404. if err := c.SaveToFile("image-file", filePath); err == nil {
  405. url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  406. if strings.HasPrefix(url, "//") {
  407. url = string(url[1:])
  408. }
  409. book.Cover = url
  410. }
  411. }
  412. }
  413. if books, _ := book.FindByField("identify", identify, "book_id"); len(books) > 0 {
  414. c.JsonResult(6006, "项目标识已存在")
  415. }
  416. book.BookName = bookName
  417. book.Description = description
  418. book.CommentCount = 0
  419. book.PrivatelyOwned = privatelyOwned
  420. book.CommentStatus = commentStatus
  421. book.Identify = identify
  422. book.DocCount = 0
  423. book.MemberId = c.Member.MemberId
  424. book.Version = time.Now().Unix()
  425. book.IsEnableShare = 0
  426. book.IsUseFirstDocument = 1
  427. book.IsDownload = 1
  428. book.AutoRelease = 0
  429. book.ItemId = itemId
  430. book.Editor = "markdown"
  431. book.Theme = "default"
  432. if err := book.Insert(); err != nil {
  433. logs.Error("Insert => ", err)
  434. c.JsonResult(6005, "保存项目失败")
  435. }
  436. bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
  437. if err != nil {
  438. beego.Error(err)
  439. }
  440. beego.Info("用户[", c.Member.Account, "]创建了项目 ->", book)
  441. c.JsonResult(0, "ok", bookResult)
  442. }
  443. c.JsonResult(6001, "error")
  444. }
  445. //复制项目
  446. func (c *BookController) Copy() {
  447. if c.Ctx.Input.IsPost() {
  448. //检查是否有复制项目的权限
  449. if _, err := c.IsPermission(); err != nil {
  450. c.JsonResult(500, err.Error())
  451. }
  452. identify := strings.TrimSpace(c.GetString("identify", ""))
  453. if identify == "" {
  454. c.JsonResult(6001, "参数错误")
  455. }
  456. book := models.NewBook()
  457. err := book.Copy(identify)
  458. if err != nil {
  459. c.JsonResult(6002, "复制项目出错")
  460. } else {
  461. bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
  462. if err != nil {
  463. beego.Error("查询失败")
  464. }
  465. c.JsonResult(0, "ok", bookResult)
  466. }
  467. }
  468. }
  469. //导入zip压缩包
  470. func (c *BookController) Import() {
  471. file, moreFile, err := c.GetFile("import-file")
  472. if err == http.ErrMissingFile {
  473. c.JsonResult(6003, "没有发现需要上传的文件")
  474. }
  475. defer file.Close()
  476. bookName := strings.TrimSpace(c.GetString("book_name"))
  477. identify := strings.TrimSpace(c.GetString("identify"))
  478. description := strings.TrimSpace(c.GetString("description", ""))
  479. privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned"))
  480. itemId, _ := c.GetInt("itemId")
  481. if bookName == "" {
  482. c.JsonResult(6001, "项目名称不能为空")
  483. }
  484. if len([]rune(bookName)) > 500 {
  485. c.JsonResult(6002, "项目名称不能大于500字")
  486. }
  487. if identify == "" {
  488. c.JsonResult(6002, "项目标识不能为空")
  489. }
  490. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, identify); !ok || err != nil {
  491. c.JsonResult(6003, "项目标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  492. }
  493. if !models.NewItemsets().Exist(itemId) {
  494. c.JsonResult(6007, "项目空间不存在")
  495. }
  496. if strings.Count(identify, "") > 50 {
  497. c.JsonResult(6004, "文档标识不能超过50字")
  498. }
  499. ext := filepath.Ext(moreFile.Filename)
  500. if !strings.EqualFold(ext, ".zip") {
  501. c.JsonResult(6004, "不支持的文件类型")
  502. }
  503. if books, _ := models.NewBook().FindByField("identify", identify, "book_id"); len(books) > 0 {
  504. c.JsonResult(6006, "项目标识已存在")
  505. }
  506. tempPath := filepath.Join(os.TempDir(), c.CruSession.SessionID())
  507. os.MkdirAll(tempPath, 0766)
  508. tempPath = filepath.Join(tempPath, moreFile.Filename)
  509. err = c.SaveToFile("import-file", tempPath)
  510. book := models.NewBook()
  511. book.MemberId = c.Member.MemberId
  512. book.Cover = conf.GetDefaultCover()
  513. book.BookName = bookName
  514. book.Description = description
  515. book.CommentCount = 0
  516. book.PrivatelyOwned = privatelyOwned
  517. book.CommentStatus = "closed"
  518. book.Identify = identify
  519. book.DocCount = 0
  520. book.MemberId = c.Member.MemberId
  521. book.Version = time.Now().Unix()
  522. book.ItemId = itemId
  523. book.Editor = "markdown"
  524. book.Theme = "default"
  525. go book.ImportBook(tempPath)
  526. beego.Info("用户[", c.Member.Account, "]导入了项目 ->", book)
  527. c.JsonResult(0, "项目正在后台转换中,请稍后查看")
  528. }
  529. // CreateToken 创建访问来令牌.
  530. //func (c *BookController) CreateToken() {
  531. //
  532. // action := c.GetString("action")
  533. //
  534. // bookResult, err := c.IsPermission()
  535. //
  536. // if err != nil {
  537. // if err == models.ErrPermissionDenied {
  538. // c.JsonResult(403, "权限不足")
  539. // }
  540. // if err == orm.ErrNoRows {
  541. // c.JsonResult(404, "项目不存在")
  542. // }
  543. // logs.Error("生成阅读令牌失败 =>", err)
  544. // c.JsonResult(6002, err.Error())
  545. // }
  546. // book := models.NewBook()
  547. //
  548. // if _, err := book.Find(bookResult.BookId); err != nil {
  549. // c.JsonResult(6001, "项目不存在")
  550. // }
  551. // if action == "create" {
  552. // if bookResult.PrivatelyOwned == 0 {
  553. // c.JsonResult(6001, "公开项目不能创建阅读令牌")
  554. // }
  555. //
  556. // book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  557. // if err := book.Update(); err != nil {
  558. // logs.Error("生成阅读令牌失败 => ", err)
  559. // c.JsonResult(6003, "生成阅读令牌失败")
  560. // }
  561. // beego.Info("用户[", c.Member.Account, "]创建项目令牌 ->", book.PrivateToken)
  562. // c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  563. // } else {
  564. // book.PrivateToken = ""
  565. // if err := book.Update(); err != nil {
  566. // logs.Error("CreateToken => ", err)
  567. // c.JsonResult(6004, "删除令牌失败")
  568. // }
  569. // beego.Info("用户[", c.Member.Account, "]创建项目令牌 ->", book.PrivateToken)
  570. // c.JsonResult(0, "ok", "")
  571. // }
  572. //}
  573. // Delete 删除项目.
  574. func (c *BookController) Delete() {
  575. c.Prepare()
  576. bookResult, err := c.IsPermission()
  577. if err != nil {
  578. c.JsonResult(6001, err.Error())
  579. return
  580. }
  581. if bookResult.RoleId != conf.BookFounder {
  582. c.JsonResult(6002, "只有创始人才能删除项目")
  583. }
  584. err = models.NewBook().ThoroughDeleteBook(bookResult.BookId)
  585. if err == orm.ErrNoRows {
  586. c.JsonResult(6002, "项目不存在")
  587. }
  588. if err != nil {
  589. logs.Error("删除项目 => ", err)
  590. c.JsonResult(6003, "删除失败")
  591. }
  592. beego.Info("用户[", c.Member.Account, "]删除了项目 ->", bookResult)
  593. c.JsonResult(0, "ok")
  594. }
  595. //发布项目.
  596. func (c *BookController) Release() {
  597. c.Prepare()
  598. identify := c.GetString("identify")
  599. bookId := 0
  600. if c.Member.IsAdministrator() {
  601. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  602. if err != nil {
  603. beego.Error("发布文档失败 ->", err)
  604. c.JsonResult(6003, "文档不存在")
  605. return
  606. }
  607. bookId = book.BookId
  608. } else {
  609. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  610. if err != nil {
  611. if err == models.ErrPermissionDenied {
  612. c.JsonResult(6001, "权限不足")
  613. }
  614. if err == orm.ErrNoRows {
  615. c.JsonResult(6002, "项目不存在")
  616. }
  617. beego.Error(err)
  618. c.JsonResult(6003, "未知错误")
  619. }
  620. if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder && book.RoleId != conf.BookEditor {
  621. c.JsonResult(6003, "权限不足")
  622. }
  623. bookId = book.BookId
  624. }
  625. go models.NewBook().ReleaseContent(bookId)
  626. c.JsonResult(0, "发布任务已推送到任务队列,稍后将在后台执行。")
  627. }
  628. //文档排序.
  629. func (c *BookController) SaveSort() {
  630. c.Prepare()
  631. identify := c.Ctx.Input.Param(":key")
  632. if identify == "" {
  633. c.Abort("404")
  634. }
  635. bookId := 0
  636. if c.Member.IsAdministrator() {
  637. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  638. if err != nil || book == nil {
  639. c.JsonResult(6001,"项目不存在")
  640. return
  641. }
  642. bookId = book.BookId
  643. } else {
  644. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  645. if err != nil {
  646. beego.Error("DocumentController.Edit => ", err)
  647. c.Abort("403")
  648. }
  649. if bookResult.RoleId == conf.BookObserver {
  650. c.JsonResult(6002, "项目不存在或权限不足")
  651. }
  652. bookId = bookResult.BookId
  653. }
  654. content := c.Ctx.Input.RequestBody
  655. var docs []map[string]interface{}
  656. err := json.Unmarshal(content, &docs)
  657. if err != nil {
  658. beego.Error(err)
  659. c.JsonResult(6003, "数据错误")
  660. }
  661. for _, item := range docs {
  662. if docId, ok := item["id"].(float64); ok {
  663. doc, err := models.NewDocument().Find(int(docId))
  664. if err != nil {
  665. beego.Error(err)
  666. continue
  667. }
  668. if doc.BookId != bookId {
  669. logs.Info("%s", "权限错误")
  670. continue
  671. }
  672. sort, ok := item["sort"].(float64)
  673. if !ok {
  674. beego.Info("排序数字转换失败 => ", item)
  675. continue
  676. }
  677. parentId, ok := item["parent"].(float64)
  678. if !ok {
  679. beego.Info("父分类转换失败 => ", item)
  680. continue
  681. }
  682. if parentId > 0 {
  683. if parent, err := models.NewDocument().Find(int(parentId)); err != nil || parent.BookId != bookId {
  684. continue
  685. }
  686. }
  687. doc.OrderSort = int(sort)
  688. doc.ParentId = int(parentId)
  689. if err := doc.InsertOrUpdate(); err != nil {
  690. fmt.Printf("%s", err.Error())
  691. beego.Error(err)
  692. }
  693. } else {
  694. fmt.Printf("文档ID转换失败 => %+v", item)
  695. }
  696. }
  697. c.JsonResult(0, "ok")
  698. }
  699. func (c *BookController) Team() {
  700. c.Prepare()
  701. c.TplName = "book/team.tpl"
  702. key := c.Ctx.Input.Param(":key")
  703. pageIndex, _ := c.GetInt("page", 1)
  704. if key == "" {
  705. c.ShowErrorPage(404, "项目不存在或已删除")
  706. }
  707. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  708. if err != nil || book == nil {
  709. if err == models.ErrPermissionDenied {
  710. c.ShowErrorPage(403, "权限不足")
  711. }
  712. c.ShowErrorPage(500, "系统错误")
  713. return
  714. }
  715. //如果不是创始人也不是管理员则不能操作
  716. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  717. c.Abort("403")
  718. }
  719. c.Data["Model"] = book
  720. members, totalCount, err := models.NewTeamRelationship().FindByBookToPager(book.BookId, pageIndex, conf.PageSize)
  721. if totalCount > 0 {
  722. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  723. c.Data["PageHtml"] = pager.HtmlPages()
  724. } else {
  725. c.Data["PageHtml"] = ""
  726. }
  727. b, err := json.Marshal(members)
  728. if err != nil {
  729. c.Data["Result"] = template.JS("[]")
  730. } else {
  731. c.Data["Result"] = template.JS(string(b))
  732. }
  733. }
  734. func (c *BookController) TeamAdd() {
  735. c.Prepare()
  736. teamId, _ := c.GetInt("teamId")
  737. book, err := c.IsPermission()
  738. if err != nil {
  739. c.JsonResult(500, err.Error())
  740. return
  741. }
  742. //如果不是创始人也不是管理员则不能操作
  743. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  744. c.Abort("403")
  745. }
  746. _, err = models.NewTeam().First(teamId, "team_id")
  747. if err != nil {
  748. if err == orm.ErrNoRows {
  749. c.JsonResult(500, "团队不存在")
  750. }
  751. c.JsonResult(5002, err.Error())
  752. }
  753. if _, err := models.NewTeamRelationship().FindByBookId(book.BookId, teamId); err == nil {
  754. c.JsonResult(5003, "团队已加入当前项目")
  755. }
  756. teamRel := models.NewTeamRelationship()
  757. teamRel.BookId = book.BookId
  758. teamRel.TeamId = teamId
  759. err = teamRel.Save()
  760. if err != nil {
  761. c.JsonResult(5004, "加入项目失败")
  762. return
  763. }
  764. teamRel.Include()
  765. c.JsonResult(0, "OK", teamRel)
  766. }
  767. //删除项目的团队.
  768. func (c *BookController) TeamDelete() {
  769. c.Prepare()
  770. teamId, _ := c.GetInt("teamId")
  771. if teamId <= 0 {
  772. c.JsonResult(5001, "参数错误")
  773. }
  774. book, err := c.IsPermission()
  775. if err != nil {
  776. c.JsonResult(5002, err.Error())
  777. return
  778. }
  779. //如果不是创始人也不是管理员则不能操作
  780. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  781. c.Abort("403")
  782. }
  783. err = models.NewTeamRelationship().DeleteByBookId(book.BookId, teamId)
  784. if err != nil {
  785. if err == orm.ErrNoRows {
  786. c.JsonResult(5003, "团队未加入项目")
  787. }
  788. c.JsonResult(5004, err.Error())
  789. }
  790. c.JsonResult(0, "OK")
  791. }
  792. //团队搜索.
  793. func (c *BookController) TeamSearch() {
  794. c.Prepare()
  795. keyword := strings.TrimSpace(c.GetString("q"))
  796. book, err := c.IsPermission()
  797. if err != nil {
  798. c.JsonResult(500, err.Error())
  799. }
  800. keyword = sqltil.EscapeLike(keyword)
  801. searchResult, err := models.NewTeamRelationship().FindNotJoinBookByBookIdentify(book.BookId, keyword, 10)
  802. if err != nil {
  803. c.JsonResult(500, err.Error(), searchResult)
  804. }
  805. c.JsonResult(0, "OK", searchResult)
  806. }
  807. //项目空间搜索.
  808. func (c *BookController) ItemsetsSearch() {
  809. c.Prepare()
  810. keyword := strings.TrimSpace(c.GetString("q"))
  811. keyword = sqltil.EscapeLike(keyword)
  812. searchResult, err := models.NewItemsets().FindItemsetsByName(keyword, 10)
  813. if err != nil {
  814. c.JsonResult(500, err.Error(), searchResult)
  815. }
  816. c.JsonResult(0, "OK", searchResult)
  817. }
  818. func (c *BookController) IsPermission() (*models.BookResult, error) {
  819. identify := c.GetString("identify")
  820. if identify == "" {
  821. return nil, errors.New("参数错误")
  822. }
  823. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  824. if err != nil {
  825. if err == models.ErrPermissionDenied {
  826. return book, errors.New("权限不足")
  827. }
  828. if err == orm.ErrNoRows {
  829. return book, errors.New("项目不存在")
  830. }
  831. return book, err
  832. }
  833. if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  834. return book, errors.New("权限不足")
  835. }
  836. return book, nil
  837. }