package article import ( "applet/app/db" "applet/app/e" "applet/app/md" "applet/app/svc" "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "github.com/gin-gonic/gin" "time" ) func List(c *gin.Context) { var req *md.ArticleListReq if err := c.ShouldBindJSON(&req); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } var resp md.ArticleListResp noticeList := make([]md.ArticleList, 0) NewArticleDb := implement.NewArticleDb(db.Db) notice, total, _ := NewArticleDb.FindArticleAndTotal(req.Page, req.Limit, "", "") resp.Total = total resp.SelectData = make([]map[string]interface{}, 0) NewArticleCateDb := implement.NewArticleCateDb(db.Db) pid, _ := NewArticleCateDb.FindArticleCate("1", "100", "0") if pid != nil { for _, v := range *pid { list := make([]map[string]string, 0) tmp := map[string]interface{}{ "name": v.Name, "value": utils.IntToStr(v.Id), "list": list, } cate, _ := NewArticleCateDb.FindArticleCate("1", "1000", utils.IntToStr(v.Id)) if cate != nil { for _, v1 := range *cate { tmp1 := map[string]string{ "name": v1.Name, "value": utils.IntToStr(v1.Id), } list = append(list, tmp1) } tmp["list"] = list } resp.SelectData = append(resp.SelectData, tmp) } } resp.CateData = make([]map[string]interface{}, 0) cate, _, _ := NewArticleCateDb.FindArticleCateAndTotalPid("1", "100") if cate != nil { for _, v := range *cate { list := make([]map[string]string, 0) tmp := map[string]interface{}{ "name": v.Name, "value": utils.IntToStr(v.Id), "list": list, } resp.CateData = append(resp.CateData, tmp) } } if notice != nil { for _, v := range *notice { tmp := md.ArticleList{ CateId: utils.IntToStr(v.CateId), Title: v.Title, Content: v.Content, Cover: v.Cover, CoverUrl: svc.GetOssUrl(v.Cover), Id: utils.IntToStr(v.Id), Sort: utils.IntToStr(v.Sort), IsShow: utils.IntToStr(v.IsShow), Pid: utils.IntToStr(v.Pid), } noticeList = append(noticeList, tmp) } } resp.List = noticeList e.OutSuc(c, resp, nil) return } func Save(c *gin.Context) { var req *md.ArticleSaveReq if err := c.ShouldBindJSON(&req); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } var data = new(model.Article) if utils.StrToInt(req.Id) > 0 { NewArticleDb := implement.NewArticleDb(db.Db) notice, _ := NewArticleDb.GetArticle(req.Id) if notice == nil { e.OutErr(c, 400, e.NewErr(400, "记录不存在")) return } data = notice } else { data.CreatedAt = int(time.Now().Unix()) db.Db.Insert(data) } data.UpdatedAt = int(time.Now().Unix()) data.Title = req.Title data.Cover = req.Cover data.Content = req.Content data.TypeId = utils.StrToInt(req.Pid) data.CateId = utils.StrToInt(req.CateId) data.Sort = utils.StrToInt(req.Sort) data.Pid = utils.StrToInt(req.Pid) data.IsShow = utils.StrToInt(req.IsShow) db.Db.Where("id=?", data.Id).Update(data) e.OutSuc(c, "success", nil) return } func Del(c *gin.Context) { var req *md.ArticleDelReq if err := c.ShouldBindJSON(&req); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } db.Db.In("id", req.Id).Delete(&model.Article{}) e.OutSuc(c, "success", nil) return }