diff --git a/api.go b/api.go index d050b31..9c0fbc6 100644 --- a/api.go +++ b/api.go @@ -161,24 +161,31 @@ func apiPostDelete(c *gin.Context) { func apiPostAdd(c *gin.Context) { var err error - var publish bool + var do string var cid int defer func() { - if !publish { - if err == nil { - c.JSON(http.StatusOK, gin.H{"success": SUCCESS, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) - } else { - logd.Error(err) + switch do { + case "auto": // 自动保存 + if err != nil { c.JSON(http.StatusOK, gin.H{"fail": FAIL, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) + return + } + c.JSON(http.StatusOK, gin.H{"success": SUCCESS, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) + case "save": // 保存草稿 + if err != nil { + responseNotice(c, NOTICE_NOTICE, err.Error(), "") + return + } + c.Redirect(http.StatusFound, "/admin/manage-draft") + case "publish": // 发布 + if err != nil { + responseNotice(c, NOTICE_NOTICE, err.Error(), "") + return } - } - if err == nil { c.Redirect(http.StatusFound, "/admin/manage-posts") - return } - logd.Error(err) }() - do := c.PostForm("do") // save or publish + do = c.PostForm("do") // auto or save or publish slug := c.PostForm("slug") title := c.PostForm("title") text := c.PostForm("text") @@ -195,13 +202,12 @@ func apiPostAdd(c *gin.Context) { tags = strings.Split(tag, ",") } serieid := CheckSerieID(serie) - publish = CheckPublish(do) artc := &Article{ Title: title, Content: text, Slug: slug, CreateTime: CheckDate(date), - IsDraft: !publish, + IsDraft: do != "publish", Author: Ei.Username, SerieID: serieid, Tags: tags, @@ -214,7 +220,7 @@ func apiPostAdd(c *gin.Context) { return } cid = int(artc.ID) - if publish { + if !artc.IsDraft { ElasticIndex(artc) DoPings(slug) } diff --git a/check.go b/check.go index 02f9cbc..3c1cd77 100644 --- a/check.go +++ b/check.go @@ -42,7 +42,3 @@ func CheckSerieID(sid string) int32 { func CheckBool(str string) bool { return str == "true" || str == "1" } - -func CheckPublish(do string) bool { - return do == "publish" -} diff --git a/views/admin/post.html b/views/admin/post.html index e843226..570f028 100644 --- a/views/admin/post.html +++ b/views/admin/post.html @@ -271,7 +271,7 @@ $(document).ready(function() { locked = true; autoSave.text('正在保存'); - $.post(formAction, data + '&do=save', function(o) { + $.post(formAction, data + '&do=auto', function(o) { savedData = data; lastSaveTime = o.time; cid = o.cid;