book.go 18 KB

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