DocumentController.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. package controllers
  2. import (
  3. "container/list"
  4. "encoding/json"
  5. "html/template"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "net/url"
  14. "image/png"
  15. "bytes"
  16. "github.com/PuerkitoBio/goquery"
  17. "github.com/astaxie/beego"
  18. "github.com/astaxie/beego/orm"
  19. "github.com/boombuler/barcode"
  20. "github.com/boombuler/barcode/qr"
  21. "github.com/lifei6671/mindoc/conf"
  22. "github.com/lifei6671/mindoc/models"
  23. "github.com/lifei6671/mindoc/utils"
  24. "github.com/lifei6671/mindoc/utils/pagination"
  25. "gopkg.in/russross/blackfriday.v2"
  26. "github.com/lifei6671/mindoc/utils/cryptil"
  27. "fmt"
  28. "github.com/lifei6671/mindoc/utils/filetil"
  29. "github.com/lifei6671/mindoc/utils/gopool"
  30. )
  31. // DocumentController struct
  32. type DocumentController struct {
  33. BaseController
  34. }
  35. // 文档首页
  36. func (c *DocumentController) Index() {
  37. c.Prepare()
  38. identify := c.Ctx.Input.Param(":key")
  39. token := c.GetString("token")
  40. if identify == "" {
  41. c.ShowErrorPage(404,"项目不存在或已删除")
  42. }
  43. // 如果没有开启匿名访问则跳转到登录
  44. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  45. promptUserToLogIn(c)
  46. return
  47. }
  48. bookResult := isReadable(identify, token, c)
  49. c.TplName = "document/" + bookResult.Theme + "_read.tpl"
  50. selected := 0
  51. if bookResult.IsUseFirstDocument {
  52. doc,err := bookResult.FindFirstDocumentByBookId(bookResult.BookId)
  53. if err == nil {
  54. if strings.TrimSpace(doc.Release) != "" {
  55. doc.Release += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") + "</div>";
  56. }
  57. selected = doc.DocumentId
  58. c.Data["Title"] = doc.DocumentName
  59. c.Data["Content"] = template.HTML(doc.Release)
  60. c.Data["Description"] = utils.AutoSummary(doc.Release,120)
  61. }
  62. }else {
  63. c.Data["Title"] = "概要"
  64. c.Data["Content"] = template.HTML(blackfriday.Run([]byte(bookResult.Description)))
  65. }
  66. tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, selected)
  67. if err != nil {
  68. if err == orm.ErrNoRows {
  69. c.ShowErrorPage(404,"生成项目文档树时出错")
  70. } else {
  71. beego.Error("生成项目文档树时出错 -> ",err)
  72. c.ShowErrorPage(500,"生成项目文档树时出错")
  73. }
  74. }
  75. c.Data["Model"] = bookResult
  76. c.Data["Result"] = template.HTML(tree)
  77. }
  78. // 阅读文档
  79. func (c *DocumentController) Read() {
  80. c.Prepare()
  81. identify := c.Ctx.Input.Param(":key")
  82. token := c.GetString("token")
  83. id := c.GetString(":id")
  84. c.Data["DocumentId"] = id
  85. if identify == "" || id == "" {
  86. c.ShowErrorPage(404,"项目不存或已删除")
  87. }
  88. // 如果没有开启匿名访问则跳转到登录
  89. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  90. promptUserToLogIn(c)
  91. return
  92. }
  93. bookResult := isReadable(identify, token, c)
  94. c.TplName = fmt.Sprintf("document/%s_read.tpl",bookResult.Theme)
  95. doc := models.NewDocument()
  96. if docId, err := strconv.Atoi(id); err == nil {
  97. doc, err = doc.FromCacheById(docId)
  98. if err != nil {
  99. beego.Error("从缓存中读取文档时失败 ->",err)
  100. c.ShowErrorPage(500,"文档不存在或已删除")
  101. }
  102. } else {
  103. doc, err = doc.FromCacheByIdentify(id,bookResult.BookId)
  104. if err != nil {
  105. if err == orm.ErrNoRows {
  106. c.ShowErrorPage(404,"文档不存在或已删除")
  107. }else{
  108. beego.Error("从缓存查询文档时出错 ->" ,err)
  109. c.ShowErrorPage(500,"未知异常")
  110. }
  111. }
  112. }
  113. if doc.BookId != bookResult.BookId {
  114. c.ShowErrorPage(404,"文档不存在或已删除")
  115. }
  116. attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
  117. if err == nil {
  118. doc.AttachList = attach
  119. }
  120. cdnimg := beego.AppConfig.String("cdnimg")
  121. if doc.Release != "" && cdnimg != "" {
  122. query, err := goquery.NewDocumentFromReader(bytes.NewBufferString(doc.Release))
  123. if err != nil {
  124. beego.Error(err)
  125. } else {
  126. query.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
  127. if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
  128. contentSelection.SetAttr("src", utils.JoinURI(cdnimg, src))
  129. }
  130. })
  131. html, err := query.Html()
  132. if err != nil {
  133. beego.Error(err)
  134. } else {
  135. doc.Release = html
  136. }
  137. }
  138. }
  139. // assemble doc info, added by dandycheung, 2017-12-20
  140. docInfo := ""
  141. docCreator, err := models.NewMember().Find(doc.MemberId)
  142. if err == nil {
  143. docInfo += docCreator.Account
  144. }
  145. docInfo += " 创建于 "
  146. docInfo += doc.CreateTime.Local().Format("2006-01-02 15:04")
  147. if doc.ModifyTime != doc.CreateTime {
  148. docInfo += ";更新于 "
  149. docInfo += doc.ModifyTime.Local().Format("2006-01-02 15:04")
  150. if strings.TrimSpace(doc.Release) != "" {
  151. doc.Release += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") +" &nbsp;&nbsp;作者:";
  152. if docCreator != nil {
  153. if docCreator.RealName != "" {
  154. doc.Release += docCreator.RealName
  155. }else{
  156. doc.Release += docCreator.Account
  157. }
  158. }
  159. doc.Release += "</div>"
  160. }
  161. }
  162. if c.IsAjax() {
  163. var data struct {
  164. DocTitle string `json:"doc_title"`
  165. Body string `json:"body"`
  166. Title string `json:"title"`
  167. DocInfo string `json:"doc_info"`
  168. }
  169. data.DocTitle = doc.DocumentName
  170. data.Body = doc.Release
  171. data.Title = doc.DocumentName + " - Powered by MinDoc"
  172. data.DocInfo = docInfo
  173. c.JsonResult(0, "ok", data)
  174. }
  175. tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, doc.DocumentId)
  176. if err != nil && err != orm.ErrNoRows {
  177. beego.Error("生成项目文档树时出错 ->",err)
  178. c.ShowErrorPage(500,"生成项目文档树时出错")
  179. }
  180. c.Data["Description"] = utils.AutoSummary(doc.Release,120)
  181. c.Data["Model"] = bookResult
  182. c.Data["Result"] = template.HTML(tree)
  183. c.Data["Title"] = doc.DocumentName
  184. c.Data["Info"] = docInfo
  185. c.Data["Content"] = template.HTML(doc.Release)
  186. }
  187. // 编辑文档
  188. func (c *DocumentController) Edit() {
  189. c.Prepare()
  190. identify := c.Ctx.Input.Param(":key")
  191. if identify == "" {
  192. c.ShowErrorPage(404,"无法解析项目标识")
  193. }
  194. bookResult := models.NewBookResult()
  195. var err error
  196. // 如果是超级管理者,则不判断权限
  197. if c.Member.IsAdministrator() {
  198. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  199. if err != nil {
  200. c.JsonResult(6002, "项目不存在或权限不足")
  201. }
  202. bookResult = models.NewBookResult().ToBookResult(*book)
  203. } else {
  204. bookResult, err = models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  205. if err != nil {
  206. if err == orm.ErrNoRows {
  207. c.ShowErrorPage(403, "项目不存在或没有权限")
  208. }else{
  209. beego.Error("查询项目时出错 -> ", err)
  210. c.ShowErrorPage(500,"查询项目时出错")
  211. }
  212. }
  213. if bookResult.RoleId == conf.BookObserver {
  214. c.JsonResult(6002, "项目不存在或权限不足")
  215. }
  216. }
  217. // 根据不同编辑器类型加载编辑器
  218. if bookResult.Editor == "markdown" {
  219. c.TplName = "document/markdown_edit_template.tpl"
  220. } else if bookResult.Editor == "html" {
  221. c.TplName = "document/new_html_edit_template.tpl"
  222. } else {
  223. c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
  224. }
  225. c.Data["Model"] = bookResult
  226. r, _ := json.Marshal(bookResult)
  227. c.Data["ModelResult"] = template.JS(string(r))
  228. c.Data["Result"] = template.JS("[]")
  229. trees, err := models.NewDocument().FindDocumentTree(bookResult.BookId)
  230. if err != nil {
  231. beego.Error("FindDocumentTree => ", err)
  232. } else {
  233. if len(trees) > 0 {
  234. if jtree, err := json.Marshal(trees); err == nil {
  235. c.Data["Result"] = template.JS(string(jtree))
  236. }
  237. } else {
  238. c.Data["Result"] = template.JS("[]")
  239. }
  240. }
  241. c.Data["BaiDuMapKey"] = beego.AppConfig.DefaultString("baidumapkey", "")
  242. }
  243. // 创建一个文档
  244. func (c *DocumentController) Create() {
  245. identify := c.GetString("identify")
  246. docIdentify := c.GetString("doc_identify")
  247. docName := c.GetString("doc_name")
  248. parentId, _ := c.GetInt("parent_id", 0)
  249. docId, _ := c.GetInt("doc_id", 0)
  250. if identify == "" {
  251. c.JsonResult(6001, "参数错误")
  252. }
  253. if docName == "" {
  254. c.JsonResult(6004, "文档名称不能为空")
  255. }
  256. bookId := 0
  257. // 如果是超级管理员则不判断权限
  258. if c.Member.IsAdministrator() {
  259. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  260. if err != nil {
  261. beego.Error(err)
  262. c.JsonResult(6002, "项目不存在或权限不足")
  263. }
  264. bookId = book.BookId
  265. } else {
  266. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  267. if err != nil || bookResult.RoleId == conf.BookObserver {
  268. beego.Error("FindByIdentify => ", err)
  269. c.JsonResult(6002, "项目不存在或权限不足")
  270. }
  271. bookId = bookResult.BookId
  272. }
  273. if docIdentify != "" {
  274. if ok, err := regexp.MatchString(`[a-z]+[a-zA-Z0-9_.\-]*$`, docIdentify); !ok || err != nil {
  275. c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”、“.”和“_”符号")
  276. }
  277. d, _ := models.NewDocument().FindByIdentityFirst(docIdentify,bookId)
  278. if d.DocumentId > 0 && d.DocumentId != docId {
  279. c.JsonResult(6006, "文档标识已被使用")
  280. }
  281. }
  282. if parentId > 0 {
  283. doc, err := models.NewDocument().Find(parentId)
  284. if err != nil || doc.BookId != bookId {
  285. c.JsonResult(6003, "父分类不存在")
  286. }
  287. }
  288. document, _ := models.NewDocument().Find(docId)
  289. document.MemberId = c.Member.MemberId
  290. document.BookId = bookId
  291. document.Identify = docIdentify
  292. document.Version = time.Now().Unix()
  293. document.DocumentName = docName
  294. document.ParentId = parentId
  295. if err := document.InsertOrUpdate(); err != nil {
  296. beego.Error("InsertOrUpdate => ", err)
  297. c.JsonResult(6005, "保存失败")
  298. } else {
  299. c.JsonResult(0, "ok", document)
  300. }
  301. }
  302. // 上传附件或图片
  303. func (c *DocumentController) Upload() {
  304. identify := c.GetString("identify")
  305. doc_id, _ := c.GetInt("doc_id")
  306. is_attach := true
  307. if identify == "" {
  308. c.JsonResult(6001, "参数错误")
  309. }
  310. name := "editormd-file-file"
  311. file, moreFile, err := c.GetFile(name)
  312. if err == http.ErrMissingFile {
  313. name = "editormd-image-file"
  314. file, moreFile, err = c.GetFile(name)
  315. if err == http.ErrMissingFile {
  316. c.JsonResult(6003, "没有发现需要上传的文件")
  317. }
  318. }
  319. if err != nil {
  320. c.JsonResult(6002, err.Error())
  321. }
  322. defer file.Close()
  323. type Size interface {
  324. Size() int64
  325. }
  326. if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() {
  327. c.JsonResult(6009, "查过文件允许的上传最大值")
  328. }
  329. ext := filepath.Ext(moreFile.Filename)
  330. if ext == "" {
  331. c.JsonResult(6003, "无法解析文件的格式")
  332. }
  333. //如果文件类型设置为 * 标识不限制文件类型
  334. if beego.AppConfig.DefaultString("upload_file_ext", "") != "*" {
  335. if !conf.IsAllowUploadFileExt(ext) {
  336. c.JsonResult(6004, "不允许的文件类型")
  337. }
  338. }
  339. bookId := 0
  340. // 如果是超级管理员,则不判断权限
  341. if c.Member.IsAdministrator() {
  342. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  343. if err != nil {
  344. c.JsonResult(6006, "文档不存在或权限不足")
  345. }
  346. bookId = book.BookId
  347. } else {
  348. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  349. if err != nil {
  350. beego.Error("DocumentController.Edit => ", err)
  351. if err == orm.ErrNoRows {
  352. c.JsonResult(6006, "权限不足")
  353. }
  354. c.JsonResult(6001, err.Error())
  355. }
  356. // 如果没有编辑权限
  357. if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  358. c.JsonResult(6006, "权限不足")
  359. }
  360. bookId = book.BookId
  361. }
  362. if doc_id > 0 {
  363. doc, err := models.NewDocument().Find(doc_id)
  364. if err != nil {
  365. c.JsonResult(6007, "文档不存在")
  366. }
  367. if doc.BookId != bookId {
  368. c.JsonResult(6008, "文档不属于指定的项目")
  369. }
  370. }
  371. fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  372. filePath := filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"),identify, fileName+ext)
  373. path := filepath.Dir(filePath)
  374. os.MkdirAll(path, os.ModePerm)
  375. err = c.SaveToFile(name, filePath)
  376. if err != nil {
  377. beego.Error("SaveToFile => ", err)
  378. c.JsonResult(6005, "保存文件失败")
  379. }
  380. attachment := models.NewAttachment()
  381. attachment.BookId = bookId
  382. attachment.FileName = moreFile.Filename
  383. attachment.CreateAt = c.Member.MemberId
  384. attachment.FileExt = ext
  385. attachment.FilePath = strings.TrimPrefix(filePath, conf.WorkingDirectory)
  386. attachment.DocumentId = doc_id
  387. if fileInfo, err := os.Stat(filePath); err == nil {
  388. attachment.FileSize = float64(fileInfo.Size())
  389. }
  390. if doc_id > 0 {
  391. attachment.DocumentId = doc_id
  392. }
  393. if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".gif") {
  394. attachment.HttpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  395. if strings.HasPrefix(attachment.HttpPath, "//") {
  396. attachment.HttpPath = conf.URLForWithCdnImage(string(attachment.HttpPath[1:]))
  397. }
  398. is_attach = false
  399. }
  400. err = attachment.Insert()
  401. if err != nil {
  402. os.Remove(filePath)
  403. beego.Error("Attachment Insert => ", err)
  404. c.JsonResult(6006, "文件保存失败")
  405. }
  406. if attachment.HttpPath == "" {
  407. attachment.HttpPath = conf.URLFor("DocumentController.DownloadAttachment", ":key", identify, ":attach_id", attachment.AttachmentId)
  408. if err := attachment.Update(); err != nil {
  409. beego.Error("SaveToFile => ", err)
  410. c.JsonResult(6005, "保存文件失败")
  411. }
  412. }
  413. result := map[string]interface{}{
  414. "errcode": 0,
  415. "success": 1,
  416. "message": "ok",
  417. "url": attachment.HttpPath,
  418. "alt": attachment.FileName,
  419. "is_attach": is_attach,
  420. "attach": attachment,
  421. }
  422. c.Ctx.Output.JSON(result, true, false)
  423. c.StopRun()
  424. }
  425. // 下载附件
  426. func (c *DocumentController) DownloadAttachment() {
  427. c.Prepare()
  428. identify := c.Ctx.Input.Param(":key")
  429. attachId, _ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  430. token := c.GetString("token")
  431. memberId := 0
  432. if c.Member != nil {
  433. memberId = c.Member.MemberId
  434. }
  435. bookId := 0
  436. // 判断用户是否参与了项目
  437. bookResult, err := models.NewBookResult().FindByIdentify(identify, memberId)
  438. if err != nil {
  439. // 判断项目公开状态
  440. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  441. if err != nil {
  442. if err == orm.ErrNoRows {
  443. c.ShowErrorPage(404,"项目不存在或已删除")
  444. }else{
  445. beego.Error("查找项目时出错 ->",err)
  446. c.ShowErrorPage(500,"系统错误")
  447. }
  448. }
  449. // 如果不是超级管理员则判断权限
  450. if c.Member == nil || c.Member.Role != conf.MemberSuperRole {
  451. // 如果项目是私有的,并且 token 不正确
  452. if (book.PrivatelyOwned == 1 && token == "") || (book.PrivatelyOwned == 1 && book.PrivateToken != token) {
  453. c.ShowErrorPage(403,"权限不足")
  454. }
  455. }
  456. bookId = book.BookId
  457. } else {
  458. bookId = bookResult.BookId
  459. }
  460. // 查找附件
  461. attachment, err := models.NewAttachment().Find(attachId)
  462. if err != nil {
  463. beego.Error("查找附件时出错 -> ", err)
  464. if err == orm.ErrNoRows {
  465. c.ShowErrorPage(404,"附件不存在或已删除")
  466. } else {
  467. c.ShowErrorPage(500,"查找附件时出错")
  468. }
  469. }
  470. if attachment.BookId != bookId {
  471. c.ShowErrorPage(404,"附件不存在或已删除")
  472. }
  473. c.Ctx.Output.Download(filepath.Join(conf.WorkingDirectory, attachment.FilePath), attachment.FileName)
  474. c.StopRun()
  475. }
  476. // 删除附件
  477. func (c *DocumentController) RemoveAttachment() {
  478. c.Prepare()
  479. attachId, _ := c.GetInt("attach_id")
  480. if attachId <= 0 {
  481. c.JsonResult(6001, "参数错误")
  482. }
  483. attach, err := models.NewAttachment().Find(attachId)
  484. if err != nil {
  485. beego.Error(err)
  486. c.JsonResult(6002, "附件不存在")
  487. }
  488. document, err := models.NewDocument().Find(attach.DocumentId)
  489. if err != nil {
  490. beego.Error(err)
  491. c.JsonResult(6003, "文档不存在")
  492. }
  493. if c.Member.Role != conf.MemberSuperRole {
  494. rel, err := models.NewRelationship().FindByBookIdAndMemberId(document.BookId, c.Member.MemberId)
  495. if err != nil {
  496. beego.Error(err)
  497. c.JsonResult(6004, "权限不足")
  498. }
  499. if rel.RoleId == conf.BookObserver {
  500. c.JsonResult(6004, "权限不足")
  501. }
  502. }
  503. err = attach.Delete()
  504. if err != nil {
  505. beego.Error(err)
  506. c.JsonResult(6005, "删除失败")
  507. }
  508. os.Remove(filepath.Join(conf.WorkingDirectory, attach.FilePath))
  509. c.JsonResult(0, "ok", attach)
  510. }
  511. // 删除文档
  512. func (c *DocumentController) Delete() {
  513. c.Prepare()
  514. identify := c.GetString("identify")
  515. doc_id, err := c.GetInt("doc_id", 0)
  516. book_id := 0
  517. // 如果是超级管理员则忽略权限判断
  518. if c.Member.IsAdministrator() {
  519. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  520. if err != nil {
  521. beego.Error("FindByIdentify => ", err)
  522. c.JsonResult(6002, "项目不存在或权限不足")
  523. }
  524. book_id = book.BookId
  525. } else {
  526. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  527. if err != nil || bookResult.RoleId == conf.BookObserver {
  528. beego.Error("FindByIdentify => ", err)
  529. c.JsonResult(6002, "项目不存在或权限不足")
  530. }
  531. book_id = bookResult.BookId
  532. }
  533. if doc_id <= 0 {
  534. c.JsonResult(6001, "参数错误")
  535. }
  536. doc, err := models.NewDocument().Find(doc_id)
  537. if err != nil {
  538. beego.Error("Delete => ", err)
  539. c.JsonResult(6003, "删除失败")
  540. }
  541. // 如果文档所属项目错误
  542. if doc.BookId != book_id {
  543. c.JsonResult(6004, "参数错误")
  544. }
  545. // 递归删除项目下的文档以及子文档
  546. err = doc.RecursiveDocument(doc.DocumentId)
  547. if err != nil {
  548. c.JsonResult(6005, "删除失败")
  549. }
  550. // 重置文档数量统计
  551. models.NewBook().ResetDocumentNumber(doc.BookId)
  552. c.JsonResult(0, "ok")
  553. }
  554. // 获取文档内容
  555. func (c *DocumentController) Content() {
  556. c.Prepare()
  557. identify := c.Ctx.Input.Param(":key")
  558. docId, err := c.GetInt("doc_id")
  559. if err != nil {
  560. docId, _ = strconv.Atoi(c.Ctx.Input.Param(":id"))
  561. }
  562. bookId := 0
  563. autoRelease := false
  564. // 如果是超级管理员,则忽略权限
  565. if c.Member.IsAdministrator() {
  566. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  567. if err != nil {
  568. c.JsonResult(6002, "项目不存在或权限不足")
  569. }
  570. bookId = book.BookId
  571. autoRelease = book.AutoRelease == 1
  572. } else {
  573. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  574. if err != nil || bookResult.RoleId == conf.BookObserver {
  575. beego.Error("FindByIdentify => ", err)
  576. c.JsonResult(6002, "项目不存在或权限不足")
  577. }
  578. bookId = bookResult.BookId
  579. autoRelease = bookResult.AutoRelease
  580. }
  581. if docId <= 0 {
  582. c.JsonResult(6001, "参数错误")
  583. }
  584. if c.Ctx.Input.IsPost() {
  585. markdown := strings.TrimSpace(c.GetString("markdown", ""))
  586. content := c.GetString("html")
  587. version, _ := c.GetInt64("version", 0)
  588. isCover := c.GetString("cover")
  589. doc, err := models.NewDocument().Find(docId)
  590. if err != nil {
  591. c.JsonResult(6003, "读取文档错误")
  592. }
  593. if doc.BookId != bookId {
  594. c.JsonResult(6004, "保存的文档不属于指定项目")
  595. }
  596. if doc.Version != version && !strings.EqualFold(isCover, "yes") {
  597. beego.Info("%d|", version, doc.Version)
  598. c.JsonResult(6005, "文档已被修改确定要覆盖吗?")
  599. }
  600. history := models.NewDocumentHistory()
  601. history.DocumentId = docId
  602. history.Content = doc.Content
  603. history.Markdown = doc.Markdown
  604. history.DocumentName = doc.DocumentName
  605. history.ModifyAt = c.Member.MemberId
  606. history.MemberId = doc.MemberId
  607. history.ParentId = doc.ParentId
  608. history.Version = time.Now().Unix()
  609. history.Action = "modify"
  610. history.ActionName = "修改文档"
  611. if markdown == "" && content != "" {
  612. doc.Markdown = content
  613. } else {
  614. doc.Markdown = markdown
  615. }
  616. doc.Version = time.Now().Unix()
  617. doc.Content = content
  618. if err := doc.InsertOrUpdate(); err != nil {
  619. beego.Error("InsertOrUpdate => ", err)
  620. c.JsonResult(6006, "保存失败")
  621. }
  622. // 如果启用了文档历史,则添加历史文档
  623. ///如果两次保存的MD5值不同则保存为历史,否则忽略
  624. go func(history *models.DocumentHistory) {
  625. if c.EnableDocumentHistory && cryptil.Md5Crypt(history.Markdown) != cryptil.Md5Crypt(doc.Markdown) {
  626. _, err = history.InsertOrUpdate()
  627. if err != nil {
  628. beego.Error("DocumentHistory InsertOrUpdate => ", err)
  629. }
  630. }
  631. }(history)
  632. //如果启用了自动发布
  633. if autoRelease {
  634. go func(identify string) {
  635. models.NewBook().ReleaseContent(bookId)
  636. }(identify)
  637. }
  638. c.JsonResult(0, "ok", doc)
  639. }
  640. doc, err := models.NewDocument().Find(docId)
  641. if err != nil {
  642. c.JsonResult(6003, "文档不存在")
  643. }
  644. attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
  645. if err == nil {
  646. doc.AttachList = attach
  647. }
  648. c.JsonResult(0, "ok", doc)
  649. }
  650. //
  651. //func (c *DocumentController) GetDocumentById(id string) (doc *models.Document, err error) {
  652. // doc = models.NewDocument()
  653. // if doc_id, err := strconv.Atoi(id); err == nil {
  654. // doc, err = doc.Find(doc_id)
  655. // if err != nil {
  656. // return nil, err
  657. // }
  658. // } else {
  659. // doc, err = doc.FindByFieldFirst("identify", id)
  660. // if err != nil {
  661. // return nil, err
  662. // }
  663. // }
  664. //
  665. // return doc, nil
  666. //}
  667. // 导出
  668. func (c *DocumentController) Export() {
  669. c.Prepare()
  670. identify := c.Ctx.Input.Param(":key")
  671. if identify == "" {
  672. c.ShowErrorPage(500,"参数错误")
  673. }
  674. output := c.GetString("output")
  675. token := c.GetString("token")
  676. // 如果没有开启匿名访问则跳转到登录
  677. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  678. promptUserToLogIn(c)
  679. return
  680. }
  681. if !conf.GetEnableExport() {
  682. c.ShowErrorPage(500,"系统没有开启导出功能")
  683. }
  684. bookResult := models.NewBookResult()
  685. if c.Member != nil && c.Member.IsAdministrator() {
  686. book, err := models.NewBook().FindByIdentify(identify)
  687. if err != nil {
  688. if err == orm.ErrNoRows {
  689. c.ShowErrorPage(404,"项目不存在")
  690. } else {
  691. beego.Error("查找项目时出错 ->",err)
  692. c.ShowErrorPage(500,"查找项目时出错")
  693. }
  694. }
  695. bookResult = models.NewBookResult().ToBookResult(*book)
  696. } else {
  697. bookResult = isReadable(identify, token, c)
  698. }
  699. if !bookResult.IsDownload {
  700. c.ShowErrorPage(200,"当前项目没有开启导出功能")
  701. }
  702. if !strings.HasPrefix(bookResult.Cover, "http:://") && !strings.HasPrefix(bookResult.Cover, "https:://") {
  703. bookResult.Cover = conf.URLForWithCdnImage(bookResult.Cover)
  704. }
  705. if output == "markdown" {
  706. if bookResult.Editor != "markdown"{
  707. c.ShowErrorPage(500,"当前项目不支持Markdown编辑器")
  708. }
  709. p,err := bookResult.ExportMarkdown(c.CruSession.SessionID())
  710. if err != nil {
  711. c.ShowErrorPage(500,"导出文档失败")
  712. }
  713. c.Ctx.Output.Download(p, bookResult.BookName+".zip")
  714. c.StopRun()
  715. return
  716. }
  717. outputPath := filepath.Join(conf.GetExportOutputPath(), strconv.Itoa(bookResult.BookId))
  718. pdfpath := filepath.Join(outputPath, "book.pdf")
  719. epubpath := filepath.Join(outputPath, "book.epub")
  720. mobipath := filepath.Join(outputPath, "book.mobi")
  721. docxpath := filepath.Join(outputPath, "book.docx")
  722. if output == "pdf" && filetil.FileExists(pdfpath){
  723. c.Ctx.Output.Download(pdfpath, bookResult.BookName+".pdf")
  724. c.Abort("200")
  725. } else if output == "epub" && filetil.FileExists(epubpath){
  726. c.Ctx.Output.Download(epubpath, bookResult.BookName+".epub")
  727. c.Abort("200")
  728. } else if output == "mobi" && filetil.FileExists(mobipath) {
  729. c.Ctx.Output.Download(mobipath, bookResult.BookName+".mobi")
  730. c.Abort("200")
  731. } else if output == "docx" && filetil.FileExists(docxpath){
  732. c.Ctx.Output.Download(docxpath, bookResult.BookName+".docx")
  733. c.Abort("200")
  734. }else if output == "pdf" || output == "epub" || output == "docx" || output == "mobi"{
  735. if err := models.BackgroupConvert(c.CruSession.SessionID(),bookResult);err != nil && err != gopool.ErrHandlerIsExist{
  736. c.ShowErrorPage(500,"导出失败,请查看系统日志")
  737. }
  738. c.ShowErrorPage(200,"文档正在后台转换,请稍后再下载")
  739. }else{
  740. c.ShowErrorPage(200,"不支持的文件格式")
  741. }
  742. c.ShowErrorPage(404,"项目没有导出文件")
  743. }
  744. // 生成项目访问的二维码
  745. func (c *DocumentController) QrCode() {
  746. c.Prepare()
  747. identify := c.GetString(":key")
  748. book, err := models.NewBook().FindByIdentify(identify)
  749. if err != nil || book.BookId <= 0 {
  750. c.ShowErrorPage(404,"项目不存在")
  751. }
  752. uri := conf.URLFor("DocumentController.Index", ":key", identify)
  753. code, err := qr.Encode(uri, qr.L, qr.Unicode)
  754. if err != nil {
  755. beego.Error("生成二维码失败 ->",err)
  756. c.ShowErrorPage(500,"生成二维码失败")
  757. }
  758. code, err = barcode.Scale(code, 150, 150)
  759. if err != nil {
  760. beego.Error("生成二维码失败 ->",err)
  761. c.ShowErrorPage(500,"生成二维码失败")
  762. }
  763. c.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png")
  764. // imgpath := filepath.Join("cache","qrcode",identify + ".png")
  765. err = png.Encode(c.Ctx.ResponseWriter, code)
  766. if err != nil {
  767. beego.Error("生成二维码失败 ->",err)
  768. c.ShowErrorPage(500,"生成二维码失败")
  769. }
  770. }
  771. // 项目内搜索
  772. func (c *DocumentController) Search() {
  773. c.Prepare()
  774. identify := c.Ctx.Input.Param(":key")
  775. token := c.GetString("token")
  776. keyword := strings.TrimSpace(c.GetString("keyword"))
  777. if identify == "" {
  778. c.JsonResult(6001, "参数错误")
  779. }
  780. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  781. promptUserToLogIn(c)
  782. return
  783. }
  784. bookResult := isReadable(identify, token, c)
  785. docs, err := models.NewDocumentSearchResult().SearchDocument(keyword, bookResult.BookId)
  786. if err != nil {
  787. beego.Error(err)
  788. c.JsonResult(6002, "搜索结果错误")
  789. }
  790. if len(docs) < 0 {
  791. c.JsonResult(404, "没有数据库")
  792. }
  793. for _, doc := range docs {
  794. doc.BookId = bookResult.BookId
  795. doc.BookName = bookResult.BookName
  796. doc.Description = bookResult.Description
  797. doc.BookIdentify = bookResult.Identify
  798. }
  799. c.JsonResult(0, "ok", docs)
  800. }
  801. // 文档历史列表
  802. func (c *DocumentController) History() {
  803. c.Prepare()
  804. c.TplName = "document/history.tpl"
  805. identify := c.GetString("identify")
  806. docId, err := c.GetInt("doc_id", 0)
  807. pageIndex, _ := c.GetInt("page", 1)
  808. book_id := 0
  809. // 如果是超级管理员则忽略权限判断
  810. if c.Member.IsAdministrator() {
  811. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  812. if err != nil {
  813. beego.Error("FindByIdentify => ", err)
  814. c.Data["ErrorMessage"] = "项目不存在或权限不足"
  815. return
  816. }
  817. book_id = book.BookId
  818. c.Data["Model"] = book
  819. } else {
  820. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  821. if err != nil || bookResult.RoleId == conf.BookObserver {
  822. beego.Error("FindByIdentify => ", err)
  823. c.Data["ErrorMessage"] = "项目不存在或权限不足"
  824. return
  825. }
  826. book_id = bookResult.BookId
  827. c.Data["Model"] = bookResult
  828. }
  829. if docId <= 0 {
  830. c.Data["ErrorMessage"] = "参数错误"
  831. return
  832. }
  833. doc, err := models.NewDocument().Find(docId)
  834. if err != nil {
  835. beego.Error("Delete => ", err)
  836. c.Data["ErrorMessage"] = "获取历史失败"
  837. return
  838. }
  839. // 如果文档所属项目错误
  840. if doc.BookId != book_id {
  841. c.Data["ErrorMessage"] = "参数错误"
  842. return
  843. }
  844. historis, totalCount, err := models.NewDocumentHistory().FindToPager(docId, pageIndex, conf.PageSize)
  845. if err != nil {
  846. beego.Error("FindToPager => ", err)
  847. c.Data["ErrorMessage"] = "获取历史失败"
  848. return
  849. }
  850. c.Data["List"] = historis
  851. c.Data["PageHtml"] = ""
  852. c.Data["Document"] = doc
  853. if totalCount > 0 {
  854. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  855. c.Data["PageHtml"] = pager.HtmlPages()
  856. }
  857. }
  858. func (c *DocumentController) DeleteHistory() {
  859. c.Prepare()
  860. c.TplName = "document/history.tpl"
  861. identify := c.GetString("identify")
  862. docId, err := c.GetInt("doc_id", 0)
  863. historyId, _ := c.GetInt("history_id", 0)
  864. if historyId <= 0 {
  865. c.JsonResult(6001, "参数错误")
  866. }
  867. bookId := 0
  868. // 如果是超级管理员则忽略权限判断
  869. if c.Member.IsAdministrator() {
  870. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  871. if err != nil {
  872. beego.Error("FindByIdentify => ", err)
  873. c.JsonResult(6002, "项目不存在或权限不足")
  874. }
  875. bookId = book.BookId
  876. } else {
  877. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  878. if err != nil || bookResult.RoleId == conf.BookObserver {
  879. beego.Error("FindByIdentify => ", err)
  880. c.JsonResult(6002, "项目不存在或权限不足")
  881. }
  882. bookId = bookResult.BookId
  883. }
  884. if docId <= 0 {
  885. c.JsonResult(6001, "参数错误")
  886. }
  887. doc, err := models.NewDocument().Find(docId)
  888. if err != nil {
  889. beego.Error("Delete => ", err)
  890. c.JsonResult(6001, "获取历史失败")
  891. }
  892. // 如果文档所属项目错误
  893. if doc.BookId != bookId {
  894. c.JsonResult(6001, "参数错误")
  895. }
  896. err = models.NewDocumentHistory().Delete(historyId, docId)
  897. if err != nil {
  898. beego.Error(err)
  899. c.JsonResult(6002, "删除失败")
  900. }
  901. c.JsonResult(0, "ok")
  902. }
  903. //通过文档历史恢复文档
  904. func (c *DocumentController) RestoreHistory() {
  905. c.Prepare()
  906. c.TplName = "document/history.tpl"
  907. identify := c.GetString("identify")
  908. docId, err := c.GetInt("doc_id", 0)
  909. historyId, _ := c.GetInt("history_id", 0)
  910. if historyId <= 0 {
  911. c.JsonResult(6001, "参数错误")
  912. }
  913. bookId := 0
  914. // 如果是超级管理员则忽略权限判断
  915. if c.Member.IsAdministrator() {
  916. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  917. if err != nil {
  918. beego.Error("FindByIdentify => ", err)
  919. c.JsonResult(6002, "项目不存在或权限不足")
  920. }
  921. bookId = book.BookId
  922. } else {
  923. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  924. if err != nil || bookResult.RoleId == conf.BookObserver {
  925. beego.Error("FindByIdentify => ", err)
  926. c.JsonResult(6002, "项目不存在或权限不足")
  927. }
  928. bookId = bookResult.BookId
  929. }
  930. if docId <= 0 {
  931. c.JsonResult(6001, "参数错误")
  932. }
  933. doc, err := models.NewDocument().Find(docId)
  934. if err != nil {
  935. beego.Error("Delete => ", err)
  936. c.JsonResult(6001, "获取历史失败")
  937. }
  938. // 如果文档所属项目错误
  939. if doc.BookId != bookId {
  940. c.JsonResult(6001, "参数错误")
  941. }
  942. err = models.NewDocumentHistory().Restore(historyId, docId, c.Member.MemberId)
  943. if err != nil {
  944. beego.Error(err)
  945. c.JsonResult(6002, "删除失败")
  946. }
  947. c.JsonResult(0, "ok", doc)
  948. }
  949. func (c *DocumentController) Compare() {
  950. c.Prepare()
  951. c.TplName = "document/compare.tpl"
  952. historyId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  953. identify := c.Ctx.Input.Param(":key")
  954. bookId := 0
  955. editor := "markdown"
  956. // 如果是超级管理员则忽略权限判断
  957. if c.Member.IsAdministrator() {
  958. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  959. if err != nil {
  960. beego.Error("DocumentController.Compare => ", err)
  961. c.ShowErrorPage(403,"权限不足")
  962. return
  963. }
  964. bookId = book.BookId
  965. c.Data["Model"] = book
  966. editor = book.Editor
  967. } else {
  968. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  969. if err != nil || bookResult.RoleId == conf.BookObserver {
  970. beego.Error("FindByIdentify => ", err)
  971. c.ShowErrorPage(403,"权限不足")
  972. return
  973. }
  974. bookId = bookResult.BookId
  975. c.Data["Model"] = bookResult
  976. editor = bookResult.Editor
  977. }
  978. if historyId <= 0 {
  979. c.ShowErrorPage(60002, "参数错误")
  980. }
  981. history, err := models.NewDocumentHistory().Find(historyId)
  982. if err != nil {
  983. beego.Error("DocumentController.Compare => ", err)
  984. c.ShowErrorPage(60003, err.Error())
  985. }
  986. doc, err := models.NewDocument().Find(history.DocumentId)
  987. if doc.BookId != bookId {
  988. c.ShowErrorPage(60002, "参数错误")
  989. }
  990. c.Data["HistoryId"] = historyId
  991. c.Data["DocumentId"] = doc.DocumentId
  992. if editor == "markdown" {
  993. c.Data["HistoryContent"] = history.Markdown
  994. c.Data["Content"] = doc.Markdown
  995. } else {
  996. c.Data["HistoryContent"] = template.HTML(history.Content)
  997. c.Data["Content"] = template.HTML(doc.Content)
  998. }
  999. }
  1000. // 递归生成文档序列数组
  1001. func RecursiveFun(parentId int, prefix, dpath string, c *DocumentController, book *models.BookResult, docs []*models.Document, paths *list.List) {
  1002. for _, item := range docs {
  1003. if item.ParentId == parentId {
  1004. EachFun(prefix, dpath, c, book, item, paths)
  1005. for _, sub := range docs {
  1006. if sub.ParentId == item.DocumentId {
  1007. prefix += strconv.Itoa(item.ParentId) + strconv.Itoa(item.OrderSort) + strconv.Itoa(item.DocumentId)
  1008. RecursiveFun(item.DocumentId, prefix, dpath, c, book, docs, paths)
  1009. break
  1010. }
  1011. }
  1012. }
  1013. }
  1014. }
  1015. func EachFun(prefix, dpath string, c *DocumentController, book *models.BookResult, item *models.Document, paths *list.List) {
  1016. name := prefix + strconv.Itoa(item.ParentId) + strconv.Itoa(item.OrderSort) + strconv.Itoa(item.DocumentId)
  1017. fpath := dpath + "/" + name + ".html"
  1018. paths.PushBack(fpath)
  1019. f, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0777)
  1020. if err != nil {
  1021. beego.Error(err)
  1022. c.ShowErrorPage(500,"系统错误")
  1023. }
  1024. html, err := c.ExecuteViewPathTemplate("document/export.tpl", map[string]interface{}{"Model": book, "Lists": item, "BaseUrl": c.BaseUrl()})
  1025. if err != nil {
  1026. f.Close()
  1027. beego.Error(err)
  1028. c.ShowErrorPage(500,"系统错误")
  1029. }
  1030. buf := bytes.NewReader([]byte(html))
  1031. doc, err := goquery.NewDocumentFromReader(buf)
  1032. doc.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
  1033. if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
  1034. contentSelection.SetAttr("src", c.BaseUrl()+src)
  1035. }
  1036. })
  1037. html, err = doc.Html()
  1038. if err != nil {
  1039. f.Close()
  1040. beego.Error(err)
  1041. c.ShowErrorPage(500,"系统错误")
  1042. }
  1043. // html = strings.Replace(html, "<img src=\"/uploads", "<img src=\"" + c.BaseUrl() + "/uploads", -1)
  1044. f.WriteString(html)
  1045. f.Close()
  1046. }
  1047. // 判断用户是否可以阅读文档
  1048. func isReadable(identify, token string, c *DocumentController) *models.BookResult {
  1049. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  1050. if err != nil {
  1051. beego.Error(err)
  1052. c.ShowErrorPage(500,"项目不存在")
  1053. }
  1054. // 如果文档是私有的
  1055. if book.PrivatelyOwned == 1 && !c.Member.IsAdministrator() {
  1056. is_ok := false
  1057. if c.Member != nil {
  1058. _, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId)
  1059. if err == nil {
  1060. is_ok = true
  1061. }
  1062. }
  1063. if book.PrivateToken != "" && !is_ok {
  1064. // 如果有访问的 Token,并且该项目设置了访问 Token,并且和用户提供的相匹配,则记录到 Session 中。
  1065. // 如果用户未提供 Token 且用户登录了,则判断用户是否参与了该项目。
  1066. // 如果用户未登录,则从 Session 中读取 Token。
  1067. if token != "" && strings.EqualFold(token, book.PrivateToken) {
  1068. c.SetSession(identify, token)
  1069. } else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
  1070. c.ShowErrorPage(403,"权限不足")
  1071. }
  1072. } else if !is_ok {
  1073. c.ShowErrorPage(403,"权限不足")
  1074. }
  1075. }
  1076. bookResult := models.NewBookResult().ToBookResult(*book)
  1077. if c.Member != nil {
  1078. rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId)
  1079. if err == nil {
  1080. bookResult.MemberId = rel.MemberId
  1081. bookResult.RoleId = rel.RoleId
  1082. bookResult.RelationshipId = rel.RelationshipId
  1083. }
  1084. }
  1085. // 判断是否需要显示评论框
  1086. if bookResult.CommentStatus == "closed" {
  1087. bookResult.IsDisplayComment = false
  1088. } else if bookResult.CommentStatus == "open" {
  1089. bookResult.IsDisplayComment = true
  1090. } else if bookResult.CommentStatus == "group_only" {
  1091. bookResult.IsDisplayComment = bookResult.RelationshipId > 0
  1092. } else if bookResult.CommentStatus == "registered_only" {
  1093. bookResult.IsDisplayComment = true
  1094. }
  1095. return bookResult
  1096. }
  1097. func isUserLoggedIn(c *DocumentController) bool {
  1098. return c.Member != nil && c.Member.MemberId > 0
  1099. }
  1100. func promptUserToLogIn(c *DocumentController) {
  1101. beego.Info("Access " + c.Ctx.Request.URL.RequestURI() + " not permitted.")
  1102. beego.Info(" Access will be redirected to login page(SessionId: " + c.CruSession.SessionID() + ").")
  1103. if c.IsAjax() {
  1104. c.JsonResult(6000, "请重新登录。")
  1105. } else {
  1106. c.Redirect(conf.URLFor("AccountController.Login")+ "?url=" + url.PathEscape(conf.BaseUrl+ c.Ctx.Request.URL.RequestURI()), 302)
  1107. }
  1108. }