BookController.go 20 KB

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