BookController.go 22 KB

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