BookController.go 25 KB

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