book.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "html/template"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/astaxie/beego"
  14. "github.com/astaxie/beego/logs"
  15. "github.com/astaxie/beego/orm"
  16. "github.com/lifei6671/mindoc/conf"
  17. "github.com/lifei6671/mindoc/graphics"
  18. "github.com/lifei6671/mindoc/models"
  19. "github.com/lifei6671/mindoc/utils"
  20. "github.com/lifei6671/mindoc/utils/pagination"
  21. "net/http"
  22. "github.com/lifei6671/mindoc/converter"
  23. "github.com/russross/blackfriday"
  24. )
  25. type BookController struct {
  26. BaseController
  27. }
  28. func (c *BookController) Index() {
  29. c.Prepare()
  30. c.TplName = "book/index.tpl"
  31. pageIndex, _ := c.GetInt("page", 1)
  32. books, totalCount, err := models.NewBook().FindToPager(pageIndex, conf.PageSize, c.Member.MemberId)
  33. if err != nil {
  34. logs.Error("BookController.Index => ", err)
  35. c.Abort("500")
  36. }
  37. for i,book := range books {
  38. books[i].Description = utils.StripTags(string(blackfriday.Run([]byte(book.Description))))
  39. }
  40. if totalCount > 0 {
  41. pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize)
  42. c.Data["PageHtml"] = pager.HtmlPages()
  43. } else {
  44. c.Data["PageHtml"] = ""
  45. }
  46. b, err := json.Marshal(books)
  47. if err != nil || len(books) <= 0 {
  48. c.Data["Result"] = template.JS("[]")
  49. } else {
  50. c.Data["Result"] = template.JS(string(b))
  51. }
  52. }
  53. // Dashboard 项目概要 .
  54. func (c *BookController) Dashboard() {
  55. c.Prepare()
  56. c.TplName = "book/dashboard.tpl"
  57. key := c.Ctx.Input.Param(":key")
  58. if key == "" {
  59. c.Abort("404")
  60. }
  61. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  62. if err != nil {
  63. if err == models.ErrPermissionDenied {
  64. c.Abort("403")
  65. }
  66. beego.Error(err)
  67. c.Abort("500")
  68. }
  69. c.Data["Description"] = template.HTML(blackfriday.Run([]byte(book.Description)))
  70. c.Data["Model"] = *book
  71. }
  72. // Setting 项目设置 .
  73. func (c *BookController) Setting() {
  74. c.Prepare()
  75. c.TplName = "book/setting.tpl"
  76. key := c.Ctx.Input.Param(":key")
  77. if key == "" {
  78. c.Abort("404")
  79. }
  80. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  81. if err != nil {
  82. if err == orm.ErrNoRows {
  83. c.Abort("404")
  84. }
  85. if err == models.ErrPermissionDenied {
  86. c.Abort("403")
  87. }
  88. c.Abort("500")
  89. }
  90. //如果不是创始人也不是管理员则不能操作
  91. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  92. c.Abort("403")
  93. }
  94. if book.PrivateToken != "" {
  95. book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  96. }
  97. c.Data["Model"] = book
  98. }
  99. //保存项目信息
  100. func (c *BookController) SaveBook() {
  101. bookResult, err := c.IsPermission()
  102. if err != nil {
  103. c.JsonResult(6001, err.Error())
  104. }
  105. book, err := models.NewBook().Find(bookResult.BookId)
  106. if err != nil {
  107. logs.Error("SaveBook => ", err)
  108. c.JsonResult(6002, err.Error())
  109. }
  110. bookName := strings.TrimSpace(c.GetString("book_name"))
  111. description := strings.TrimSpace(c.GetString("description", ""))
  112. commentStatus := c.GetString("comment_status")
  113. tag := strings.TrimSpace(c.GetString("label"))
  114. editor := strings.TrimSpace(c.GetString("editor"))
  115. autoRelease := strings.TrimSpace(c.GetString("auto_release")) == "on"
  116. publisher := strings.TrimSpace(c.GetString("publisher"))
  117. historyCount,_ := c.GetInt("history_count",0)
  118. isDownload := strings.TrimSpace(c.GetString("is_download")) == "on"
  119. if strings.Count(description, "") > 500 {
  120. c.JsonResult(6004, "项目描述不能大于500字")
  121. }
  122. if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
  123. commentStatus = "closed"
  124. }
  125. if tag != "" {
  126. tags := strings.Split(tag, ",")
  127. if len(tags) > 10 {
  128. c.JsonResult(6005, "最多允许添加10个标签")
  129. }
  130. }
  131. if editor != "markdown" && editor != "html" {
  132. editor = "markdown"
  133. }
  134. book.BookName = bookName
  135. book.Description = description
  136. book.CommentStatus = commentStatus
  137. book.Publisher = publisher
  138. book.Label = tag
  139. book.Editor = editor
  140. book.HistoryCount = historyCount
  141. book.IsDownload = 0
  142. if autoRelease {
  143. book.AutoRelease = 1
  144. } else {
  145. book.AutoRelease = 0
  146. }
  147. if isDownload {
  148. book.IsDownload = 0
  149. }else{
  150. book.IsDownload = 1
  151. }
  152. if err := book.Update(); err != nil {
  153. c.JsonResult(6006, "保存失败")
  154. }
  155. bookResult.BookName = bookName
  156. bookResult.Description = description
  157. bookResult.CommentStatus = commentStatus
  158. bookResult.Label = tag
  159. c.JsonResult(0, "ok", bookResult)
  160. }
  161. //设置项目私有状态.
  162. func (c *BookController) PrivatelyOwned() {
  163. status := c.GetString("status")
  164. if status != "open" && status != "close" {
  165. c.JsonResult(6003, "参数错误")
  166. }
  167. state := 0
  168. if status == "open" {
  169. state = 0
  170. } else {
  171. state = 1
  172. }
  173. bookResult, err := c.IsPermission()
  174. if err != nil {
  175. c.JsonResult(6001, err.Error())
  176. }
  177. //只有创始人才能变更私有状态
  178. if bookResult.RoleId != conf.BookFounder {
  179. c.JsonResult(6002, "权限不足")
  180. }
  181. book, err := models.NewBook().Find(bookResult.BookId)
  182. if err != nil {
  183. c.JsonResult(6005, "项目不存在")
  184. }
  185. book.PrivatelyOwned = state
  186. err = book.Update()
  187. if err != nil {
  188. logs.Error("PrivatelyOwned => ", err)
  189. c.JsonResult(6004, "保存失败")
  190. }
  191. c.JsonResult(0, "ok")
  192. }
  193. // Transfer 转让项目.
  194. func (c *BookController) Transfer() {
  195. c.Prepare()
  196. account := c.GetString("account")
  197. if account == "" {
  198. c.JsonResult(6004, "接受者账号不能为空")
  199. }
  200. member, err := models.NewMember().FindByAccount(account)
  201. if err != nil {
  202. logs.Error("FindByAccount => ", err)
  203. c.JsonResult(6005, "接受用户不存在")
  204. }
  205. if member.Status != 0 {
  206. c.JsonResult(6006, "接受用户已被禁用")
  207. }
  208. if member.MemberId == c.Member.MemberId {
  209. c.JsonResult(6007, "不能转让给自己")
  210. }
  211. bookResult, err := c.IsPermission()
  212. if err != nil {
  213. c.JsonResult(6001, err.Error())
  214. }
  215. err = models.NewRelationship().Transfer(bookResult.BookId, c.Member.MemberId, member.MemberId)
  216. if err != nil {
  217. logs.Error("Transfer => ", err)
  218. c.JsonResult(6008, err.Error())
  219. }
  220. c.JsonResult(0, "ok")
  221. }
  222. //上传项目封面.
  223. func (c *BookController) UploadCover() {
  224. bookResult, err := c.IsPermission()
  225. if err != nil {
  226. c.JsonResult(6001, err.Error())
  227. }
  228. book, err := models.NewBook().Find(bookResult.BookId)
  229. if err != nil {
  230. logs.Error("SaveBook => ", err)
  231. c.JsonResult(6002, err.Error())
  232. }
  233. file, moreFile, err := c.GetFile("image-file")
  234. defer file.Close()
  235. if err != nil {
  236. logs.Error("", err.Error())
  237. c.JsonResult(500, "读取文件异常")
  238. }
  239. ext := filepath.Ext(moreFile.Filename)
  240. if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") {
  241. c.JsonResult(500, "不支持的图片格式")
  242. }
  243. x1, _ := strconv.ParseFloat(c.GetString("x"), 10)
  244. y1, _ := strconv.ParseFloat(c.GetString("y"), 10)
  245. w1, _ := strconv.ParseFloat(c.GetString("width"), 10)
  246. h1, _ := strconv.ParseFloat(c.GetString("height"), 10)
  247. x := int(x1)
  248. y := int(y1)
  249. width := int(w1)
  250. height := int(h1)
  251. fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  252. filePath := filepath.Join("uploads", time.Now().Format("200601"), fileName+ext)
  253. path := filepath.Dir(filePath)
  254. os.MkdirAll(path, os.ModePerm)
  255. err = c.SaveToFile("image-file", filePath)
  256. if err != nil {
  257. logs.Error("", err)
  258. c.JsonResult(500, "图片保存失败")
  259. }
  260. defer func(filePath string) {
  261. os.Remove(filePath)
  262. }(filePath)
  263. //剪切图片
  264. subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height)
  265. if err != nil {
  266. logs.Error("graphics.ImageCopyFromFile => ", err)
  267. c.JsonResult(500, "图片剪切")
  268. }
  269. filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
  270. //生成缩略图并保存到磁盘
  271. err = graphics.ImageResizeSaveFile(subImg, 175, 230, filePath)
  272. if err != nil {
  273. logs.Error("ImageResizeSaveFile => ", err.Error())
  274. c.JsonResult(500, "保存图片失败")
  275. }
  276. url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  277. if strings.HasPrefix(url, "//") {
  278. url = string(url[1:])
  279. }
  280. oldCover := book.Cover
  281. book.Cover = url
  282. if err := book.Update(); err != nil {
  283. c.JsonResult(6001, "保存图片失败")
  284. }
  285. //如果原封面不是默认封面则删除
  286. if oldCover != conf.GetDefaultCover() {
  287. os.Remove("." + oldCover)
  288. }
  289. c.JsonResult(0, "ok", url)
  290. }
  291. // Users 用户列表.
  292. func (c *BookController) Users() {
  293. c.Prepare()
  294. c.TplName = "book/users.tpl"
  295. key := c.Ctx.Input.Param(":key")
  296. pageIndex, _ := c.GetInt("page", 1)
  297. if key == "" {
  298. c.Abort("404")
  299. }
  300. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  301. if err != nil {
  302. if err == models.ErrPermissionDenied {
  303. c.Abort("403")
  304. }
  305. c.Abort("500")
  306. }
  307. c.Data["Model"] = *book
  308. members, totalCount, err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId, pageIndex, 15)
  309. if totalCount > 0 {
  310. pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize)
  311. c.Data["PageHtml"] = pager.HtmlPages()
  312. } else {
  313. c.Data["PageHtml"] = ""
  314. }
  315. b, err := json.Marshal(members)
  316. if err != nil {
  317. c.Data["Result"] = template.JS("[]")
  318. } else {
  319. c.Data["Result"] = template.JS(string(b))
  320. }
  321. }
  322. // Create 创建项目.
  323. func (c *BookController) Create() {
  324. if c.Ctx.Input.IsPost() {
  325. book_name := strings.TrimSpace(c.GetString("book_name", ""))
  326. identify := strings.TrimSpace(c.GetString("identify", ""))
  327. description := strings.TrimSpace(c.GetString("description", ""))
  328. privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned"))
  329. comment_status := c.GetString("comment_status")
  330. if book_name == "" {
  331. c.JsonResult(6001, "项目名称不能为空")
  332. }
  333. if identify == "" {
  334. c.JsonResult(6002, "项目标识不能为空")
  335. }
  336. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, identify); !ok || err != nil {
  337. c.JsonResult(6003, "项目标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  338. }
  339. if strings.Count(identify, "") > 50 {
  340. c.JsonResult(6004, "文档标识不能超过50字")
  341. }
  342. if strings.Count(description, "") > 500 {
  343. c.JsonResult(6004, "项目描述不能大于500字")
  344. }
  345. if privatelyOwned != 0 && privatelyOwned != 1 {
  346. privatelyOwned = 1
  347. }
  348. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  349. comment_status = "closed"
  350. }
  351. book := models.NewBook()
  352. book.Cover = conf.GetDefaultCover()
  353. //如果客户端上传了项目封面则直接保存
  354. if file, moreFile, err := c.GetFile("image-file");err == nil {
  355. defer file.Close()
  356. ext := filepath.Ext(moreFile.Filename)
  357. //如果上传的是图片
  358. if strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".gif") || strings.EqualFold(ext, ".jpeg") {
  359. fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  360. filePath := filepath.Join("uploads", time.Now().Format("200601"), fileName + ext)
  361. path := filepath.Dir(filePath)
  362. os.MkdirAll(path, os.ModePerm)
  363. if err := c.SaveToFile("image-file", filePath); err == nil {
  364. url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  365. if strings.HasPrefix(url, "//") {
  366. url = string(url[1:])
  367. }
  368. book.Cover = url
  369. }
  370. }
  371. }
  372. if books, _ := book.FindByField("identify", identify); len(books) > 0 {
  373. c.JsonResult(6006, "项目标识已存在")
  374. }
  375. book.BookName = book_name
  376. book.Description = description
  377. book.CommentCount = 0
  378. book.PrivatelyOwned = privatelyOwned
  379. book.CommentStatus = comment_status
  380. book.Identify = identify
  381. book.DocCount = 0
  382. book.MemberId = c.Member.MemberId
  383. book.CommentCount = 0
  384. book.Version = time.Now().Unix()
  385. book.Editor = "markdown"
  386. book.Theme = "default"
  387. if err := book.Insert();err != nil {
  388. logs.Error("Insert => ", err)
  389. c.JsonResult(6005, "保存项目失败")
  390. }
  391. bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
  392. if err != nil {
  393. beego.Error(err)
  394. }
  395. c.JsonResult(0, "ok", bookResult)
  396. }
  397. c.JsonResult(6001, "error")
  398. }
  399. //导入
  400. func (c *BookController) Import() {
  401. file, moreFile, err := c.GetFile("import-file")
  402. if err == http.ErrMissingFile {
  403. c.JsonResult(6003, "没有发现需要上传的文件")
  404. }
  405. defer file.Close()
  406. beego.Info(moreFile.Filename)
  407. tempPath := filepath.Join(os.TempDir(),c.CruSession.SessionID())
  408. os.MkdirAll(tempPath,0766)
  409. tempPath = filepath.Join(tempPath,moreFile.Filename)
  410. err = c.SaveToFile("import-file", tempPath)
  411. converter.Resolve(tempPath)
  412. }
  413. // CreateToken 创建访问来令牌.
  414. func (c *BookController) CreateToken() {
  415. action := c.GetString("action")
  416. bookResult, err := c.IsPermission()
  417. if err != nil {
  418. if err == models.ErrPermissionDenied {
  419. c.JsonResult(403, "权限不足")
  420. }
  421. if err == orm.ErrNoRows {
  422. c.JsonResult(404, "项目不存在")
  423. }
  424. logs.Error("生成阅读令牌失败 =>", err)
  425. c.JsonResult(6002, err.Error())
  426. }
  427. book := models.NewBook()
  428. if _, err := book.Find(bookResult.BookId); err != nil {
  429. c.JsonResult(6001, "项目不存在")
  430. }
  431. if action == "create" {
  432. if bookResult.PrivatelyOwned == 0 {
  433. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  434. }
  435. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  436. if err := book.Update(); err != nil {
  437. logs.Error("生成阅读令牌失败 => ", err)
  438. c.JsonResult(6003, "生成阅读令牌失败")
  439. }
  440. c.JsonResult(0, "ok", c.BaseUrl()+beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  441. } else {
  442. book.PrivateToken = ""
  443. if err := book.Update(); err != nil {
  444. logs.Error("CreateToken => ", err)
  445. c.JsonResult(6004, "删除令牌失败")
  446. }
  447. c.JsonResult(0, "ok", "")
  448. }
  449. }
  450. // Delete 删除项目.
  451. func (c *BookController) Delete() {
  452. c.Prepare()
  453. bookResult, err := c.IsPermission()
  454. if err != nil {
  455. c.JsonResult(6001, err.Error())
  456. }
  457. if bookResult.RoleId != conf.BookFounder {
  458. c.JsonResult(6002, "只有创始人才能删除项目")
  459. }
  460. err = models.NewBook().ThoroughDeleteBook(bookResult.BookId)
  461. if err == orm.ErrNoRows {
  462. c.JsonResult(6002, "项目不存在")
  463. }
  464. if err != nil {
  465. logs.Error("删除项目 => ", err)
  466. c.JsonResult(6003, "删除失败")
  467. }
  468. c.JsonResult(0, "ok")
  469. }
  470. //发布项目.
  471. func (c *BookController) Release() {
  472. c.Prepare()
  473. identify := c.GetString("identify")
  474. bookId := 0
  475. if c.Member.IsAdministrator() {
  476. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  477. if err != nil {
  478. }
  479. bookId = book.BookId
  480. } else {
  481. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  482. if err != nil {
  483. if err == models.ErrPermissionDenied {
  484. c.JsonResult(6001, "权限不足")
  485. }
  486. if err == orm.ErrNoRows {
  487. c.JsonResult(6002, "项目不存在")
  488. }
  489. beego.Error(err)
  490. c.JsonResult(6003, "未知错误")
  491. }
  492. if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder && book.RoleId != conf.BookEditor {
  493. c.JsonResult(6003, "权限不足")
  494. }
  495. bookId = book.BookId
  496. }
  497. go func(identify string) {
  498. models.NewDocument().ReleaseContent(bookId)
  499. //当文档发布后,需要删除已缓存的转换项目
  500. outputPath := filepath.Join(beego.AppConfig.DefaultString("book_output_path", "cache"), strconv.Itoa(bookId))
  501. os.RemoveAll(outputPath)
  502. }(identify)
  503. c.JsonResult(0, "发布任务已推送到任务队列,稍后将在后台执行。")
  504. }
  505. //文档排序.
  506. func (c *BookController) SaveSort() {
  507. c.Prepare()
  508. identify := c.Ctx.Input.Param(":key")
  509. if identify == "" {
  510. c.Abort("404")
  511. }
  512. book_id := 0
  513. if c.Member.IsAdministrator() {
  514. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  515. if err != nil {
  516. }
  517. book_id = book.BookId
  518. } else {
  519. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  520. if err != nil {
  521. beego.Error("DocumentController.Edit => ", err)
  522. c.Abort("403")
  523. }
  524. if bookResult.RoleId == conf.BookObserver {
  525. c.JsonResult(6002, "项目不存在或权限不足")
  526. }
  527. book_id = bookResult.BookId
  528. }
  529. content := c.Ctx.Input.RequestBody
  530. var docs []map[string]interface{}
  531. err := json.Unmarshal(content, &docs)
  532. if err != nil {
  533. beego.Error(err)
  534. c.JsonResult(6003, "数据错误")
  535. }
  536. for _, item := range docs {
  537. if doc_id, ok := item["id"].(float64); ok {
  538. doc, err := models.NewDocument().Find(int(doc_id))
  539. if err != nil {
  540. beego.Error(err)
  541. continue
  542. }
  543. if doc.BookId != book_id {
  544. logs.Info("%s", "权限错误")
  545. continue
  546. }
  547. sort, ok := item["sort"].(float64)
  548. if !ok {
  549. beego.Info("排序数字转换失败 => ", item)
  550. continue
  551. }
  552. parent_id, ok := item["parent"].(float64)
  553. if !ok {
  554. beego.Info("父分类转换失败 => ", item)
  555. continue
  556. }
  557. if parent_id > 0 {
  558. if parent, err := models.NewDocument().Find(int(parent_id)); err != nil || parent.BookId != book_id {
  559. continue
  560. }
  561. }
  562. doc.OrderSort = int(sort)
  563. doc.ParentId = int(parent_id)
  564. if err := doc.InsertOrUpdate(); err != nil {
  565. fmt.Printf("%s", err.Error())
  566. beego.Error(err)
  567. }
  568. } else {
  569. fmt.Printf("文档ID转换失败 => %+v", item)
  570. }
  571. }
  572. c.JsonResult(0, "ok")
  573. }
  574. func (c *BookController) IsPermission() (*models.BookResult, error) {
  575. identify := c.GetString("identify")
  576. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  577. if err != nil {
  578. if err == models.ErrPermissionDenied {
  579. return book, errors.New("权限不足")
  580. }
  581. if err == orm.ErrNoRows {
  582. return book, errors.New("项目不存在")
  583. }
  584. return book, err
  585. }
  586. if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  587. return book, errors.New("权限不足")
  588. }
  589. return book, nil
  590. }