Skip to content

Commit 24d202c

Browse files
committed
图书接口增加权限,图书章节树自动悬浮
1 parent e610600 commit 24d202c

File tree

10 files changed

+212
-49
lines changed

10 files changed

+212
-49
lines changed

controller/book/book.go

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strconv"
77
"strings"
8+
"time"
89
"unicode/utf8"
910

1011
"github.com/gin-gonic/gin"
@@ -89,6 +90,10 @@ func Save(c *gin.Context, isEdit bool) {
8990
} else {
9091
//更新图书
9192
if err := model.DB.First(&updatedBook, bookData.ID).Error; err == nil {
93+
if updatedBook.UserID != user.ID {
94+
SendErrJSON("您没有权限执行此操作", c)
95+
return
96+
}
9297
updatedBook.ReadLimits = bookData.ReadLimits
9398
updatedBook.Name = bookData.Name
9499
updatedBook.CoverURL = bookData.CoverURL
@@ -158,6 +163,15 @@ func UpdateName(c *gin.Context) {
158163
SendErrJSON("错误的图书id", c)
159164
return
160165
}
166+
167+
userInter, _ := c.Get("user")
168+
user := userInter.(model.User)
169+
170+
if book.UserID != user.ID {
171+
SendErrJSON("您没有权限执行此操作", c)
172+
return
173+
}
174+
161175
book.Name = bookData.Name
162176
if err := model.DB.Save(&book).Error; err != nil {
163177
SendErrJSON("error", c)
@@ -187,6 +201,15 @@ func Publish(c *gin.Context) {
187201
SendErrJSON("错误的图书id", c)
188202
return
189203
}
204+
205+
userInter, _ := c.Get("user")
206+
user := userInter.(model.User)
207+
208+
if book.UserID != user.ID {
209+
SendErrJSON("您没有权限执行此操作", c)
210+
return
211+
}
212+
190213
book.Status = model.BookVerifySuccess
191214
if err := model.DB.Save(&book).Error; err != nil {
192215
fmt.Println(err.Error())
@@ -246,7 +269,7 @@ func List(c *gin.Context) {
246269

247270
if err := model.DB.Model(&model.Book{}).Where("read_limits <> ?", model.BookReadLimitsPrivate).
248271
Where("status <> ?", model.BookVerifyFail).Where("status <> ?", model.BookUnpublish).
249-
Find(&books).Error; err != nil {
272+
Order("created_at desc").Find(&books).Error; err != nil {
250273
fmt.Println(err.Error())
251274
SendErrJSON("error", c)
252275
return
@@ -287,7 +310,7 @@ func MyBooks(c *gin.Context) {
287310
pageSize := 20
288311
offset := (pageNo - 1) * pageSize
289312

290-
if err := model.DB.Model(&model.Book{}).Where("user_id = ?", user.ID).Offset(offset).Limit(pageSize).Find(&books).Error; err != nil {
313+
if err := model.DB.Model(&model.Book{}).Where("user_id = ?", user.ID).Offset(offset).Limit(pageSize).Order("created_at desc").Find(&books).Error; err != nil {
291314
fmt.Println(err.Error())
292315
SendErrJSON("error", c)
293316
return
@@ -310,7 +333,7 @@ func MyBooks(c *gin.Context) {
310333
})
311334
}
312335

313-
// UserPublicBooks 用户公开的图书
336+
// UserPublicBooks 用户公开的图书列表
314337
func UserPublicBooks(c *gin.Context) {
315338
SendErrJSON := common.SendErrJSON
316339

@@ -363,7 +386,7 @@ func UserPublicBooks(c *gin.Context) {
363386
})
364387
}
365388

366-
// Info 获取图书信息
389+
// Info 获取图书信息, 若图书是私有的,那么只有作者本人才能查看
367390
func Info(c *gin.Context) {
368391
SendErrJSON := common.SendErrJSON
369392
id, err := strconv.Atoi(c.Param("id"))
@@ -412,7 +435,7 @@ func Info(c *gin.Context) {
412435
})
413436
}
414437

415-
// Chapters 获取图书的所有章节
438+
// Chapters 获取图书的所有章节, 若图书是私有的,那么只有作者本人才能查看
416439
func Chapters(c *gin.Context) {
417440
SendErrJSON := common.SendErrJSON
418441
id, err := strconv.Atoi(c.Param("bookID"))
@@ -455,7 +478,7 @@ func Chapters(c *gin.Context) {
455478
})
456479
}
457480

458-
// Chapter 查询章节
481+
// Chapter 查询章节, 若图书是私有的,那么只有作者本人才能查看
459482
func Chapter(c *gin.Context) {
460483
SendErrJSON := common.SendErrJSON
461484
id, err := strconv.Atoi(c.Param("chapterID"))
@@ -581,8 +604,23 @@ func DeleteChapter(c *gin.Context) {
581604
SendErrJSON("错误的章节id", c)
582605
return
583606
}
584-
var sql = "DELETE FROM book_chapters WHERE id = ? OR parent_id = ?"
585-
if err := model.DB.Exec(sql, id, id).Error; err != nil {
607+
608+
var chapter model.BookChapter
609+
if err := model.DB.First(&chapter, id).Error; err != nil {
610+
SendErrJSON("错误的章节id", c)
611+
return
612+
}
613+
614+
userInter, _ := c.Get("user")
615+
user := userInter.(model.User)
616+
617+
if chapter.UserID != user.ID {
618+
SendErrJSON("您没有权限执行此操作", c)
619+
return
620+
}
621+
622+
var sql = "UPDATE book_chapters SET deleted_at = ? WHERE id = ? OR parent_id = ?"
623+
if err := model.DB.Exec(sql, time.Now(), id, id).Error; err != nil {
586624
fmt.Println(err.Error())
587625
SendErrJSON("error", c)
588626
return
@@ -623,6 +661,15 @@ func UpdateChapterContent(c *gin.Context) {
623661
SendErrJSON("错误的章节id", c)
624662
return
625663
}
664+
665+
userInter, _ := c.Get("user")
666+
user := userInter.(model.User)
667+
668+
if chapter.UserID != user.ID {
669+
SendErrJSON("您没有权限执行此操作", c)
670+
return
671+
}
672+
626673
chapter.Content = reqData.Content
627674
chapter.HTMLContent = reqData.HTMLContent
628675

@@ -666,6 +713,15 @@ func UpdateChapterName(c *gin.Context) {
666713
SendErrJSON("无效的章节id", c)
667714
return
668715
}
716+
717+
userInter, _ := c.Get("user")
718+
user := userInter.(model.User)
719+
720+
if chapter.UserID != user.ID {
721+
SendErrJSON("您没有权限执行此操作", c)
722+
return
723+
}
724+
669725
chapter.Name = reqData.Name
670726
if err := model.DB.Save(&chapter).Error; err != nil {
671727
fmt.Println(err.Error())

model/errorcode.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,10 @@ type errorCode struct {
1111

1212
// ErrorCode 错误码
1313
var ErrorCode = errorCode{
14-
SUCCESS : 0,
15-
ERROR : 1,
16-
NotFound : 404,
17-
LoginError : 1000, //用户名或密码错误
18-
LoginTimeout : 1001, //登录超时
19-
InActive : 1002, //未激活账号
14+
SUCCESS: 0,
15+
ERROR: 1,
16+
NotFound: 404,
17+
LoginError: 1000, //用户名或密码错误
18+
LoginTimeout: 1001, //登录超时
19+
InActive: 1002, //未激活账号
2020
}
21-
22-
23-
24-
25-
26-
27-
28-
29-

website/assets/styles/book/book.css

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,54 @@ body {
22
background-color: #e1e1e1;
33
}
44

5+
.common-body {
6+
width: auto;
7+
margin-bottom: 0!important;
8+
}
9+
510
.book-box {
6-
width: 1200px;
711
margin: 0 auto;
8-
background-color: #fff;
12+
text-align: center;
13+
}
14+
15+
.book-container {
16+
padding-right: 20px;
17+
display: inline-block;
18+
vertical-align: top;
19+
position: relative;
20+
background: #fff;
921
}
1022

1123
.book-header {
12-
height: 55px;
13-
padding-left: 20px;
1424
border-bottom: 1px #e2e2e2 solid;
25+
padding-left: 20px;
1526
}
1627

1728
.book-header h1 {
18-
line-height: 55px;
29+
line-height: 40px;
1930
font-size: 26px;
31+
padding: 8px 0;
2032
}
2133

2234
.book-tree-box {
35+
text-align: left;
2336
display: inline-block;
2437
vertical-align: top;
2538
width: 320px;
26-
padding-left: 20px;
27-
margin-top: 4px;
2839
margin-bottom: 4px;
2940
border-right: 1px solid #e2e2e2;
30-
min-height: 600px;
41+
overflow-y: auto;
42+
overflow-x: hidden;
43+
}
44+
45+
.book-tree-box-fixed {
46+
position: fixed;
47+
top: 0;
48+
}
49+
50+
.book-tree-container {
51+
background: #fff;
52+
padding-left: 20px;
3153
}
3254

3355
.book-tree-link:hover {
@@ -39,11 +61,15 @@ body {
3961
}
4062

4163
.book-content-box {
64+
text-align: left;
4265
width: 840px;
4366
display: inline-block;
4467
vertical-align: top;
4568
margin-left: 19px;
46-
margin-top: 4px;
69+
}
70+
71+
.book-content-box-expand {
72+
margin-left: 339px;
4773
}
4874

4975
.book-chapter-name {

website/layouts/nosidebar.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
export default {
1818
data () {
1919
return {
20-
siteConfig: this.$store.state.siteConfig,
21-
user: this.$store.state.user,
22-
userLoginVisible: !this.$store.state.user,
23-
messages: this.$store.state.messages,
24-
messageCount: this.$store.state.messageCount
20+
siteConfig: this.$store.state.siteConfig
2521
}
2622
},
2723
head () {

website/layouts/onlyfooter.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
export default {
1212
data () {
1313
return {
14-
siteConfig: this.$store.state.siteConfig,
15-
user: this.$store.state.user
14+
siteConfig: this.$store.state.siteConfig
1615
}
1716
},
1817
head () {

website/layouts/onlyheader.vue

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<template>
2+
<div>
3+
<app-header />
4+
<div class="common-body" style="margin-bottom: 20px;">
5+
<nuxt/>
6+
</div>
7+
<BackTop></BackTop>
8+
</div>
9+
</template>
10+
11+
<script>
12+
import Header from '~/components/Header'
13+
import config from '~/config'
14+
15+
export default {
16+
data () {
17+
return {
18+
siteConfig: this.$store.state.siteConfig
19+
}
20+
},
21+
head () {
22+
let siteConfig = this.siteConfig
23+
let allowBaiduAd = this.$store.state.baiduAdConfig.allowBaiduAd
24+
return {
25+
titleTemplate: '%s - ' + siteConfig.title,
26+
meta: [
27+
{ hid: 'description', name: 'description', content: siteConfig.description },
28+
{ name: 'keywords', content: siteConfig.keywords }
29+
],
30+
script: allowBaiduAd ? [ {src: config.baiduAdURL} ] : []
31+
}
32+
},
33+
middleware: 'appData',
34+
components: {
35+
'app-header': Header
36+
}
37+
}
38+
</script>

0 commit comments

Comments
 (0)