//分类查看 func (this *MainController) Category() { var ( page int pagesize int name string err error count int64 result map[string][]*models.Post ) name = this.Ctx.Input.Param(":name") beego.Debug("name", name) if page, err = strconv.Atoi(this.Ctx.Input.Param(":page")); err != nil || page < 1 { page = 1 } if pagesize, err = strconv.Atoi(this.getOption("pagesize")); err != nil || pagesize < 1 { pagesize = 20 } else { pagesize *= 2 } tagpost := new(models.TagPost) tag := new(models.Tag) tag.Name = name if tag.Read("Name") != nil { this.Abort("404") } query := tagpost.Query().Filter("tag_id", tag.Id).Filter("post_status", 0) count, _ = query.Count() result = make(map[string][]*models.Post) if count > 0 { var tp []*models.TagPost var list []*models.Post var pids []int = make([]int, 0) query.OrderBy("-post_time").Limit(pagesize, (page-1)*pagesize).All(&tp) for _, v := range tp { pids = append(pids, v.PostId) } new(models.Post).Query().Filter("id__in", pids).All(&list) for _, v := range list { year := v.PostTime.Format("2006") if _, ok := result[year]; !ok { result[year] = make([]*models.Post, 0) } result[year] = append(result[year], v) } } this.Data["tag"] = tag this.Data["page"] = page this.Data["pagesize"] = pagesize this.Data["count"] = count this.Data["result"] = result this.Data["pagebar"] = 1 //util.NewPager(page, int(count), pagesize, tag.Link()).ToString() this.setHeadMetas(tag.Name, tag.Name, tag.Name) this.display("category") }
//保存 func (this *ArticleController) Save() { var ( id int = 0 title string = strings.TrimSpace(this.GetString("title")) content string = this.GetString("content") tags string = strings.TrimSpace(this.GetString("tags")) urlname string = strings.TrimSpace(this.GetString("urlname")) color string = strings.TrimSpace(this.GetString("color")) timestr string = strings.TrimSpace(this.GetString("posttime")) status int = 0 istop int8 = 0 urltype int8 = 0 post models.Post ) if title == "" { this.showmsg("标题不能为空!") } id, _ = this.GetInt("id") status, _ = this.GetInt("status") if this.GetString("istop") == "1" { istop = 1 } if this.GetString("urltype") == "1" { urltype = 1 } if status != 1 && status != 2 { status = 0 } addtags := make([]string, 0) //标签过滤 if tags != "" { tagarr := strings.Split(tags, ",") for _, v := range tagarr { if tag := strings.TrimSpace(v); tag != "" { exists := false for _, vv := range addtags { if vv == tag { exists = true break } } if !exists { addtags = append(addtags, tag) } } } } if id < 1 { post.UserId = this.userid post.Author = this.username post.PostTime = this.getTime() post.UpdateTime = this.getTime() post.Insert() } else { post.Id = id if post.Read() != nil { goto RD } if post.Tags != "" { var tagobj models.Tag var tagpostobj models.TagPost oldtags := strings.Split(strings.Trim(post.Tags, ","), ",") //标签统计-1 tagobj.Query().Filter("name__in", oldtags).Update(orm.Params{"count": orm.ColValue(orm.Col_Minus, 1)}) //删掉tag_post表的记录 tagpostobj.Query().Filter("postid", post.Id).Delete() } } if len(addtags) > 0 { for _, v := range addtags { tag := models.Tag{Name: v} if tag.Read("Name") == orm.ErrNoRows { tag.Count = 1 tag.Insert() } else { tag.Count += 1 tag.Update("Count") } tp := models.TagPost{TagId: tag.Id, PostId: post.Id, PostStatus: int8(status), PostTime: this.getTime()} tp.Insert() } post.Tags = "," + strings.Join(addtags, ",") + "," } if posttime, err := time.Parse("2006-01-02 15:04:05", timestr); err == nil { post.PostTime = posttime } else { post.PostTime, _ = time.Parse("2006-01-02 15:04:05", post.PostTime.Format("2006-01-02 15:04:05")) } post.Status = int8(status) post.Title = title post.Color = color post.IsTop = istop post.Content = content post.UrlName = urlname post.UrlType = urltype post.UpdateTime = this.getTime() //post.Update( "title", "color", "is_top", "content", "url_name", "url_type", "updated", "post_time") post.Update("tags", "status", "title", "color", "is_top", "content", "url_name", "url_type", "post_time") RD: this.Redirect("/admin/article/list", 302) }