BookController.go 20 KB

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