DocumentController.go 34 KB

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