[ADD]文章编辑
This commit is contained in:
parent
fc2cb83bfd
commit
7e5f9a4561
|
@ -60,7 +60,6 @@ func (c *IndexController) GetDetail() {
|
|||
c.TplName = "details.html"
|
||||
|
||||
}
|
||||
|
||||
//留言
|
||||
// @router /message [get]
|
||||
func (c *IndexController) GetMessage() {
|
||||
|
|
|
@ -2,6 +2,7 @@ package controllers
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/jinzhu/gorm"
|
||||
"liteblog/models"
|
||||
|
@ -30,16 +31,11 @@ func (c *NoteController) Save() {
|
|||
// 判空
|
||||
title := c.GetMustString("title", "标题不能为空")
|
||||
content := c.GetMustString("content", "内容不能为空")
|
||||
log.Print(title)
|
||||
log.Print(content)
|
||||
// 获取内容摘要
|
||||
summary, _ := getSummary(content)
|
||||
log.Print(summary)
|
||||
// 根据key查询文章
|
||||
note, err := models.QueryNoteByKeyAndUserId(key, int(c.User.ID))
|
||||
var n models.Note
|
||||
log.Print(note)
|
||||
log.Print(err)
|
||||
if err != nil {
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
c.Abort500(syserrors.NewError("保存失败!", err))
|
||||
|
@ -53,6 +49,7 @@ func (c *NoteController) Save() {
|
|||
UserId: int(c.User.ID),
|
||||
}
|
||||
} else {
|
||||
log.Print("进入了修改过程")
|
||||
//查询不报错,这存在文章,那就做更新文章操作
|
||||
n = note
|
||||
n.Title = title
|
||||
|
@ -64,9 +61,25 @@ func (c *NoteController) Save() {
|
|||
if err := models.SaveNote(&n); err != nil {
|
||||
c.Abort500(syserrors.NewError("保存失败!", err))
|
||||
}
|
||||
c.JSONOk("成功")
|
||||
c.JSONOk("成功", fmt.Sprintf("/details/%s", key))
|
||||
}
|
||||
|
||||
// 编辑文章
|
||||
// @router /edit/:key [get]
|
||||
func (c *NoteController) EditPage() {
|
||||
key := c.Ctx.Input.Param(":key")
|
||||
note, err := models.QueryNoteByKeyAndUserId(key, int(c.User.ID))
|
||||
|
||||
if err != nil {
|
||||
c.Abort500(syserrors.NewError("文章不存在!", err))
|
||||
}
|
||||
|
||||
c.Data["note"] = note
|
||||
c.Data["key"] = key
|
||||
c.TplName = "note_new.html"
|
||||
}
|
||||
|
||||
|
||||
func (c *NoteController) NestPrepare() {
|
||||
c.MustLogin()
|
||||
if c.User.Role != 0 {
|
||||
|
|
|
@ -7,10 +7,18 @@ import (
|
|||
_ "github.com/jinzhu/gorm"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||||
"time"
|
||||
)
|
||||
|
||||
var db = initDbConnect()
|
||||
|
||||
type Model struct {
|
||||
ID uint `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `sql:"index" json:"deleted_at"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
//SetMaxOpenConns用于设置最大打开的连接数
|
||||
//SetMaxIdleConns用于设置闲置的连接数
|
||||
|
|
|
@ -2,11 +2,11 @@ package models
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
|
||||
type Note struct {
|
||||
gorm.Model
|
||||
Model
|
||||
Key string `gorm:"unique_index; not null"` // 文章唯一标识
|
||||
UserId int
|
||||
User User
|
||||
|
|
|
@ -1,16 +1,12 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"unique_index"`
|
||||
Email string `gorm:"unique_index"`
|
||||
Avatar string
|
||||
Pwd string
|
||||
Role int `gorm:"default:1"` // 0 管理员 1正常用户
|
||||
Model
|
||||
Name string `gorm:"unique_index" json:"name"`
|
||||
Email string `gorm:"unique_index" json:"email"`
|
||||
Avatar string `json:"avatar"`
|
||||
Pwd string `json:"-"`
|
||||
Role int `gorm:"default:1" json:"role"` // 0 管理员 1正常用户
|
||||
}
|
||||
|
||||
func QueryUserByEmailAndPassword(email, password string) (user User, err error) {
|
||||
|
|
|
@ -7,43 +7,70 @@
|
|||
{{ template "shares/link.html" .}}
|
||||
</head>
|
||||
<body class="lay-blog">
|
||||
{{ template "shares/header.html" .}}
|
||||
<div class="container-wrap">
|
||||
<div class="container container-message container-details">
|
||||
<div class="contar-wrap">
|
||||
<div class="item">
|
||||
{{ template "shares/note_tpl.html" .note}}
|
||||
</div>
|
||||
<a name="comment"> </a>
|
||||
<div class="comt layui-clear">
|
||||
<a href="javascript:;" class="pull-left">评论</a>
|
||||
<a href="comment.html" class="pull-right">写评论</a>
|
||||
{{ template "shares/header.html" .}}
|
||||
<div class="container-wrap">
|
||||
<div class="container container-message container-details">
|
||||
<div class="contar-wrap">
|
||||
<div class="item">
|
||||
{{ template "shares/note_tpl.html" .note}}
|
||||
</div>
|
||||
<a name="comment"> </a>
|
||||
<div class="comt layui-clear">
|
||||
<a href="javascript:;" class="pull-left">评论</a>
|
||||
<a href="comment.html" class="pull-right">写评论</a>
|
||||
</div>
|
||||
<div id="LAY-msg-box">
|
||||
<div class="info-item">
|
||||
<img class="info-img" src="/static/images/info-img.png" alt="">
|
||||
<div class="info-text">
|
||||
<p class="title count">
|
||||
<span class="name">一片空白</span>
|
||||
<span class="info-img like"><i class="layui-icon layui-icon-praise"></i>5.8万</span>
|
||||
</p>
|
||||
<p class="info-intr">父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。</p>
|
||||
</div>
|
||||
</div>
|
||||
<div id="LAY-msg-box">
|
||||
<div class="info-item">
|
||||
<img class="info-img" src="/static/images/info-img.png" alt="">
|
||||
<div class="info-text">
|
||||
<p class="title count">
|
||||
<span class="name">一片空白</span>
|
||||
<span class="info-img like"><i class="layui-icon layui-icon-praise"></i>5.8万</span>
|
||||
</p>
|
||||
<p class="info-intr">父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<img class="info-img" src="/static/images/info-img.png" alt="">
|
||||
<div class="info-text">
|
||||
<p class="title count">
|
||||
<span class="name">一片空白</span>
|
||||
<span class="info-img like"><i class="layui-icon layui-icon-praise"></i>5.8万</span>
|
||||
</p>
|
||||
<p class="info-intr">父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<img class="info-img" src="/static/images/info-img.png" alt="">
|
||||
<div class="info-text">
|
||||
<p class="title count">
|
||||
<span class="name">一片空白</span>
|
||||
<span class="info-img like"><i class="layui-icon layui-icon-praise"></i>5.8万</span>
|
||||
</p>
|
||||
<p class="info-intr">父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{ template "shares/footer.html" .}}
|
||||
</div>
|
||||
{{ template "shares/footer.html" .}}
|
||||
<script>
|
||||
var user = {{ .User }};
|
||||
var note = {userId:{{ .note.UserId }}, key:{{ .note.Key }}};
|
||||
|
||||
layui.use(['util','jquery'], function(){
|
||||
var util = layui.util,$= layui.jquery;
|
||||
//用户必须登陆,
|
||||
//登陆的用户的role必须为0,可以修改文章
|
||||
//登陆的用户的id,必须等于当前文章详情页的文章所属用户的id
|
||||
if(user&& user.id >0&&user.role===0&&user.id ===note.userId){
|
||||
util.fixbar({
|
||||
bar1: '',//编辑的图标
|
||||
bar2:'' //删除的图标
|
||||
,click: function(type){
|
||||
console.log(type);
|
||||
if(type === 'bar1'){
|
||||
//跳转到修改文章的页面
|
||||
window.location.href="/note/edit/"+note.key
|
||||
}
|
||||
if(type === 'bar2'){
|
||||
//删除的逻辑,等待实现。
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -31,7 +31,7 @@
|
|||
<label class="layui-form-label">标题</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="title" required=""
|
||||
value=""
|
||||
value="{{ .note.Title }}"
|
||||
lay-verify="required" placeholder="请输入标题"
|
||||
autocomplete="off" class="layui-input">
|
||||
</div>
|
||||
|
@ -40,7 +40,11 @@
|
|||
<div class="layui-form-item layui-form-text">
|
||||
<label class="layui-form-label">内容</label>
|
||||
<div class="layui-input-block">
|
||||
<div id="edit"></div>
|
||||
<div id="edit">
|
||||
{{ if .note }}
|
||||
{{ str2html .note.Content }}
|
||||
{{ end }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
|
|
Loading…
Reference in New Issue