[ADD]文章编辑

This commit is contained in:
viletyy 2019-06-28 13:18:54 +08:00
parent fc2cb83bfd
commit 7e5f9a4561
7 changed files with 102 additions and 55 deletions

View File

@ -60,7 +60,6 @@ func (c *IndexController) GetDetail() {
c.TplName = "details.html"
}
//留言
// @router /message [get]
func (c *IndexController) GetMessage() {

View File

@ -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 {

View File

@ -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用于设置闲置的连接数

View File

@ -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

View File

@ -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) {

View File

@ -45,5 +45,32 @@
</div>
</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: '&#xe642;',//编辑的图标
bar2:'&#xe640;' //删除的图标
,click: function(type){
console.log(type);
if(type === 'bar1'){
//跳转到修改文章的页面
window.location.href="/note/edit/"+note.key
}
if(type === 'bar2'){
//删除的逻辑,等待实现。
}
}
});
}
});
</script>
</body>
</html>

View File

@ -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">