diff --git a/controllers/note.go b/controllers/note.go index b053572..d41e1bd 100644 --- a/controllers/note.go +++ b/controllers/note.go @@ -79,6 +79,18 @@ func (c *NoteController) EditPage() { c.TplName = "note_new.html" } +// 删除文章 +// @router /del/:key [post] +func (c *NoteController) Del() { + key := c.Ctx.Input.Param(":key") + + if err := models.DeleteNoteByUserIdAndKey(key, int(c.User.ID)); err != nil { + c.Abort500(syserrors.NewError("删除失败", err)) + } + + c.JSONOk("删除成功", "/") +} + func (c *NoteController) NestPrepare() { c.MustLogin() diff --git a/models/note.go b/models/note.go index 79d56df..f9f42b1 100644 --- a/models/note.go +++ b/models/note.go @@ -39,4 +39,9 @@ func QueryNoteByKey(key string) (note Note, err error) { func SaveNote(n *Note) error { return db.Save(n).Error +} + +func DeleteNoteByUserIdAndKey(key string, userId int) (err error) { + err = db.Delete(&Note{}, "`key` = ? AND `user_id` = ?", key, userId).Error + return err } \ No newline at end of file diff --git a/views/details.html b/views/details.html index 19658b6..3b9cd8c 100755 --- a/views/details.html +++ b/views/details.html @@ -66,6 +66,20 @@ } if(type === 'bar2'){ //删除的逻辑,等待实现。 + $.post("/note/del/"+note.key,function (data) { + if (data.code === 0) { + layer.msg("删除成功"); + if (data.action) { + setTimeout(function () { + window.location.href = data.action; + }, 300); + } + } else { + layer.msg("删除失败:" + data.msg); + } + }, "json").error(function () { + layer.msg("网络异常") + }); } } });