Skip to content

Commit 473120a

Browse files
committed
收录入口
1 parent 562bf05 commit 473120a

5 files changed

Lines changed: 91 additions & 8 deletions

File tree

commands/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func RegisterModel() {
7676
new(models.ReadCount),
7777
new(models.ReadRecord),
7878
new(models.Bookmark),
79+
new(models.SubmitBooks),
7980
models.NewRelateBook(),
8081
models.NewAuth(),
8182
models.NewBanner(),

controllers/SubmitController.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package controllers
22

3+
import (
4+
"strings"
5+
6+
"github.com/TruthHun/BookStack/models"
7+
"github.com/astaxie/beego"
8+
)
9+
310
type SubmitController struct {
411
BaseController
512
}
@@ -11,5 +18,29 @@ func (this *SubmitController) Index() {
1118
}
1219

1320
func (this *SubmitController) Post() {
21+
uid := this.Member.MemberId
22+
if uid <= 0 {
23+
this.JsonResult(1, "请先登录")
24+
}
25+
26+
form := &models.SubmitBooks{}
27+
err := this.ParseForm(form)
28+
if err != nil {
29+
beego.Error(err.Error())
30+
this.JsonResult(1, "数据解析失败")
31+
}
32+
33+
lowerURL := strings.ToLower(form.Url)
34+
if !(strings.HasPrefix(lowerURL, "https://") || strings.HasPrefix(lowerURL, "http://")) {
35+
this.JsonResult(1, "URL链接地址格式不正确")
36+
}
1437

38+
if form.Url == "" || form.Title == "" {
39+
this.JsonResult(1, "请填写必填项")
40+
}
41+
form.Uid = uid
42+
if err = form.Add(); err != nil {
43+
this.JsonResult(1, err.Error())
44+
}
45+
this.JsonResult(0, "提交成功,感谢您的分享。")
1546
}

models/submit_books.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
11
package models
22

3-
import "time"
3+
import (
4+
"time"
5+
6+
"github.com/astaxie/beego"
7+
"golang.org/x/tools/go/ssa/interp/testdata/src/errors"
8+
9+
"github.com/TruthHun/gotil/cryptil"
10+
"github.com/astaxie/beego/orm"
11+
)
412

513
type SubmitBooks struct {
614
Id int
7-
Uid int
8-
Title string
9-
Url string
15+
Uid int `orm:"index"`
16+
Title string `form:"title"`
17+
Url string `form:"url"`
1018
UrlMd5 string `orm:"size(32);unique"`
11-
Message string `xorm:"size(512)"`
19+
Message string `orm:"size(512)" form:"message"`
1220
Status bool
1321
CreatedAt time.Time
1422
UpdatedAt time.Time
1523
}
24+
25+
func (m *SubmitBooks) Add() (err error) {
26+
o := orm.NewOrm()
27+
m.UrlMd5 = cryptil.Md5Crypt(m.Url)
28+
m.CreatedAt = time.Now()
29+
m.UpdatedAt = m.CreatedAt
30+
exist := &SubmitBooks{}
31+
o.QueryTable(m).Filter("url_md5", m.UrlMd5).One(exist)
32+
if exist.Id > 0 { //已存在,直接返回
33+
return
34+
}
35+
36+
o.QueryTable(m).Filter("uid", m.Uid).OrderBy("-id").One(exist)
37+
if exist.Id > 0 && m.CreatedAt.Unix()-exist.CreatedAt.Unix() < 60 { // TODO: 后期从配置中查询
38+
err = errors.New("提交频率过快,1分钟内仅限提交一次")
39+
return
40+
}
41+
42+
_, err = o.Insert(m)
43+
if err != nil {
44+
beego.Error(err.Error())
45+
err = errors.New("数据写入失败")
46+
}
47+
48+
return
49+
}

utils/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747

4848
//更多存储类型有待扩展
4949
const (
50-
Version = "2.0 beta1"
50+
Version = "2.0 beta2"
5151
StoreLocal string = "local"
5252
StoreOss string = "oss"
5353
)

views/submit/index.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ <h4>收录说明</h4>
4040
<div>您在阅读外文开发文档,网速太慢或网络不稳定打不开?</div>
4141
<div><strong>我们帮您解决!</strong></div>
4242
<div>您只需要提交相关链接,我们来负责整理和排版,给你我以及每一位读者良好的阅读体验。</div>
43-
<div>书栈网内容转载分发,遵循 <strong>CC-BY-NC</strong><strong>知识共享-署名-非商用</strong>)协议,在转载分发过程中,注明来源,不作商用,并且保留内容中的打赏二维码和赞助链接</div>
43+
<div>书栈网内容转载分发,遵循 <strong>CC-BY-NC</strong><strong>知识共享-署名-非商用</strong>)协议,在转载分发过程中,注明来源,不作商用,并且保留内容中的打赏二维码和赞助链接等</div>
4444
</div>
4545
<h4>书栈开源</h4>
4646
<div class="help-block">
4747
<strong>书栈网 BookStack</strong>
48-
<p>Gitee: <a href="https://gitee.com/truthhun/bookstack" target="_blank">https://gitee.com/truthhun/bookstack</a></p>
48+
<p>Gitee: <a href="https://gitee.com/truthhun/BookStack" target="_blank">https://gitee.com/truthhun/BookStack</a></p>
4949
<p>GitHub: <a href="https://github.com/truthhun/bookstack" target="_blank">https://github.com/truthhun/bookstack</a></p>
5050
<div><strong>配套微信小程序 BookChat</strong></div>
5151
<p>Gitee: <a href="https://gitee.com/truthhun/BookChat" target="_blank">https://gitee.com/truthhun/BookChat</a></p>
@@ -56,5 +56,22 @@ <h4>书栈开源</h4>
5656
</div>
5757
</div>
5858
{{template "widgets/footer.html" .}}
59+
<script src="{{$.StaticDomain}}/static/layer/layer.js" type="text/javascript"></script>
60+
<script>
61+
$(function () {
62+
$(".submit-book [type=submit]").click(function (e) {
63+
e.preventDefault()
64+
var form = $(this).parents("form"),action=form.attr("action");
65+
$.post(action,form.serialize(),function (res) {
66+
layer.msg(res.message);
67+
if(res.errcode==0){
68+
setTimeout(function () {
69+
location.href="/"
70+
},1500)
71+
}
72+
})
73+
});
74+
})
75+
</script>
5976
</body>
6077
</html>

0 commit comments

Comments
 (0)