[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) {
|
||||||
|
|
|
@ -45,5 +45,32 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{ template "shares/footer.html" .}}
|
{{ 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