@@ -11,6 +11,7 @@ import (
1111 "os"
1212 "path/filepath"
1313 "regexp"
14+ "sort"
1415 "strconv"
1516 "strings"
1617 "time"
@@ -195,7 +196,6 @@ func (this *BookController) Setting() {
195196
196197// SaveBook 保存书籍信息
197198func (this * BookController ) SaveBook () {
198-
199199 bookResult , err := this .IsPermission ()
200200 if err != nil {
201201 this .JsonResult (6001 , err .Error ())
@@ -239,6 +239,7 @@ func (this *BookController) SaveBook() {
239239 book .Lang = this .GetString ("lang" )
240240 book .AdTitle = this .GetString ("ad_title" )
241241 book .AdLink = this .GetString ("ad_link" )
242+ _ , book .NavJSON = this .parseBookNav ()
242243
243244 if err := book .Update (); err != nil {
244245 this .JsonResult (6006 , "保存失败" )
@@ -272,6 +273,47 @@ func (this *BookController) SaveBook() {
272273 this .JsonResult (0 , "ok" , bookResult )
273274}
274275
276+ func (this * BookController ) parseBookNav () (navs models.BookNavs , navStr string ) {
277+ var data struct {
278+ Name []string `json:"name"`
279+ URL []string `json:"url"`
280+ Sort []string `json:"sort"`
281+ Icon []string `json:"icon"`
282+ Color []string `json:"color"`
283+ Target []string `json:"target"`
284+ }
285+ b , _ := json .Marshal (this .Ctx .Request .PostForm )
286+ json .Unmarshal (b , & data )
287+ lenName := len (data .Name )
288+ if lenName == 0 || lenName != len (data .URL ) || lenName != len (data .Sort ) || lenName != len (data .Icon ) || lenName != len (data .Color ) || lenName != len (data .Target ) {
289+ return
290+ }
291+
292+ for idx , name := range data .Name {
293+ name = strings .TrimSpace (name )
294+ url := strings .TrimSpace (data .URL [idx ])
295+ if url == "" || name == "" {
296+ continue
297+ }
298+ nav := models.BookNav {
299+ Name : name ,
300+ URL : url ,
301+ Color : strings .TrimSpace (data .Color [idx ]),
302+ Icon : strings .TrimSpace (data .Icon [idx ]),
303+ Target : strings .TrimSpace (data .Target [idx ]),
304+ }
305+ nav .Sort , _ = strconv .Atoi (strings .TrimSpace (data .Sort [idx ]))
306+ navs = append (navs , nav )
307+ }
308+ if len (navs ) > 0 {
309+ // 排序
310+ sort .Sort (navs )
311+ b , _ := json .Marshal (navs )
312+ navStr = string (b )
313+ }
314+ return
315+ }
316+
275317//设置书籍私有状态.
276318func (this * BookController ) PrivatelyOwned () {
277319
@@ -610,6 +652,47 @@ func (this *BookController) Create() {
610652 this .JsonResult (0 , "ok" , bookResult )
611653}
612654
655+ // Create 创建书籍.
656+ func (this * BookController ) Copy () {
657+ if opt , err := models .NewOption ().FindByKey ("ALL_CAN_WRITE_BOOK" ); err == nil {
658+ if opt .OptionValue == "false" && this .Member .Role == conf .MemberGeneralRole { // 读者无权限创建书籍
659+ this .JsonResult (1 , "普通读者无法创建书籍,如需创建书籍,请向管理员申请成为作者" )
660+ }
661+ }
662+ identify := strings .TrimSpace (this .GetString ("identify" , "" ))
663+ sourceIdentify := strings .TrimSpace (this .GetString ("source_identify" , "" ))
664+ sourceBook , err := models .NewBook ().FindByIdentify (sourceIdentify )
665+ if err != nil {
666+ this .JsonResult (1 , err .Error ())
667+ }
668+ existBook , _ := models .NewBook ().FindByIdentify (identify , "book_id" )
669+ if existBook != nil && existBook .BookId > 0 {
670+ this .JsonResult (1 , "请更换新的书籍标识" )
671+ }
672+
673+ // 如果是私有书籍,且不是团队的人,不允许拷贝该项目
674+ if sourceBook .PrivatelyOwned == 1 {
675+ rel , err := models .NewRelationship ().FindByBookIdAndMemberId (sourceBook .BookId , this .Member .MemberId )
676+ if err != nil || rel == nil || rel .RelationshipId == 0 {
677+ this .JsonResult (1 , "无拷贝书籍权限" )
678+ }
679+ }
680+ sourceBook .BookId = 0
681+ sourceBook .BookName = strings .TrimSpace (this .GetString ("book_name" , "" ))
682+ sourceBook .Identify = identify
683+ sourceBook .Description = strings .TrimSpace (this .GetString ("description" , "" ))
684+ sourceBook .Author = strings .TrimSpace (this .GetString ("author" , "" ))
685+ sourceBook .AuthorURL = strings .TrimSpace (this .GetString ("author_url" , "" ))
686+ sourceBook .PrivatelyOwned , _ = strconv .Atoi (this .GetString ("privately_owned" ))
687+ sourceBook .MemberId = this .Member .MemberId
688+ err = sourceBook .Copy (sourceIdentify )
689+ if err != nil {
690+ this .JsonResult (1 , "拷贝书籍失败:" + err .Error ())
691+ }
692+
693+ this .JsonResult (0 , "拷贝书籍成功" )
694+ }
695+
613696// CreateToken 创建访问来令牌.
614697func (this * BookController ) CreateToken () {
615698 if this .forbidGeneralRole () {
@@ -702,6 +785,7 @@ func (this *BookController) Delete() {
702785// 发布书籍.
703786func (this * BookController ) Release () {
704787 identify := this .GetString ("identify" )
788+ force , _ := this .GetBool ("force" )
705789 bookId := 0
706790 if this .Member .IsAdministrator () {
707791 book , err := models .NewBook ().FindByFieldFirst ("identify" , identify )
@@ -727,13 +811,7 @@ func (this *BookController) Release() {
727811 bookId = book .BookId
728812 }
729813
730- if exist := utils .BooksRelease .Exist (bookId ); exist {
731- this .JsonResult (1 , "上次内容发布正在执行中,请稍后再操作" )
732- }
733-
734- go func (identify string ) {
735- models .NewDocument ().ReleaseContent (bookId , this .BaseUrl ())
736- }(identify )
814+ go models .NewDocument ().ReleaseContent (bookId , this .BaseUrl (), force )
737815
738816 this .JsonResult (0 , "发布任务已推送到任务队列,稍后将在后台执行。" )
739817}
@@ -1262,3 +1340,29 @@ func (this *BookController) Comment() {
12621340 }
12631341 this .JsonResult (1 , "书籍不存在" )
12641342}
1343+
1344+ // ExportMarkdown 将书籍导出为markdown
1345+ // 注意:系统管理员和书籍参与者有权限导出
1346+ func (this * BookController ) Export2Markdown () {
1347+ identify := this .GetString ("identify" )
1348+ if this .Member .MemberId == 0 {
1349+ this .JsonResult (1 , "请先登录" )
1350+ }
1351+ if ! this .Member .IsAdministrator () {
1352+ if _ , err := models .NewBookResult ().FindByIdentify (identify , this .Member .MemberId ); err != nil {
1353+ this .JsonResult (1 , "无操作权限" )
1354+ }
1355+ }
1356+ path , err := models .NewBook ().Export2Markdown (identify )
1357+ if err != nil {
1358+ this .JsonResult (1 , err .Error ())
1359+ }
1360+ defer func () {
1361+ os .Remove (path )
1362+ }()
1363+ attchmentName := filepath .Base (path )
1364+ if book , _ := models .NewBook ().FindByIdentify (identify , "book_name" , "book_id" ); book != nil && book .BookId > 0 {
1365+ attchmentName = book .BookName + ".zip"
1366+ }
1367+ this .Ctx .Output .Download (strings .TrimLeft (path , "./" ), attchmentName )
1368+ }
0 commit comments