DocumentController.go 35 KB

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