document.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. package controllers
  2. import (
  3. "os"
  4. "time"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "net/http"
  9. "path/filepath"
  10. "encoding/json"
  11. "html/template"
  12. "container/list"
  13. "github.com/lifei6671/godoc/models"
  14. "github.com/lifei6671/godoc/conf"
  15. "github.com/astaxie/beego"
  16. "github.com/astaxie/beego/orm"
  17. "github.com/lifei6671/godoc/utils"
  18. )
  19. //DocumentController struct.
  20. type DocumentController struct {
  21. BaseController
  22. }
  23. //判断用户是否可以阅读文档.
  24. func isReadable (identify,token string,c *DocumentController) *models.BookResult {
  25. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  26. if err != nil {
  27. beego.Error(err)
  28. c.Abort("500")
  29. }
  30. if c.Member != nil && c.Member.Role == conf.MemberSuperRole {
  31. bookResult := book.ToBookResult()
  32. return bookResult
  33. }
  34. //如果文档是私有的
  35. if book.PrivatelyOwned == 1 {
  36. is_ok := false
  37. if c.Member != nil {
  38. _, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId)
  39. if err == nil {
  40. is_ok = true
  41. }
  42. }
  43. if book.PrivateToken != "" && !is_ok {
  44. //如果有访问的Token,并且该项目设置了访问Token,并且和用户提供的相匹配,则记录到Session中.
  45. //如果用户未提供Token且用户登录了,则判断用户是否参与了该项目.
  46. //如果用户未登录,则从Session中读取Token.
  47. if token != "" && strings.EqualFold(token, book.PrivateToken) {
  48. c.SetSession(identify, token)
  49. } else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
  50. c.Abort("403")
  51. }
  52. } else if !is_ok {
  53. c.Abort("403")
  54. }
  55. }
  56. bookResult := book.ToBookResult()
  57. if c.Member != nil {
  58. rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId)
  59. if err == nil {
  60. bookResult.MemberId = rel.MemberId
  61. bookResult.RoleId = rel.RoleId
  62. bookResult.RelationshipId = rel.RelationshipId
  63. }
  64. }
  65. //判断是否需要显示评论框
  66. if bookResult.CommentStatus == "closed" {
  67. bookResult.IsDisplayComment = false
  68. } else if bookResult.CommentStatus == "open" {
  69. bookResult.IsDisplayComment = true
  70. } else if bookResult.CommentStatus == "group_only" {
  71. bookResult.IsDisplayComment = bookResult.RelationshipId > 0
  72. } else if bookResult.CommentStatus == "registered_only" {
  73. bookResult.IsDisplayComment = true
  74. }
  75. return bookResult
  76. }
  77. //文档首页.
  78. func (c *DocumentController) Index() {
  79. c.Prepare()
  80. identify := c.Ctx.Input.Param(":key")
  81. token := c.GetString("token")
  82. if identify == "" {
  83. c.Abort("404")
  84. }
  85. //如果没有开启你们访问则跳转到登录
  86. if !c.EnableAnonymous && c.Member == nil {
  87. c.Redirect(beego.URLFor("AccountController.Login"),302)
  88. return
  89. }
  90. bookResult := isReadable(identify,token,c)
  91. c.TplName = "document/" + bookResult.Theme + "_read.tpl"
  92. tree,err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId,0)
  93. if err != nil {
  94. beego.Error(err)
  95. c.Abort("500")
  96. }
  97. c.Data["Model"] = bookResult
  98. c.Data["Result"] = template.HTML(tree)
  99. c.Data["Title"] = "概要"
  100. c.Data["Content"] = bookResult.Description
  101. }
  102. //阅读文档.
  103. func (c *DocumentController) Read() {
  104. c.Prepare()
  105. identify := c.Ctx.Input.Param(":key")
  106. token := c.GetString("token")
  107. id := c.GetString(":id")
  108. if identify == "" || id == ""{
  109. c.Abort("404")
  110. }
  111. //如果没有开启你们访问则跳转到登录
  112. if !c.EnableAnonymous && c.Member == nil {
  113. c.Redirect(beego.URLFor("AccountController.Login"),302)
  114. return
  115. }
  116. bookResult := isReadable(identify,token,c)
  117. c.TplName = "document/" + bookResult.Theme + "_read.tpl"
  118. doc := models.NewDocument()
  119. if doc_id,err := strconv.Atoi(id);err == nil {
  120. doc,err = doc.Find(doc_id)
  121. if err != nil {
  122. beego.Error(err)
  123. c.Abort("500")
  124. }
  125. }else{
  126. doc,err = doc.FindByFieldFirst("identify",id)
  127. if err != nil {
  128. beego.Error(err)
  129. c.Abort("500")
  130. }
  131. }
  132. if doc.BookId != bookResult.BookId {
  133. c.Abort("403")
  134. }
  135. if c.IsAjax() {
  136. var data struct{
  137. DocTitle string `json:"doc_title"`
  138. Body string `json:"body"`
  139. Title string `json:"title"`
  140. }
  141. data.DocTitle = doc.DocumentName
  142. data.Body = doc.Release
  143. data.Title = doc.DocumentName + " - Powered by MinDoc"
  144. c.JsonResult(0,"ok",data)
  145. }
  146. tree,err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId,doc.DocumentId)
  147. if err != nil {
  148. beego.Error(err)
  149. c.Abort("500")
  150. }
  151. c.Data["Model"] = bookResult
  152. c.Data["Result"] = template.HTML(tree)
  153. c.Data["Title"] = doc.DocumentName
  154. c.Data["Content"] = template.HTML(doc.Release)
  155. }
  156. //编辑文档.
  157. func (c *DocumentController) Edit() {
  158. c.Prepare()
  159. identify := c.Ctx.Input.Param(":key")
  160. if identify == "" {
  161. c.Abort("404")
  162. }
  163. bookResult := models.NewBookResult()
  164. //如果是超级管理者,则不判断权限
  165. if c.Member.Role == conf.MemberSuperRole {
  166. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  167. if err != nil {
  168. c.JsonResult(6002, "项目不存在或权限不足")
  169. }
  170. bookResult = book.ToBookResult()
  171. }else {
  172. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  173. if err != nil {
  174. beego.Error("DocumentController.Edit => ", err)
  175. c.Abort("403")
  176. }
  177. if bookResult.RoleId == conf.BookObserver {
  178. c.JsonResult(6002, "项目不存在或权限不足")
  179. }
  180. }
  181. //根据不同编辑器类型加载编辑器
  182. if bookResult.Editor == "markdown" {
  183. c.TplName = "document/markdown_edit_template.tpl"
  184. }else if bookResult.Editor == "html"{
  185. c.TplName = "document/html_edit_template.tpl"
  186. }else{
  187. c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
  188. }
  189. c.Data["Model"] = bookResult
  190. r,_ := json.Marshal(bookResult)
  191. c.Data["ModelResult"] = template.JS(string(r))
  192. c.Data["Result"] = template.JS("[]")
  193. trees ,err := models.NewDocument().FindDocumentTree(bookResult.BookId)
  194. if err != nil {
  195. beego.Error("FindDocumentTree => ", err)
  196. }else{
  197. if len(trees) > 0 {
  198. if jtree, err := json.Marshal(trees); err == nil {
  199. c.Data["Result"] = template.JS(string(jtree))
  200. }
  201. }else{
  202. c.Data["Result"] = template.JS("[]")
  203. }
  204. }
  205. }
  206. //创建一个文档.
  207. func (c *DocumentController) Create() {
  208. identify := c.GetString("identify")
  209. doc_identify := c.GetString("doc_identify")
  210. doc_name := c.GetString("doc_name")
  211. parent_id,_ := c.GetInt("parent_id",0)
  212. doc_id,_ := c.GetInt("doc_id",0)
  213. if identify == "" {
  214. c.JsonResult(6001,"参数错误")
  215. }
  216. if doc_name == "" {
  217. c.JsonResult(6004,"文档名称不能为空")
  218. }
  219. if doc_identify != "" {
  220. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, doc_identify); !ok || err != nil {
  221. c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  222. }
  223. d,_ := models.NewDocument().FindByFieldFirst("identify",doc_identify);
  224. if d.DocumentId > 0 && d.DocumentId != doc_id{
  225. c.JsonResult(6006,"文档标识已被使用")
  226. }
  227. }
  228. book_id := 0
  229. //如果是超级管理员则不判断权限
  230. if c.Member.Role == conf.MemberSuperRole {
  231. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  232. if err != nil {
  233. beego.Error(err)
  234. c.JsonResult(6002, "项目不存在或权限不足")
  235. }
  236. book_id = book.BookId
  237. }else{
  238. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  239. if err != nil || bookResult.RoleId == conf.BookObserver {
  240. beego.Error("FindByIdentify => ", err)
  241. c.JsonResult(6002, "项目不存在或权限不足")
  242. }
  243. book_id = bookResult.BookId
  244. }
  245. if parent_id > 0 {
  246. doc,err := models.NewDocument().Find(parent_id)
  247. if err != nil || doc.BookId != book_id {
  248. c.JsonResult(6003,"父分类不存在")
  249. }
  250. }
  251. document,_ := models.NewDocument().Find(doc_id)
  252. document.MemberId = c.Member.MemberId
  253. document.BookId = book_id
  254. if doc_identify != ""{
  255. document.Identify = doc_identify
  256. }
  257. document.Version = time.Now().Unix()
  258. document.DocumentName = doc_name
  259. document.ParentId = parent_id
  260. if err := document.InsertOrUpdate();err != nil {
  261. beego.Error("InsertOrUpdate => ",err)
  262. c.JsonResult(6005,"保存失败")
  263. }else{
  264. c.JsonResult(0,"ok",document)
  265. }
  266. }
  267. //上传附件或图片.
  268. func (c *DocumentController) Upload() {
  269. identify := c.GetString("identify")
  270. doc_id,_ := c.GetInt("doc_id")
  271. if identify == "" {
  272. c.JsonResult(6001,"参数错误")
  273. }
  274. name := "editormd-file-file"
  275. file,moreFile,err := c.GetFile(name)
  276. if err == http.ErrMissingFile {
  277. name = "editormd-image-file"
  278. file,moreFile,err = c.GetFile(name);
  279. if err == http.ErrMissingFile {
  280. c.JsonResult(6003,"没有发现需要上传的文件")
  281. }
  282. }
  283. if err != nil {
  284. c.JsonResult(6002,err.Error())
  285. }
  286. defer file.Close()
  287. ext := filepath.Ext(moreFile.Filename)
  288. if ext == "" {
  289. c.JsonResult(6003,"无法解析文件的格式")
  290. }
  291. if !conf.IsAllowUploadFileExt(ext) {
  292. c.JsonResult(6004,"不允许的文件类型")
  293. }
  294. book_id := 0
  295. //如果是超级管理员,则不判断权限
  296. if c.Member.Role == conf.MemberSuperRole {
  297. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  298. if err != nil {
  299. c.JsonResult(6006, "文档不存在或权限不足")
  300. }
  301. book_id = book.BookId
  302. }else{
  303. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  304. if err != nil {
  305. beego.Error("DocumentController.Edit => ", err)
  306. if err == orm.ErrNoRows {
  307. c.JsonResult(6006, "权限不足")
  308. }
  309. c.JsonResult(6001, err.Error())
  310. }
  311. //如果没有编辑权限
  312. if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  313. c.JsonResult(6006, "权限不足")
  314. }
  315. book_id = book.BookId
  316. }
  317. if doc_id > 0 {
  318. doc,err := models.NewDocument().Find(doc_id);
  319. if err != nil {
  320. c.JsonResult(6007,"文档不存在")
  321. }
  322. if doc.BookId != book_id {
  323. c.JsonResult(6008,"文档不属于指定的项目")
  324. }
  325. }
  326. fileName := "attachment_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  327. filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext
  328. path := filepath.Dir(filePath)
  329. os.MkdirAll(path, os.ModePerm)
  330. err = c.SaveToFile(name,filePath)
  331. if err != nil {
  332. beego.Error("SaveToFile => ",err)
  333. c.JsonResult(6005,"保存文件失败")
  334. }
  335. attachment := models.NewAttachment()
  336. attachment.BookId = book_id
  337. attachment.FileName = moreFile.Filename
  338. attachment.CreateAt = c.Member.MemberId
  339. attachment.FileExt = ext
  340. attachment.FilePath = filePath
  341. if doc_id > 0{
  342. attachment.DocumentId = doc_id
  343. }
  344. if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg") || strings.EqualFold(ext,"png") || strings.EqualFold(ext,"gif") {
  345. attachment.HttpPath = c.BaseUrl() + "/" + filePath
  346. }
  347. err = attachment.Insert();
  348. if err != nil {
  349. os.Remove(filePath)
  350. beego.Error("Attachment Insert => ",err)
  351. c.JsonResult(6006,"文件保存失败")
  352. }
  353. if attachment.HttpPath == "" {
  354. attachment.HttpPath = c.BaseUrl() + beego.URLFor("DocumentController.DownloadAttachment",":key", identify, ":attach_id", attachment.AttachmentId)
  355. if err := attachment.Update();err != nil {
  356. beego.Error("SaveToFile => ",err)
  357. c.JsonResult(6005,"保存文件失败")
  358. }
  359. }
  360. result := map[string]interface{}{
  361. "errcode" : 0,
  362. "success" : 1,
  363. "message" :"ok",
  364. "url" : attachment.HttpPath,
  365. "alt" : attachment.FileName,
  366. }
  367. c.Data["json"] = result
  368. c.ServeJSON(true)
  369. c.StopRun()
  370. }
  371. //DownloadAttachment 下载附件.
  372. func (c *DocumentController) DownloadAttachment() {
  373. c.Prepare()
  374. identify := c.Ctx.Input.Param(":key")
  375. attach_id,_ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  376. token := c.GetString("token")
  377. member_id := 0
  378. if c.Member != nil {
  379. member_id = c.Member.MemberId
  380. }
  381. book_id := 0
  382. //判断用户是否参与了项目
  383. bookResult,err := models.NewBookResult().FindByIdentify(identify,member_id)
  384. if err != nil {
  385. //判断项目公开状态
  386. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  387. if err != nil {
  388. c.Abort("404")
  389. }
  390. //如果不是超级管理员则判断权限
  391. if c.Member == nil || c.Member.Role != conf.MemberSuperRole {
  392. //如果项目是私有的,并且token不正确
  393. if (book.PrivatelyOwned == 1 && token == "" ) || ( book.PrivatelyOwned == 1 && book.PrivateToken != token ) {
  394. c.Abort("403")
  395. }
  396. }
  397. book_id = book.BookId
  398. }else{
  399. book_id = bookResult.BookId
  400. }
  401. //查找附件
  402. attachment,err := models.NewAttachment().Find(attach_id)
  403. if err != nil {
  404. beego.Error("DownloadAttachment => ", err)
  405. if err == orm.ErrNoRows {
  406. c.Abort("404")
  407. } else {
  408. c.Abort("500")
  409. }
  410. }
  411. if attachment.BookId != book_id {
  412. c.Abort("404")
  413. }
  414. c.Ctx.Output.Download(attachment.FilePath,attachment.FileName)
  415. c.StopRun()
  416. }
  417. //删除文档.
  418. func (c *DocumentController) Delete() {
  419. c.Prepare()
  420. identify := c.GetString("identify")
  421. doc_id,err := c.GetInt("doc_id",0)
  422. book_id := 0
  423. //如果是超级管理员则忽略权限判断
  424. if c.Member.Role == conf.MemberSuperRole {
  425. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  426. if err != nil {
  427. beego.Error("FindByIdentify => ", err)
  428. c.JsonResult(6002, "项目不存在或权限不足")
  429. }
  430. book_id = book.BookId
  431. }else {
  432. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  433. if err != nil || bookResult.RoleId == conf.BookObserver {
  434. beego.Error("FindByIdentify => ", err)
  435. c.JsonResult(6002, "项目不存在或权限不足")
  436. }
  437. book_id = bookResult.BookId
  438. }
  439. if doc_id <= 0 {
  440. c.JsonResult(6001,"参数错误")
  441. }
  442. doc,err := models.NewDocument().Find(doc_id)
  443. if err != nil {
  444. beego.Error("Delete => ",err)
  445. c.JsonResult(6003,"删除失败")
  446. }
  447. //如果文档所属项目错误
  448. if doc.BookId != book_id {
  449. c.JsonResult(6004,"参数错误")
  450. }
  451. //递归删除项目下的文档以及子文档
  452. err = doc.RecursiveDocument(doc.DocumentId)
  453. if err != nil {
  454. c.JsonResult(6005,"删除失败")
  455. }
  456. //重置文档数量统计
  457. models.NewBook().ResetDocumentNumber(doc.BookId)
  458. c.JsonResult(0,"ok")
  459. }
  460. //获取文档内容.
  461. func (c *DocumentController) Content() {
  462. c.Prepare()
  463. identify := c.Ctx.Input.Param(":key")
  464. doc_id,err := c.GetInt("doc_id")
  465. if err != nil {
  466. doc_id,_ = strconv.Atoi(c.Ctx.Input.Param(":id"))
  467. }
  468. book_id := 0
  469. //如果是超级管理员,则忽略权限
  470. if c.Member.Role == conf.MemberSuperRole {
  471. book ,err := models.NewBook().FindByFieldFirst("identify",identify)
  472. if err != nil {
  473. c.JsonResult(6002, "项目不存在或权限不足")
  474. }
  475. book_id = book.BookId
  476. }else {
  477. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  478. if err != nil || bookResult.RoleId == conf.BookObserver {
  479. beego.Error("FindByIdentify => ", err)
  480. c.JsonResult(6002, "项目不存在或权限不足")
  481. }
  482. book_id = bookResult.BookId
  483. }
  484. if doc_id <= 0 {
  485. c.JsonResult(6001,"参数错误")
  486. }
  487. if c.Ctx.Input.IsPost() {
  488. markdown := strings.TrimSpace(c.GetString("markdown",""))
  489. content := c.GetString("html")
  490. version,_ := c.GetInt64("version",0)
  491. is_cover := c.GetString("cover")
  492. doc ,err := models.NewDocument().Find(doc_id);
  493. if err != nil {
  494. c.JsonResult(6003,"读取文档错误")
  495. }
  496. if doc.BookId != book_id {
  497. c.JsonResult(6004,"保存的文档不属于指定项目")
  498. }
  499. if doc.Version != version && !strings.EqualFold(is_cover,"yes"){
  500. beego.Info("%d|",version,doc.Version)
  501. c.JsonResult(6005,"文档已被修改确定要覆盖吗?")
  502. }
  503. if markdown == "" && content != ""{
  504. doc.Markdown = content
  505. }else{
  506. doc.Markdown = markdown
  507. }
  508. doc.Version = time.Now().Unix()
  509. doc.Content = content
  510. if err := doc.InsertOrUpdate();err != nil {
  511. beego.Error("InsertOrUpdate => ",err)
  512. c.JsonResult(6006,"保存失败")
  513. }
  514. c.JsonResult(0,"ok",doc)
  515. }
  516. doc,err := models.NewDocument().Find(doc_id)
  517. if err != nil {
  518. c.JsonResult(6003,"文档不存在")
  519. }
  520. c.JsonResult(0,"ok",doc)
  521. }
  522. //导出文件
  523. func (c *DocumentController) Export() {
  524. c.Prepare()
  525. c.TplName = "document/export.tpl"
  526. identify := c.Ctx.Input.Param(":key")
  527. output := c.GetString("output")
  528. token := c.GetString("token")
  529. if identify == "" {
  530. c.Abort("404")
  531. }
  532. //如果没有开启你们访问则跳转到登录
  533. if !c.EnableAnonymous && c.Member == nil {
  534. c.Redirect(beego.URLFor("AccountController.Login"),302)
  535. return
  536. }
  537. book := isReadable(identify,token,c)
  538. docs, err := models.NewDocument().FindListByBookId(book.BookId)
  539. if err != nil {
  540. beego.Error(err)
  541. c.Abort("500")
  542. }
  543. if output == "pdf" {
  544. exe := beego.AppConfig.String("wkhtmltopdf")
  545. if exe == "" {
  546. c.TplName = "errors/error.tpl";
  547. c.Data["ErrorMessage"] = "没有配置PDF导出程序"
  548. c.Data["ErrorCode"] = 50010
  549. return
  550. }
  551. dpath := "cache/" + book.Identify
  552. os.MkdirAll(dpath, 0766)
  553. pathList := list.New()
  554. RecursiveFun(0, "", dpath, c, book, docs, pathList)
  555. //defer os.RemoveAll(dpath)
  556. os.MkdirAll("./cache", 0766)
  557. pdfpath := "cache/" + identify + ".pdf"
  558. if _,err := os.Stat(pdfpath); os.IsNotExist(err){
  559. paths := make([]string, len(docs))
  560. index := 0
  561. for e := pathList.Front(); e != nil; e = e.Next() {
  562. paths[index] = e.Value.(string)
  563. index ++
  564. }
  565. beego.Info(paths,pdfpath)
  566. utils.ConverterHtmlToPdf(paths, pdfpath)
  567. }
  568. c.Ctx.Output.Download(pdfpath, identify + ".pdf")
  569. c.StopRun()
  570. }
  571. c.StopRun()
  572. }
  573. //递归生成文档序列数组.
  574. func RecursiveFun(parent_id int,prefix,dpath string,c *DocumentController,book *models.BookResult,docs []*models.Document,paths *list.List) {
  575. for _, item := range docs {
  576. if item.ParentId == parent_id {
  577. name := prefix + strconv.Itoa(item.ParentId) + strconv.Itoa(item.OrderSort) + strconv.Itoa(item.DocumentId)
  578. fpath := dpath + "/" + name + ".html"
  579. paths.PushBack(fpath)
  580. f, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0777)
  581. if err != nil {
  582. beego.Error(err)
  583. c.Abort("500")
  584. }
  585. html, err := c.ExecuteViewPathTemplate("document/export.tpl", map[string]interface{}{"Model" : book, "Lists":item,"BaseUrl" : c.BaseUrl()})
  586. if err != nil {
  587. f.Close()
  588. beego.Error(err)
  589. c.Abort("500")
  590. }
  591. beego.Info(fpath,html)
  592. f.WriteString(html)
  593. f.Close()
  594. for _, sub := range docs {
  595. if sub.ParentId == item.DocumentId {
  596. RecursiveFun(item.DocumentId,name,dpath,c,book,docs,paths)
  597. break;
  598. }
  599. }
  600. }
  601. }
  602. }