book.go 18 KB

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