[ADD]新增文章

This commit is contained in:
viletyy 2019-06-27 15:42:44 +08:00
parent 3b2c6346cd
commit 05d3f3f259
24 changed files with 10549 additions and 28 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/astaxie/beego"
"liteblog/models"
"liteblog/syserrors"
"github.com/satori/go.uuid"
)
const SESSION_USER_KEY = "SESSION_USER_KEY"
@ -100,3 +101,12 @@ func (ctx *BaseController) JSONOkData(count int, data interface{}) {
}
ctx.ServeJSON()
}
func (ctx *BaseController) UUID() string {
u, err := uuid.NewV4()
if err != nil {
ctx.Abort500(syserrors.NewError("系统错误", err))
}
return u.String()
}

92
controllers/note.go Normal file
View File

@ -0,0 +1,92 @@
package controllers
import (
"bytes"
"github.com/PuerkitoBio/goquery"
"github.com/jinzhu/gorm"
"liteblog/models"
"liteblog/syserrors"
"log"
"time"
)
type NoteController struct {
BaseController
}
// 写文章
// @router /new [get]
func (c *NoteController) NewPage() {
log.Print("开始写文章了...")
c.Data["key"] = c.UUID()
c.TplName = "note_new.html"
}
// 保存文章
// @router /save/:key [post]
func (c *NoteController) Save() {
// 得到页面传过来的key
key := c.Ctx.Input.Param(":key")
// 判空
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))
}
n = models.Note{
Key: key,
Summary: summary,
Title: title,
Content: content,
UserId: int(c.User.ID),
}
} else {
//查询不报错,这存在文章,那就做更新文章操作
n = note
n.Title = title
n.Content = content
n.Summary = summary
n.UpdatedAt = time.Now()
}
if err := models.SaveNote(&n); err != nil {
c.Abort500(syserrors.NewError("保存失败!", err))
}
c.JSONOk("成功")
}
func (c *NoteController) NestPrepare() {
c.MustLogin()
if c.User.Role != 0 {
c.Abort500(syserrors.NewError("您没有权限", nil))
}
}
func getSummary(content string) (string, error) {
var buf bytes.Buffer
buf.Write([]byte(content))
//用goquery来解析content
doc, err := goquery.NewDocumentFromReader(&buf)
if err != nil {
return "", err
}
// Text()得到body元素下的文本内容去掉html元素
str := doc.Find("body").Text()
// 截取字符
if len(str) > 600 {
str = str[0:600] + "..."
}
return str, nil
}

View File

@ -15,32 +15,13 @@ type UserController struct {
// 用户个人中心
// @router /user [get]
func (c *UserController) User() {
if c.IsLogin {
c.TplName = "user.html"
} else {
c.Redirect("/login",200)
}
}
// 登陆页面
// @router /login [get]
func (c *UserController) Login() {
if c.IsLogin {
c.Redirect("/user",200)
} else {
c.TplName = "login.html"
}
c.TplName = "user.html"
}
// 注册页面
// @router /reg [get]
func (c *UserController) Reg() {
if c.IsLogin {
c.Redirect("/user",200)
} else {
c.TplName = "reg.html"
}
c.TplName = "reg.html"
}
// 登陆接口
@ -76,9 +57,9 @@ func (c *UserController) SignUp() {
// 判断密码不能为空
password := c.GetMustString("password", "密码不能为空")
// 判断确认密码不能为空
password_confirmation := c.GetMustString("password_confirmation", "确认密码不能为空")
passwordConfirmation := c.GetMustString("password_confirmation", "确认密码不能为空")
if strings.Compare(password, password_confirmation) != 0 {
if strings.Compare(password, passwordConfirmation) != 0 {
c.Abort500(errors.New("密码与确认密码不一致"))
}
@ -100,7 +81,7 @@ func (c *UserController) SignUp() {
}); err != nil {
c.Abort500(syserrors.NewError("用户注册失败", err))
}
c.JSONOk("注册成功", "/login")
c.JSONOk("注册成功", "/user")
}
// 退出登陆接口

View File

@ -22,7 +22,7 @@ func init() {
// 自动迁移模式
db.AutoMigrate(&User{},
//&Model.UserDetailModel{},
&Note{},
//&Model.UserAuthsModel{},
)

24
models/note.go Normal file
View File

@ -0,0 +1,24 @@
package models
import "github.com/jinzhu/gorm"
type Note struct {
gorm.Model
Key string `gorm:"unique_index; not null"` // 文章唯一标识
UserId int
User User
Title string
Summary string `gorm:"type:text"`
Content string `gorm:"type:text"`
Visit int `gorm:"default:0"`
Praise int `gorm:"default:0"`
}
func QueryNoteByKeyAndUserId(key string, userId int) (note Note, err error) {
err = db.Model(&Note{}).First(&note, "`key` = ? AND `user_id` = ?", key, userId).Error
return note, err
}
func SaveNote(n *Note) error {
return db.Save(n).Error
}

View File

@ -6,9 +6,16 @@ import (
)
func init() {
//beego.Router("/", &controllers.IndexController{})
beego.ErrorController(&controllers.ErrorController{})
beego.Include(
&controllers.IndexController{},
&controllers.UserController{})
&controllers.UserController{},
)
beego.AddNamespace(
beego.NewNamespace(
"note",
beego.NSInclude(&controllers.NoteController{}),
),
)
}

Binary file not shown.

View File

@ -0,0 +1,29 @@
@CHARSET "UTF-8";
.w-e-toolbar {
flex-wrap: wrap;
-webkit-box-lines: multiple;
}
.w-e-toolbar .w-e-menu:hover{
z-index: 10002!important;
}
.w-e-menu a {
text-decoration: none;
}
.fullscreen-editor {
position: fixed !important;
width: 100% !important;
height: 100% !important;
left: 0px !important;
top: 0px !important;
background-color: white;
z-index: 9999;
}
.fullscreen-editor .w-e-text-container {
width: 100% !important;
height: 95% !important;
}

View File

@ -0,0 +1,20 @@
/**
*
*/
window.wangEditor.fullscreen = {
// editor create之后调用
init: function (editorSelector) {
var $ = layui.jquery;
$(editorSelector + " .w-e-toolbar").append('<div class="w-e-menu"><a class="_wangEditor_btn_fullscreen" href="###" onclick="window.wangEditor.fullscreen.toggleFullscreen(\'' + editorSelector + '\')">全屏</a></div>');
},
toggleFullscreen: function (editorSelector) {
var $ = layui.jquery;
$(editorSelector).toggleClass('fullscreen-editor');
if ($(editorSelector + ' ._wangEditor_btn_fullscreen').text() == '全屏') {
$(editorSelector + ' ._wangEditor_btn_fullscreen').text('退出全屏');
} else {
$(editorSelector + ' ._wangEditor_btn_fullscreen').text('全屏');
}
}
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

1
static/lib/wangEditor/wangEditor.min.css vendored Executable file

File diff suppressed because one or more lines are too long

4
static/lib/wangEditor/wangEditor.min.js vendored Executable file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

95
views/note_new.html Normal file
View File

@ -0,0 +1,95 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>关于-闲言轻博客</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
{{ template "shares/link.html". }}
<style>
.layui-form-label {
width: 50px;
}
.layui-input-block {
margin-left: 80px;
}
@media screen and (max-width: 450px) {
.layui-form-item .layui-input-inline {
margin: 0 0 10px 80px;
}
}
</style>
</head>
<body class="lay-blog">
{{ template "shares/header.html". }}
<div class="container-wrap">
<div class="container container-message container-details container-about">
<div class="contar-wrap">
<div class="item">
<div class="item-box">
<form class="layui-form layui-form-pane" action="">
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-block">
<input type="text" name="title" required=""
value=""
lay-verify="required" placeholder="请输入标题"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">内容</label>
<div class="layui-input-block">
<div id="edit"></div>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit=""
lay-filter="save">提交
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
{{ template "shares/footer.html". }}
<script type="text/javascript" src="/static/lib/wangEditor/wangEditor.min.js"></script>
<script>
layui.use(['form', 'jquery', 'layer', 'sysn'], function () {
var form = layui.form,
sysn = layui.sysn,
$ = layui.jquery,
layer = layui.layer;
//监听提交
form.on('submit(save)', function (formData) {
var fdata = formData.field;
fdata.content = editor.txt.html();
sysn.post("/note/save/{{ .key }}", fdata)
// .setTimeout(5000)
.success(function (data) {
layer.msg(data.msg);
if (data.action) {
setTimeout(function () {
window.location.href = data.action;
}, 300)
}
}).run();
return false;
});
// 初始化 wangEditor
var E = window.wangEditor;
var editor = new E('#edit');
// 图片不采用上传模式,直接保存到数据库
editor.customConfig.uploadImgShowBase64 = true;
editor.customConfig.pasteFilterStyle = false;
editor.customConfig.zIndex = 1;
editor.create();
});
</script>
</body>
</html>

View File

@ -30,8 +30,40 @@
<h4 class="item-title">
<p><i class="layui-icon layui-icon-speaker"></i>
{{.User.Name}},您已经登陆,是否<a href="/signout"><span>登出</span></a>
</p></h4>
</p>
</h4>
{{if eq .User.Role 0}}
<h4 class="item-title">
<p>
<a href="/note/new"><i class="layui-icon layui-icon-add-1">&#xe654;</i><span>新增文章</span></a>
</p>
</h4>
{{ end }}
{{ else }}
<form class="layui-form" action="">
<div class="layui-form-item">
<label class="layui-form-label">邮箱</label>
<div class="layui-input-inline">
<input type="text" name="email" required lay-verify="required"
placeholder="请输入邮箱" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-inline">
<input type="password" name="password" required lay-verify="required"
placeholder="请输入密码"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="login">登陆</button>
<a href="/reg" class="layui-btn layui-btn-primary">注册用户</a>
</div>
</div>
</form>
{{ end }}
</div>
</div>

BIN
wangEditor/fonts/w-e-icon.woff Executable file

Binary file not shown.

View File

@ -0,0 +1,29 @@
@CHARSET "UTF-8";
.w-e-toolbar {
flex-wrap: wrap;
-webkit-box-lines: multiple;
}
.w-e-toolbar .w-e-menu:hover{
z-index: 10002!important;
}
.w-e-menu a {
text-decoration: none;
}
.fullscreen-editor {
position: fixed !important;
width: 100% !important;
height: 100% !important;
left: 0px !important;
top: 0px !important;
background-color: white;
z-index: 9999;
}
.fullscreen-editor .w-e-text-container {
width: 100% !important;
height: 95% !important;
}

View File

@ -0,0 +1,20 @@
/**
*
*/
window.wangEditor.fullscreen = {
// editor create之后调用
init: function (editorSelector) {
var $ = layui.jquery;
$(editorSelector + " .w-e-toolbar").append('<div class="w-e-menu"><a class="_wangEditor_btn_fullscreen" href="###" onclick="window.wangEditor.fullscreen.toggleFullscreen(\'' + editorSelector + '\')">全屏</a></div>');
},
toggleFullscreen: function (editorSelector) {
var $ = layui.jquery;
$(editorSelector).toggleClass('fullscreen-editor');
if ($(editorSelector + ' ._wangEditor_btn_fullscreen').text() == '全屏') {
$(editorSelector + ' ._wangEditor_btn_fullscreen').text('退出全屏');
} else {
$(editorSelector + ' ._wangEditor_btn_fullscreen').text('全屏');
}
}
};

411
wangEditor/wangEditor.css Executable file

File diff suppressed because one or more lines are too long

4674
wangEditor/wangEditor.js Executable file

File diff suppressed because one or more lines are too long

1
wangEditor/wangEditor.min.css vendored Executable file

File diff suppressed because one or more lines are too long

4
wangEditor/wangEditor.min.js vendored Executable file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long