一、博客展示
1、api接口
import request from '@/utils/request.js'
export default {
limitSelect(current,size) {
return request({
url: `/service_blog/m-blog/limitSelect/${current}/${size}`,
method: 'get'
})
},
blogById(id){
return request({
url:`/service_blog/m-blog/blogById/${id}`,
method:'get',
})
},
deleteBlogById(id){
return request({
url:`/service_blog/m-blog/blogById/${id}`,
method:'delete',
})
},
updateBlogById(data){
return request({
url:`/service_blog/m-blog/blogById`,
method:'put',
data
})
},
insertBlog(data){
return request({
url:`/service_blog/m-blog/insertBlog`,
method:'post',
data
})
},
}
2、代码
<template>
<div class="blog">
<Handler id="handler"></Handler>
<el-row type="flex" justify="center">
<el-col :span="18">
<div class="block">
<el-timeline>
<el-timeline-item timestamp="" placement="top">
<el-card id="item" @click="blogDetailsave()">
<h4 style="text-align: center;" @click="blogDetailsave()">编辑一篇新的博客</h4>
<div class="buttonC"></div>
</el-card>
</el-timeline-item>
<el-timeline-item v-for="blog in blogs" :key="blog.id" :timestamp="blog.gmtModified" placement="top">
<el-card id="item">
<h4 @click="blogDetail(blog.id)">{{blog.title}}</h4>
<p @click="blogDetail(blog.id)">名字 提交于 {{blog.gmtModified}}</p>
<div class="buttonC">
<el-button id="a" type="primary" icon="el-icon-edit" circle @click="updateBlogById(blog.id,blog.userId)"></el-button>
<el-button id="a" type="success" icon="el-icon-check" circle @click="blogDetail(blog.id)"></el-button>
<el-button id="a" type="danger" icon="el-icon-delete" circle @click="deleteBlogById(blog.id,blog.userId)"></el-button>
</div>
</el-card>
</el-timeline-item>
</el-timeline>
</div>
<el-pagination
:page-size="size"
:current-page="current"
background
@current-change="currentChange"
style="opacity: 0.5;box-shadow: 5px 5px 5x #000000; "
layout="prev, pager, next"
:total="total">
</el-pagination>
</el-col>
</el-row>
</div>
</template>
<script>
import Handler from '@/components/Handler.vue'
import blog from '@/api/blog.js'
export default{
data(){
return{
current:1,
size:3,
total:1,
blogs:{},
userId:0,
}
},
components:{
Handler
},
created() {
if(this.$store.state.userInfo!=null||this.$store.state.userInfo!=undefined){
this.userId=this.$store.state.userInfo.id
}
this.init()
},
methods:{
init(){
this.limitSelect()
},
currentChange(val){
this.current=val;
this.limitSelect()
},
limitSelect(){
blog.limitSelect(this.current,this.size).then(response=>{
this.blogs=response.data.data.limitBlog.records
this.total=response.data.data.limitBlog.total
this.size=response.data.data.limitBlog.size
this.current=response.data.data.limitBlog.current
})
},
blogDetail(id){
this.$router.push({name:"BlogDetail",params:{BlogId:id}});
},
deleteBlogById(id,userId){
if(this.userId===userId){
this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
blog.deleteBlogById(id).then(response=>{
if(response.data.code===20000){
this.$message({
message: '删除成功',
type: 'success'
});
this.init();
}else{
this.$message.error(response.data.message);
}
}).catch(error=>{
this.$message.error(error.data.message);
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除',
});
});
}else{
this.$message.error("你没有权限删除此博客");
}
},
updateBlogById(id,userId){
if(this.userId===userId){
this.$router.push({name:"BlogEdit",params:{BlogId:id}});
}else{
this.$message.error("这篇文章不是你的哦!");
}
},
blogDetailsave(){
if(this.$store.state.loginFlag){
this.$router.push({name:"BlogAdd"})
}else{
this.$message({
type: 'info',
message: '请登录',
});
this.$router.push({name:"Login"})
}
}
}
}
</script>
<style>
#handler{
position: relative;
}
.blog{
background-image:url(http://www.lzqcode.com/templets/static/image/index_bg.jpg);
background-position: center;
background-attachment: fixed;
background-repeat: no-repeat;
position: relative;
height: 100vh;
}
.block{
height: 80vh;
}
#item{
min-width: 500px;
border:0;
background-color: rgba(0,0,0,0.5);
box-shadow: 5px 5px 5px rgba(0,0,0,0.5);
color: rgba(255,255,255,0.8);
transition: 0.5s;
overflow:hidden;
position: relative;
}
#item:hover{
box-shadow: 5px 5px 5px rgba(0,0,0,0.5);
transform: translateY(-10%);
transform: scale(1.02,1.02);
}
#item h4,#item p{
text-align: left;
text-shadow: 5px 5px 9px rgba(255,255,255,0.5);
font-family: "agency fb";
transition: 0.5s;
}
#item:hover h4,#item:hover p{
text-shadow: 5px 5px 9px rgba(255,255,255,1);
}
.buttonC{
display: inline-block;
position: absolute;
right: 2.5%;
bottom: 30%;
}
#a{
transition: 0.5s;
box-shadow:1px 2px 15px rgba(255,255,255,0.7);
opacity: 0.5;
}
#a:hover{
box-shadow:1px 2px 15px rgba(255,255,255,1);
opacity: 1;
}
</style>
二、博客编辑
1、整合mavon-editor编辑器
安装依赖 |
---|
npm install mavon-editor --save |
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
Vue.use(mavonEditor)
2、博客编辑页面
<template>
<div class="blogEdit">
<Handler id="handler"></Handler>
<el-container>
<el-header >
<el-form id="BlogForm" :inline="true" :model="blogMD" class="demo-form-inline" style="min-width: 950px;">
<el-form-item id="el-item" label="">
<el-input v-model="blogMD.title" placeholder="标题" style="min-width: 90vh;"></el-input>
</el-form-item>
<el-form-item id="el-item" label="">
<el-button type="primary" @click="save()">{{saveTitle}}</el-button>
</el-form-item>
<el-form-item id="el-item" label="">
<el-button type="success" @click="publish()">{{publishTilte}}</el-button>
</el-form-item>
<el-form-item id="el-item" label="">
<el-input v-model="blogMD.description" placeholder="简介" style="min-width: 117vh;"></el-input>
</el-form-item>
</el-form>
</el-header>
<div >
<mavon-editor v-model="blogMD.content" id="editor" @fullScreen="fullScreen()"></mavon-editor>
</div>
<!-- <el-footer>Footer</el-footer> -->
</el-container>
</div>
</template>
<script>
import Handler from '@/components/Handler.vue'
import blog from '@/api/blog.js'
export default{
data(){
return{
blogMD:{
id:'',
userId:'',
title:'',
description: "test",
content: "rest",
status: 0,
},
saveFlag:true,
saveTitle:'保存',
publishTilte:'未发布',
screenFlag:false,
}
},
created() {
if(this.$route.params.BlogId===undefined||
this.$route.params.BlogId===null||
this.$route.params.BlogId==="")
{
this.blogMD.userId=this.$store.state.userInfo.id;
}else{
this.saveFlag=false
this.saveTitle="修改完成"
this.blogMD.id=this.$route.params.BlogId
this.blogById();
}
if(this.blogMD.status===1){
this.publishTilte="已发布";
}else{
this.publishTilte="未发布";
}
},
methods:{
blogById(){
blog.blogById(this.$route.params.BlogId).then(response=>{
if(response.data.code===20000){
const data=response.data.data.blog
this.blogMD.userId=data.userId
this.blogMD.title=data.title
this.blogMD.description=data.description
this.blogMD.content=data.content
this.blogMD.status=data.status
}else{
this.$message.error(response.data.message);
}
}).catch(error=>{
this.$message.error(error.data.message);
})
},
publish(){
if(this.blogMD.status===0){
this.blogMD.status=1;
this.save();
}else{
this.save();
}
},
save(){
if(this.saveFlag){
blog.insertBlog(this.blogMD).then(response=>{
if(response.data.code===20000){
this.$message({
message: '保存成功',
type: 'success'
});
this.$router.push({name:"Blog"})
}else{
this.$message.error(response.data.message);
}
}).catch(error=>{
this.$message.error(error.data.message);
})
}else{
blog.updateBlogById(this.blogMD).then(response=>{
if(response.data.code===20000){
this.$message({
message: '修改成功',
type: 'success'
});
this.$router.push({name:"Blog"})
}else{
this.$message.error(response.data.message);
}
}).catch(error=>{
this.$message.error(error.data.message);
})
}
},
fullScreen(){
let a=document.querySelector("#editor");
if(this.screenFlag===true){
this.screenFlag=false;
a.style.maxWidth=130+'vh'
}else{
this.screenFlag=true;
a.style.maxWidth=300+'vh'
}
}
},
components:{
Handler
}
}
</script>
<style>
#handler{
position: relative;
}
.blogEdit{
background-image:url(http://www.lzqcode.com/templets/static/image/index_bg.jpg);
background-position: center;
background-attachment: fixed;
background-repeat: no-repeat;
position: relative;
height: 100vh;
overflow-y: scroll;
}
#editor{
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
min-height: 70vh;
max-width: 130vh;
overflow: hidden;
}
.el-header, .el-footer {
color: #333;
text-align: center;
line-height: 60px;
min-height: 15vh;
}
.el-header{
margin-top: 2%;
}
.el-main {
color: #333;
text-align: center;
line-height: 160px;
}
#BlogForm{
position: relative;
}
#el-item{
margin: 0 1vh;
}
</style>
三、博客详情页面
1、安装依赖
用来解析md格式文档 |
---|
用于解析md文档 npm install markdown-it --save |
md样式 npm install github-markdown-css |
2、开发页面
<template>
<div class="blogDetail">
<Handler id="handler"></Handler>
<div class="mblog">
<h2>{{ blogMD.title }}</h2>
<!-- <el-divider></el-divider> -->
<div class="content markdown-body" id="content" v-html="blogMD.content"></div>
</div>
</div>
</template>
<script>
import 'github-markdown-css/github-markdown.css'
import Handler from '@/components/Handler.vue'
import blog from '@/api/blog.js'
export default{
data(){
return{
blogMD:{
id:'',
userId:'',
title:'',
description: "test",
content: "rest",
status: 0,
},
}
},
created() {
this.blogById()
},
methods:{
blogById(){
blog.blogById(this.$route.params.BlogId).then(response=>{
if(response.data.code===20000){
const data=response.data.data.blog
this.blogMD.userId=data.userId
this.blogMD.title=data.title
this.blogMD.description=data.description
var MarkdownIt = require('markdown-it'),
md = new MarkdownIt();
var result = md.render(data.content);
this.blogMD.content = result
this.blogMD.status=data.status
}else{
this.$message.error(response.data.message);
}
}).catch(error=>{
this.$message.error(error.data.message);
})
},
},
components:{
Handler
}
}
</script>
<style>
#handler{
position: relative;
}
.blogDetail{
background-image:url(http://www.lzqcode.com/templets/static/image/index_bg.jpg);
background-position: center;
background-attachment: fixed;
background-repeat: no-repeat;
position: relative;
height: 100vh;
overflow-y: scroll;
}
#content{
background-color: rgba(255,255,255,0.5);
box-shadow: 5px 5px 15px rgba(0,0,0,0.5);
border-radius: 50px;
text-align: left;
margin: 0 10%;
padding: 5% 0;
min-width: 70vh;
min-height: 80vh;
}
</style>