BookController.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/lifei6671/mindoc/utils/sqltil"
  7. "html/template"
  8. "os"
  9. "path/filepath"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "net/http"
  15. "github.com/astaxie/beego"
  16. "github.com/astaxie/beego/logs"
  17. "github.com/astaxie/beego/orm"
  18. "github.com/lifei6671/mindoc/conf"
  19. "github.com/lifei6671/mindoc/graphics"
  20. "github.com/lifei6671/mindoc/models"
  21. "github.com/lifei6671/mindoc/utils"
  22. "github.com/lifei6671/mindoc/utils/pagination"
  23. "github.com/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.CommentCount = 0
  425. book.Version = time.Now().Unix()
  426. book.IsEnableShare = 0
  427. book.IsUseFirstDocument = 1
  428. book.IsDownload = 1
  429. book.AutoRelease = 0
  430. book.ItemId = itemId
  431. book.Editor = "markdown"
  432. book.Theme = "default"
  433. if err := book.Insert(); err != nil {
  434. logs.Error("Insert => ", err)
  435. c.JsonResult(6005, "保存项目失败")
  436. }
  437. bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
  438. if err != nil {
  439. beego.Error(err)
  440. }
  441. beego.Info("用户[", c.Member.Account, "]创建了项目 ->", book)
  442. c.JsonResult(0, "ok", bookResult)
  443. }
  444. c.JsonResult(6001, "error")
  445. }
  446. //复制项目
  447. func (c *BookController) Copy() {
  448. if c.Ctx.Input.IsPost() {
  449. //检查是否有复制项目的权限
  450. if _, err := c.IsPermission(); err != nil {
  451. c.JsonResult(500, err.Error())
  452. }
  453. identify := strings.TrimSpace(c.GetString("identify", ""))
  454. if identify == "" {
  455. c.JsonResult(6001, "参数错误")
  456. }
  457. book := models.NewBook()
  458. err := book.Copy(identify)
  459. if err != nil {
  460. c.JsonResult(6002, "复制项目出错")
  461. } else {
  462. bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
  463. if err != nil {
  464. beego.Error("查询失败")
  465. }
  466. c.JsonResult(0, "ok", bookResult)
  467. }
  468. }
  469. }
  470. //导入zip压缩包
  471. func (c *BookController) Import() {
  472. file, moreFile, err := c.GetFile("import-file")
  473. if err == http.ErrMissingFile {
  474. c.JsonResult(6003, "没有发现需要上传的文件")
  475. }
  476. defer file.Close()
  477. bookName := strings.TrimSpace(c.GetString("book_name"))
  478. identify := strings.TrimSpace(c.GetString("identify"))
  479. description := strings.TrimSpace(c.GetString("description", ""))
  480. privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned"))
  481. itemId, _ := c.GetInt("itemId")
  482. if bookName == "" {
  483. c.JsonResult(6001, "项目名称不能为空")
  484. }
  485. if len([]rune(bookName)) > 500 {
  486. c.JsonResult(6002, "项目名称不能大于500字")
  487. }
  488. if identify == "" {
  489. c.JsonResult(6002, "项目标识不能为空")
  490. }
  491. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, identify); !ok || err != nil {
  492. c.JsonResult(6003, "项目标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  493. }
  494. if !models.NewItemsets().Exist(itemId) {
  495. c.JsonResult(6007, "项目空间不存在")
  496. }
  497. if strings.Count(identify, "") > 50 {
  498. c.JsonResult(6004, "文档标识不能超过50字")
  499. }
  500. ext := filepath.Ext(moreFile.Filename)
  501. if !strings.EqualFold(ext, ".zip") {
  502. c.JsonResult(6004, "不支持的文件类型")
  503. }
  504. if books, _ := models.NewBook().FindByField("identify", identify, "book_id"); len(books) > 0 {
  505. c.JsonResult(6006, "项目标识已存在")
  506. }
  507. tempPath := filepath.Join(os.TempDir(), c.CruSession.SessionID())
  508. os.MkdirAll(tempPath, 0766)
  509. tempPath = filepath.Join(tempPath, moreFile.Filename)
  510. err = c.SaveToFile("import-file", tempPath)
  511. book := models.NewBook()
  512. book.MemberId = c.Member.MemberId
  513. book.Cover = conf.GetDefaultCover()
  514. book.BookName = bookName
  515. book.Description = description
  516. book.CommentCount = 0
  517. book.PrivatelyOwned = privatelyOwned
  518. book.CommentStatus = "closed"
  519. book.Identify = identify
  520. book.DocCount = 0
  521. book.MemberId = c.Member.MemberId
  522. book.CommentCount = 0
  523. book.Version = time.Now().Unix()
  524. book.ItemId = itemId
  525. book.Editor = "markdown"
  526. book.Theme = "default"
  527. go book.ImportBook(tempPath)
  528. beego.Info("用户[", c.Member.Account, "]导入了项目 ->", book)
  529. c.JsonResult(0, "项目正在后台转换中,请稍后查看")
  530. }
  531. // CreateToken 创建访问来令牌.
  532. //func (c *BookController) CreateToken() {
  533. //
  534. // action := c.GetString("action")
  535. //
  536. // bookResult, err := c.IsPermission()
  537. //
  538. // if err != nil {
  539. // if err == models.ErrPermissionDenied {
  540. // c.JsonResult(403, "权限不足")
  541. // }
  542. // if err == orm.ErrNoRows {
  543. // c.JsonResult(404, "项目不存在")
  544. // }
  545. // logs.Error("生成阅读令牌失败 =>", err)
  546. // c.JsonResult(6002, err.Error())
  547. // }
  548. // book := models.NewBook()
  549. //
  550. // if _, err := book.Find(bookResult.BookId); err != nil {
  551. // c.JsonResult(6001, "项目不存在")
  552. // }
  553. // if action == "create" {
  554. // if bookResult.PrivatelyOwned == 0 {
  555. // c.JsonResult(6001, "公开项目不能创建阅读令牌")
  556. // }
  557. //
  558. // book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  559. // if err := book.Update(); err != nil {
  560. // logs.Error("生成阅读令牌失败 => ", err)
  561. // c.JsonResult(6003, "生成阅读令牌失败")
  562. // }
  563. // beego.Info("用户[", c.Member.Account, "]创建项目令牌 ->", book.PrivateToken)
  564. // c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  565. // } else {
  566. // book.PrivateToken = ""
  567. // if err := book.Update(); err != nil {
  568. // logs.Error("CreateToken => ", err)
  569. // c.JsonResult(6004, "删除令牌失败")
  570. // }
  571. // beego.Info("用户[", c.Member.Account, "]创建项目令牌 ->", book.PrivateToken)
  572. // c.JsonResult(0, "ok", "")
  573. // }
  574. //}
  575. // Delete 删除项目.
  576. func (c *BookController) Delete() {
  577. c.Prepare()
  578. bookResult, err := c.IsPermission()
  579. if err != nil {
  580. c.JsonResult(6001, err.Error())
  581. return
  582. }
  583. if bookResult.RoleId != conf.BookFounder {
  584. c.JsonResult(6002, "只有创始人才能删除项目")
  585. }
  586. err = models.NewBook().ThoroughDeleteBook(bookResult.BookId)
  587. if err == orm.ErrNoRows {
  588. c.JsonResult(6002, "项目不存在")
  589. }
  590. if err != nil {
  591. logs.Error("删除项目 => ", err)
  592. c.JsonResult(6003, "删除失败")
  593. }
  594. beego.Info("用户[", c.Member.Account, "]删除了项目 ->", bookResult)
  595. c.JsonResult(0, "ok")
  596. }
  597. //发布项目.
  598. func (c *BookController) Release() {
  599. c.Prepare()
  600. identify := c.GetString("identify")
  601. bookId := 0
  602. if c.Member.IsAdministrator() {
  603. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  604. if err != nil {
  605. beego.Error("发布文档失败 ->", err)
  606. c.JsonResult(6003, "文档不存在")
  607. return
  608. }
  609. bookId = book.BookId
  610. } else {
  611. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  612. if err != nil {
  613. if err == models.ErrPermissionDenied {
  614. c.JsonResult(6001, "权限不足")
  615. }
  616. if err == orm.ErrNoRows {
  617. c.JsonResult(6002, "项目不存在")
  618. }
  619. beego.Error(err)
  620. c.JsonResult(6003, "未知错误")
  621. }
  622. if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder && book.RoleId != conf.BookEditor {
  623. c.JsonResult(6003, "权限不足")
  624. }
  625. bookId = book.BookId
  626. }
  627. go models.NewBook().ReleaseContent(bookId)
  628. c.JsonResult(0, "发布任务已推送到任务队列,稍后将在后台执行。")
  629. }
  630. //文档排序.
  631. func (c *BookController) SaveSort() {
  632. c.Prepare()
  633. identify := c.Ctx.Input.Param(":key")
  634. if identify == "" {
  635. c.Abort("404")
  636. }
  637. bookId := 0
  638. if c.Member.IsAdministrator() {
  639. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  640. if err != nil || book == nil {
  641. c.JsonResult(6001, "项目不存在")
  642. return
  643. }
  644. bookId = book.BookId
  645. } else {
  646. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  647. if err != nil {
  648. beego.Error("DocumentController.Edit => ", err)
  649. c.Abort("403")
  650. }
  651. if bookResult.RoleId == conf.BookObserver {
  652. c.JsonResult(6002, "项目不存在或权限不足")
  653. }
  654. bookId = bookResult.BookId
  655. }
  656. content := c.Ctx.Input.RequestBody
  657. var docs []map[string]interface{}
  658. err := json.Unmarshal(content, &docs)
  659. if err != nil {
  660. beego.Error(err)
  661. c.JsonResult(6003, "数据错误")
  662. }
  663. for _, item := range docs {
  664. if docId, ok := item["id"].(float64); ok {
  665. doc, err := models.NewDocument().Find(int(docId))
  666. if err != nil {
  667. beego.Error(err)
  668. continue
  669. }
  670. if doc.BookId != bookId {
  671. logs.Info("%s", "权限错误")
  672. continue
  673. }
  674. sort, ok := item["sort"].(float64)
  675. if !ok {
  676. beego.Info("排序数字转换失败 => ", item)
  677. continue
  678. }
  679. parentId, ok := item["parent"].(float64)
  680. if !ok {
  681. beego.Info("父分类转换失败 => ", item)
  682. continue
  683. }
  684. if parentId > 0 {
  685. if parent, err := models.NewDocument().Find(int(parentId)); err != nil || parent.BookId != bookId {
  686. continue
  687. }
  688. }
  689. doc.OrderSort = int(sort)
  690. doc.ParentId = int(parentId)
  691. if err := doc.InsertOrUpdate(); err != nil {
  692. fmt.Printf("%s", err.Error())
  693. beego.Error(err)
  694. }
  695. } else {
  696. fmt.Printf("文档ID转换失败 => %+v", item)
  697. }
  698. }
  699. c.JsonResult(0, "ok")
  700. }
  701. func (c *BookController) Team() {
  702. c.Prepare()
  703. c.TplName = "book/team.tpl"
  704. key := c.Ctx.Input.Param(":key")
  705. pageIndex, _ := c.GetInt("page", 1)
  706. if key == "" {
  707. c.ShowErrorPage(404, "项目不存在或已删除")
  708. }
  709. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  710. if err != nil || book == nil {
  711. if err == models.ErrPermissionDenied {
  712. c.ShowErrorPage(403, "权限不足")
  713. }
  714. c.ShowErrorPage(500, "系统错误")
  715. return
  716. }
  717. //如果不是创始人也不是管理员则不能操作
  718. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  719. c.Abort("403")
  720. }
  721. c.Data["Model"] = book
  722. members, totalCount, err := models.NewTeamRelationship().FindByBookToPager(book.BookId, pageIndex, conf.PageSize)
  723. if totalCount > 0 {
  724. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  725. c.Data["PageHtml"] = pager.HtmlPages()
  726. } else {
  727. c.Data["PageHtml"] = ""
  728. }
  729. b, err := json.Marshal(members)
  730. if err != nil {
  731. c.Data["Result"] = template.JS("[]")
  732. } else {
  733. c.Data["Result"] = template.JS(string(b))
  734. }
  735. }
  736. func (c *BookController) TeamAdd() {
  737. c.Prepare()
  738. teamId, _ := c.GetInt("teamId")
  739. book, err := c.IsPermission()
  740. if err != nil {
  741. c.JsonResult(500, err.Error())
  742. return
  743. }
  744. //如果不是创始人也不是管理员则不能操作
  745. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  746. c.Abort("403")
  747. }
  748. _, err = models.NewTeam().First(teamId, "team_id")
  749. if err != nil {
  750. if err == orm.ErrNoRows {
  751. c.JsonResult(500, "团队不存在")
  752. }
  753. c.JsonResult(5002, err.Error())
  754. }
  755. if _, err := models.NewTeamRelationship().FindByBookId(book.BookId, teamId); err == nil {
  756. c.JsonResult(5003, "团队已加入当前项目")
  757. }
  758. teamRel := models.NewTeamRelationship()
  759. teamRel.BookId = book.BookId
  760. teamRel.TeamId = teamId
  761. err = teamRel.Save()
  762. if err != nil {
  763. c.JsonResult(5004, "加入项目失败")
  764. return
  765. }
  766. teamRel.Include()
  767. c.JsonResult(0, "OK", teamRel)
  768. }
  769. //删除项目的团队.
  770. func (c *BookController) TeamDelete() {
  771. c.Prepare()
  772. teamId, _ := c.GetInt("teamId")
  773. if teamId <= 0 {
  774. c.JsonResult(5001, "参数错误")
  775. }
  776. book, err := c.IsPermission()
  777. if err != nil {
  778. c.JsonResult(5002, err.Error())
  779. return
  780. }
  781. //如果不是创始人也不是管理员则不能操作
  782. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  783. c.Abort("403")
  784. }
  785. err = models.NewTeamRelationship().DeleteByBookId(book.BookId, teamId)
  786. if err != nil {
  787. if err == orm.ErrNoRows {
  788. c.JsonResult(5003, "团队未加入项目")
  789. }
  790. c.JsonResult(5004, err.Error())
  791. }
  792. c.JsonResult(0, "OK")
  793. }
  794. //团队搜索.
  795. func (c *BookController) TeamSearch() {
  796. c.Prepare()
  797. keyword := strings.TrimSpace(c.GetString("q"))
  798. book, err := c.IsPermission()
  799. if err != nil {
  800. c.JsonResult(500, err.Error())
  801. }
  802. keyword = sqltil.EscapeLike(keyword)
  803. searchResult, err := models.NewTeamRelationship().FindNotJoinBookByBookIdentify(book.BookId, keyword, 10)
  804. if err != nil {
  805. c.JsonResult(500, err.Error(), searchResult)
  806. }
  807. c.JsonResult(0, "OK", searchResult)
  808. }
  809. //项目空间搜索.
  810. func (c *BookController) ItemsetsSearch() {
  811. c.Prepare()
  812. keyword := strings.TrimSpace(c.GetString("q"))
  813. keyword = sqltil.EscapeLike(keyword)
  814. searchResult, err := models.NewItemsets().FindItemsetsByName(keyword, 10)
  815. if err != nil {
  816. c.JsonResult(500, err.Error(), searchResult)
  817. }
  818. c.JsonResult(0, "OK", searchResult)
  819. }
  820. func (c *BookController) IsPermission() (*models.BookResult, error) {
  821. identify := c.GetString("identify")
  822. if identify == "" {
  823. return nil, errors.New("参数错误")
  824. }
  825. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  826. if err != nil {
  827. if err == models.ErrPermissionDenied {
  828. return book, errors.New("权限不足")
  829. }
  830. if err == orm.ErrNoRows {
  831. return book, errors.New("项目不存在")
  832. }
  833. return book, err
  834. }
  835. if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  836. return book, errors.New("权限不足")
  837. }
  838. return book, nil
  839. }