DocumentController.go 35 KB

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