Browse Source

博客数量&浏览量统计

layui
mail_yanpeng@163.com 6 years ago
parent
commit
61163cef6a
  1. 12
      controllers/BlogController.go
  2. 11
      controllers/PageController.go
  3. BIN
      data/beeblog.db
  4. 10
      service/BlogService.go
  5. 29
      service/UserService.go
  6. 12
      views/iframe/blog.html

12
controllers/BlogController.go

@ -24,7 +24,7 @@ func (this *BlogController) Save() {
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: 1}
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)}
err := service.SaveBlog(blog, labels)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
@ -32,6 +32,7 @@ func (this *BlogController) Save() {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
service.CountBlog(uid.(int64))
return
}
@ -39,12 +40,17 @@ func (this *BlogController) Get() {
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
blog, err := service.GetBlog(id)
if err == nil {
this.Data["Blog"] = blog
if err != nil {
this.Redirect("/500.html",302)
return
}
this.Data["Blog"] = blog
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.TplName = "blog.html"
service.CountBrows(blog.UserId)
service.EditBlogBrows(id)
return
}
func (this *BlogController) New() {

11
controllers/PageController.go

@ -1,6 +1,9 @@
package controllers
import "github.com/astaxie/beego"
import (
"github.com/astaxie/beego"
"beeblog/service"
)
type PageController struct {
beego.Controller
@ -8,6 +11,12 @@ type PageController struct {
// @router /iframe/blog [get]
func (this *PageController) Blog() {
cats, err := service.GetCats()
if err != nil {
this.Redirect("500.html", 302)
return
}
this.Data["Cats"] = cats
this.TplName = "iframe/blog.html"
}

BIN
data/beeblog.db

Binary file not shown.

10
service/BlogService.go

@ -23,6 +23,16 @@ func count(num int, size int, cat int64) (*utils.Page, error) {
return utils.PageUtil(totalCount, num, size), nil
}
func EditBlogBrows(id int64) {
o := orm.NewOrm()
blog := &models.Blog{Id: id}
err := o.Read(blog)
if err == nil {
blog.Browses += 1
o.Update(blog, "Browses")
}
}
func GetBlog(id int64) (*models.Blog, error) {
o := orm.NewOrm()
blog := &models.Blog{Id: id}

29
service/UserService.go

@ -33,3 +33,32 @@ func SaveUser(user *models.User) error {
}
return nil
}
func CountBlog(uid int64) {
o := orm.NewOrm()
totalItem := 0
err := o.Raw("SELECT count(*) FROM blog where delflag = 0 and user_id =? ", uid).QueryRow(&totalItem) //获取总条数
if err != nil {
return
}
user := &models.User{Id: uid}
err = o.Read(user)
if err != nil {
return
}
user.BlogCount = totalItem
o.Update(user, "BlogCount")
return
}
func CountBrows(uid int64){
o := orm.NewOrm()
browses := 0
o.Raw("UPDATE `user` SET `blog_browes` = (select SUM(browses) browses from blog where user_id = ?1) WHERE `id` = ?2 ", uid,uid).QueryRow(&browses) //获取总条数
return
}
func CountComments(uid int64) {
}
func CountLike(uid int64) {
}

12
views/iframe/blog.html

@ -28,11 +28,9 @@
<span>开发语言:</span>
<span>
<select class="form-control " style="width: 150px;display: inline-block;height: 32px" id="catory">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
{{range .Cats}}
<option value="{{.Id}}">{{.Title}}</option>
{{end}}
</select>
</span>
</div>
@ -48,8 +46,8 @@
<button type="button" class="btn btn-primary" style="height: 26px;margin-top: -5px;" id="addBtn">添加</button>
</div>
<div class="common" style="text-align: center">
<button type="button" class="btn btn-primary" id="saveBtn">首要</button>
<button type="button" class="btn btn-warning" id="cacelBtn">取消</button>
<button type="button" class="btn btn-primary btn-xl" id="saveBtn">保存</button>
<button type="button" class="btn btn-warning btn-xl" id="cacelBtn">取消</button>
</div>
</div>
</body>

Loading…
Cancel
Save