nebula/pages/article/_article_id.vue

296 lines
10 KiB
Vue
Raw Normal View History

2020-06-19 17:37:45 +08:00
<template>
2020-06-23 17:40:43 +08:00
<el-row class="article__wrapper">
2020-08-02 23:55:08 +08:00
<el-col>
<el-card>
<div class="card-body d-flex flex-column article">
<div class="article__item">
<h1 class="list__title" v-html="article.articleTitle"></h1>
<el-row class="pt-5">
<el-col :xs="3" :sm="1" :xl="1">
<el-avatar v-if="article.articleAuthorAvatarUrl" :src="article.articleAuthorAvatarUrl"></el-avatar>
<el-avatar v-else src="https://static.rymcu.com/article/1578475481946.png"></el-avatar>
</el-col>
<el-col :xs="9" :sm="11" :xl="11">
<div style="margin-left: 1rem;">
<el-link rel="nofollow" @click="onRouter('user', article.articleAuthorName)" :underline="false"
2020-08-02 23:55:08 +08:00
class="text-default">{{ article.articleAuthorName }}
2020-07-03 11:04:25 +08:00
</el-link>
2020-08-02 23:55:08 +08:00
<small class="d-block text-muted">{{ article.timeAgo }}</small>
</div>
</el-col>
<el-col :xs="12" :sm="12" :xl="12" v-if="user" class="text-right">
<el-link rel="nofollow" :underline="false" title="总浏览数"><i class="el-icon-s-data"></i><span style="color: red;">{{ article.articleViewCount }}</span>
2020-08-02 23:55:08 +08:00
</el-link>
</el-col>
<el-col style="margin: 1rem 0;">
<el-col :span="12">
<el-tag
style="margin-right: 0.5rem;"
v-for="tag in article.tags"
:key="tag.idTag"
size="small"
effect="plain">
# {{ tag.tagTitle }}
</el-tag>
</el-col>
<el-col v-if="user" :span="12" style="text-align: right;">
<template v-if="user.idUser !== article.articleAuthorId">
2020-09-28 00:37:14 +08:00
<el-button size="mini" v-if="isFollow" @click="cancelFollowUser(article.articleAuthorId)" plain>取消关注</el-button>
<el-button size="mini" v-else @click="followUser(article.articleAuthorId)" plain>关注</el-button>
</template>
2020-09-28 00:37:14 +08:00
<el-button size="mini" v-if="hasPermissions" @click="handleCommand('edit')" plain>编辑文章</el-button>
<el-button size="mini" v-if="isAdmin" @click="handleCommand('editTag')" plain>编辑标签</el-button>
<el-button size="mini" @click="handleCommand('share')" plain>分享</el-button>
</el-col>
<el-col v-else :span="12" style="text-align: right;">
2020-09-28 00:37:14 +08:00
<el-button size="mini" @click="gotoLogin" plain>关注</el-button>
<el-button size="mini" @click="handleCommand('share')" plain>分享</el-button>
</el-col>
2020-08-02 23:55:08 +08:00
</el-col>
<el-col v-if="article.portfolios && article.portfolios.length > 0">
<el-col>
<h4>所属作品集</h4>
2020-06-23 17:40:43 +08:00
</el-col>
2020-08-02 23:55:08 +08:00
<el-col style="padding: 1rem">
2020-08-04 21:50:59 +08:00
<el-col v-for="portfolio in article.portfolios" :key="portfolio.idPortfolio" :xs="16" :xl="8">
<el-col :xs="4" :sm="4" :xl="4">
2020-08-02 23:55:08 +08:00
<el-avatar :size="24" :src="portfolio.headImgUrl"></el-avatar>
</el-col>
<el-col :xs="20" :sm="20" :xl="20">
<el-link rel="nofollow" @click="onRouter('portfolio', portfolio.idPortfolio)" :underline="false"
2020-08-02 23:55:08 +08:00
class="text-default">{{ portfolio.portfolioTitle }}
</el-link>
2020-06-23 17:40:43 +08:00
</el-col>
</el-col>
</el-col>
2020-08-02 23:55:08 +08:00
</el-col>
<el-col v-if="isShare" style="margin-bottom: 1rem;">
<share-box :url="shareData.shareUrl"></share-box>
</el-col>
</el-row>
<div class="pt-7 pipe-content__reset vditor-reset" id="articleContent" v-html="article.articleContent"
style="overflow: hidden;"></div>
2020-06-23 17:40:43 +08:00
</div>
2020-08-02 23:55:08 +08:00
</div>
</el-card>
2020-06-23 17:40:43 +08:00
</el-col>
2020-08-02 23:55:08 +08:00
<el-col>
<comment-box :fetching="isFetching" :user="user" :avatar="avatar" :title="article.articleTitle"
:post-id="routeArticleId"></comment-box>
2020-06-23 17:40:43 +08:00
</el-col>
2020-09-23 22:39:36 +08:00
<el-col>
<el-dialog :visible.sync="dialogVisible">
<edit-tags
:idArticle="article.idArticle"
:tags="article.articleTags"
@closeDialog="closeTagsDialog">
</edit-tags>
</el-dialog>
</el-col>
2020-06-23 17:40:43 +08:00
</el-row>
2020-06-19 17:37:45 +08:00
</template>
<script>
2020-06-29 21:30:58 +08:00
import Vue from 'vue';
2020-07-03 11:04:25 +08:00
import {mapState} from 'vuex';
2020-08-02 23:55:08 +08:00
import ShareBox from '~/components/widget/share';
2020-09-23 22:39:36 +08:00
import EditTags from '~/components/widget/tags';
2020-06-30 17:50:32 +08:00
2020-06-23 17:40:43 +08:00
export default {
name: "ArticleDetail",
2020-08-02 23:55:08 +08:00
components: {
2020-09-23 22:39:36 +08:00
ShareBox,
EditTags
2020-08-02 23:55:08 +08:00
},
2020-06-23 17:40:43 +08:00
validate({params, store}) {
return params.article_id && !isNaN(Number(params.article_id))
},
fetch({store, params, error}) {
return Promise.all([
store
.dispatch('article/fetchDetail', params)
.catch(err => error({statusCode: 404})),
2020-07-31 16:17:12 +08:00
store.dispatch('comment/fetchList', {post_id: params.article_id})
2020-06-23 17:40:43 +08:00
])
},
computed: {
...mapState({
article: state => state.article.detail.data,
isFetching: state => state.article.detail.fetching,
isMobile: state => state.global.isMobile,
2020-07-31 16:17:12 +08:00
user: state => state.oauth,
avatar: state => state.userInfo?.avatarURL
2020-07-03 11:04:25 +08:00
}),
hasPermissions() {
let account = this.$store.state.userInfo?.nickname;
2020-07-03 11:04:25 +08:00
if (account) {
if (account === this.article.articleAuthor.userNickname) {
return true;
}
}
return this.$store.getters.hasPermissions('blog_admin');
2020-07-31 16:17:12 +08:00
},
2020-09-23 22:39:36 +08:00
isAdmin() {
return this.$store.getters.hasPermissions('blog_admin');
},
2020-07-31 16:17:12 +08:00
routeArticleId() {
return Number(this.$route.params.article_id)
2020-07-03 11:04:25 +08:00
}
2020-06-23 17:40:43 +08:00
},
2020-07-03 11:04:25 +08:00
head() {
2020-06-30 17:50:32 +08:00
return {
2020-07-03 11:04:25 +08:00
title: this.article.articleTitle || 'RYMCU - 嵌入式知识学习交流平台',
2020-07-01 16:30:35 +08:00
meta: [
{
name: 'keywords',
content: this.article.articleTags || 'RYMCU'
},
{
name: 'description',
content: this.article.articlePreviewContent
},
{
name: 'site_name',
content: 'RYMCU'
},
{
name: 'url',
content: this.article.articlePermalink
},
{
name: 'og:title',
content: this.article.articleTitle + ' - RYMCU'
},
{
name: 'og:description',
content: this.article.articlePreviewContent
},
{
name: 'og:site_name',
content: 'RYMCU'
},
{
name: 'og:url',
content: this.article.articlePermalink
}
]
2020-06-30 17:50:32 +08:00
}
},
2020-06-23 17:40:43 +08:00
data() {
return {
loading: false,
2020-06-29 21:30:58 +08:00
isShare: false,
2020-09-23 22:39:36 +08:00
dialogVisible: false,
isFollow: false,
2020-06-29 21:30:58 +08:00
shareData: {},
2020-06-23 17:40:43 +08:00
}
2020-06-29 21:30:58 +08:00
},
methods: {
2020-07-03 11:04:25 +08:00
onRouter(name, data) {
2020-06-29 21:30:58 +08:00
this.$router.push(
{
2020-08-01 00:43:00 +08:00
path: `/${name}/${data}`
2020-06-29 21:30:58 +08:00
}
)
},
handleCommand(item) {
let _ts = this;
if (item === 'edit') {
_ts.$router.push({
2020-08-02 00:25:44 +08:00
path: `/article/post/${_ts.article.idArticle}`
2020-06-29 21:30:58 +08:00
})
2020-09-23 22:39:36 +08:00
} else if (item === 'editTag') {
_ts.$set(_ts, 'dialogVisible', true);
2020-06-29 21:30:58 +08:00
} else {
if (_ts.isShare) {
_ts.$set(_ts, 'isShare', false);
} else {
if (_ts.user) {
_ts.$axios.$get('/api/article/' + _ts.article.idArticle + '/share').then(function (res) {
if (res) {
_ts.$set(_ts, 'shareData', res);
_ts.$set(_ts, 'isShare', true);
}
});
} else {
_ts.$set(_ts, 'shareData', {shareUrl: _ts.article.articlePermalink});
_ts.$set(_ts, 'isShare', true);
}
}
2020-06-29 21:30:58 +08:00
}
},
gotoLogin() {
this.$router.push({
name: 'login'
})
2020-09-23 22:39:36 +08:00
},
closeTagsDialog() {
this.$set(this, 'dialogVisible', false);
this.$store.dispatch('article/fetchDetail', this.$route.params)
},
followUser(idUser) {
let _ts = this;
if (_ts.user) {
_ts.$axios.$post('/api/follow', {
followingId: idUser,
followingType: 0
}).then(function (res) {
_ts.$set(_ts, 'isFollow', res);
})
} else {
_ts.gotoLogin()
}
},
cancelFollowUser(idUser) {
let _ts = this;
if (_ts.user) {
_ts.$axios.$post('/api/follow/cancel-follow', {
followingId: idUser,
followingType: 0
}).then(function (res) {
_ts.$set(_ts, 'isFollow', res);
})
} else {
_ts.gotoLogin()
}
2020-06-29 21:30:58 +08:00
}
},
mounted() {
2020-07-31 16:17:12 +08:00
this.$store.commit('setActiveMenu', 'articleDetail');
2020-06-29 21:30:58 +08:00
Vue.nextTick(() => {
2020-07-03 11:04:25 +08:00
const previewElement = document.getElementById("articleContent");
// //const outLineElement = document.getElementById("articleToC");
// VditorPreview.setContentTheme('light');
2020-06-30 17:50:32 +08:00
Vue.VditorPreview.codeRender(previewElement, 'zh_CN');
2020-08-11 09:38:16 +08:00
Vue.VditorPreview.highlightRender({"enable": true, "lineNumber": true, "style": "github"}, previewElement);
2020-06-30 17:50:32 +08:00
Vue.VditorPreview.mathRender(previewElement, {
2020-07-03 11:04:25 +08:00
math: {"engine": "KaTeX", "inlineDigit": false, "macros": {}},
});
2020-06-30 17:50:32 +08:00
Vue.VditorPreview.mermaidRender(previewElement, ".language-mermaid");
Vue.VditorPreview.graphvizRender(previewElement);
Vue.VditorPreview.chartRender(previewElement);
Vue.VditorPreview.mindmapRender(previewElement);
Vue.VditorPreview.abcRender(previewElement);
Vue.VditorPreview.mediaRender(previewElement);
2020-08-14 01:09:27 +08:00
Vue.VditorPreview.lazyLoadImageRender(previewElement);
2020-07-03 11:04:25 +08:00
//VditorPreview.outlineRender(previewElement, outLineElement);
2020-08-02 23:55:08 +08:00
window.scrollTo(0, 0);
2020-06-29 21:30:58 +08:00
})
2020-06-19 17:37:45 +08:00
}
2020-06-23 17:40:43 +08:00
}
</script>
2020-06-19 17:37:45 +08:00
2020-06-23 17:40:43 +08:00
<style lang="scss">
2020-06-29 21:30:58 +08:00
@import "~vditor/src/assets/scss/index.scss";
2020-07-03 11:04:25 +08:00
2020-06-23 17:40:43 +08:00
.article__wrapper {
max-width: 980px;
margin: 0 auto;
display: block;
padding-left: 1rem;
padding-right: 1rem;
box-sizing: border-box;
}
2020-06-19 17:37:45 +08:00
</style>