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