BlogPost.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <el-container>
  3. <el-header height="220">
  4. <h3>文章列表</h3>
  5. <el-row style="margin-top: 10px">
  6. <el-select
  7. v-model="queryInfo.categoryId"
  8. clearable
  9. style="margin-left: 5px"
  10. @change="onSelectChange"
  11. >
  12. <el-option
  13. v-for="(item, index) in categoryList"
  14. :key="index"
  15. :label="item.name"
  16. :value="item.id"
  17. />
  18. </el-select>
  19. <el-button type="success" icon="el-icon-plus" style="margin-left: 5px" @click="handleAdd">添加</el-button>
  20. <el-button type="warning" icon="el-icon-delete" style="margin-left: 5px" @click="handleResetIndex">重置索引</el-button>
  21. </el-row>
  22. </el-header>
  23. <el-main>
  24. <el-table
  25. :data="dataList"
  26. border
  27. height="480"
  28. style="width: 100%"
  29. >
  30. <el-table-column
  31. fixed="left"
  32. label="No"
  33. type="index"
  34. />
  35. <el-table-column
  36. prop="title"
  37. label="标题"
  38. >
  39. <template slot-scope="scope">
  40. <router-link style="text-decoration-line: none" target="_blank" :to="`/blog/post/${scope.row.articleId}`">
  41. <span>{{ scope.row.title }}</span>
  42. </router-link>
  43. </template>
  44. </el-table-column>
  45. <el-table-column
  46. prop="category"
  47. label="分类"
  48. >
  49. <template slot-scope="scope">
  50. <el-tag>
  51. <span>{{ scope.row.category }}</span>
  52. </el-tag>
  53. </template>
  54. </el-table-column>
  55. <el-table-column
  56. prop="tags"
  57. label="标签"
  58. >
  59. <template slot-scope="scope">
  60. <el-tag v-for="(tag, index) in scope.row.tags" :key="index" style="margin: 5px" type="warning">
  61. <span>{{ tag }}</span>
  62. </el-tag>
  63. </template>
  64. </el-table-column>
  65. <el-table-column
  66. prop="editor"
  67. label="编辑器"
  68. >
  69. <template slot-scope="scope">
  70. <el-tag v-if="scope.row.editor === 'markdown'" type="info"><span>{{ scope.row.editor }}</span></el-tag>
  71. <el-tag v-else type="info"><span>{{ scope.row.editor }}</span></el-tag>
  72. </template>
  73. </el-table-column>
  74. <el-table-column
  75. prop="published"
  76. label="是否发布"
  77. >
  78. <template slot-scope="scope">
  79. <el-tag v-if="scope.row.published" type="success">
  80. <span>已发布</span>
  81. </el-tag>
  82. <el-tag v-else type="warning">
  83. <span>未发布</span>
  84. </el-tag>
  85. </template>
  86. </el-table-column>
  87. <el-table-column
  88. fixed="right"
  89. label="操作"
  90. width="280"
  91. >
  92. <template slot-scope="scope">
  93. <el-button
  94. style="margin-top: 5px; margin-left: 5px"
  95. size="mini"
  96. type="warning"
  97. @click="handleEdit(scope.$index, scope.row)"
  98. >修改</el-button>
  99. <el-button
  100. style="margin-top: 5px; margin-left: 5px"
  101. size="mini"
  102. type="danger"
  103. @click="handleDelete(scope.$index, scope.row)"
  104. >删除</el-button>
  105. </template>
  106. </el-table-column>
  107. </el-table>
  108. <el-pagination
  109. background
  110. :small="screenWidth <= 768"
  111. layout="prev, pager, next"
  112. :page-size="pageSize"
  113. :current-page="currentPage"
  114. :total="totalSize"
  115. @current-change="handleCurrentChange"
  116. @prev-click="handleCurrentChange"
  117. @next-click="handleCurrentChange"
  118. />
  119. <el-dialog
  120. title="编辑文章"
  121. append-to-body
  122. :visible.sync="addDialog"
  123. width="100%"
  124. center
  125. >
  126. <template>
  127. <div id="editor">
  128. <mavon-editor
  129. ref="md"
  130. style="height: 100%"
  131. :ishljs="true"
  132. :value="addForm.content"
  133. @change="onEditorChange"
  134. />
  135. </div>
  136. <el-form :model="addForm" label-width="80px" style="margin: 5px">
  137. <el-form-item label="标题">
  138. <el-input v-model="addForm.title" style="width: 30%; padding-right: 1px" />
  139. </el-form-item>
  140. <el-form-item label="分类">
  141. <el-select
  142. v-model="addForm.categoryId"
  143. style="padding-right: 1px"
  144. >
  145. <el-option
  146. v-for="(item, index) in categoryList"
  147. :key="index"
  148. :label="item.name"
  149. :value="item.id"
  150. />
  151. </el-select>
  152. </el-form-item>
  153. <el-form-item label="标签">
  154. <el-select
  155. v-model="addForm.tags"
  156. style="padding-right: 1px"
  157. placeholder="输入标签,用回车添加"
  158. clearable
  159. multiple
  160. filterable
  161. allow-create
  162. default-first-option
  163. >
  164. <el-option v-for="item in addForm.tags" :key="item" :label="item" :value="item" />
  165. </el-select>
  166. </el-form-item>
  167. <el-form-item label="是否发布">
  168. <el-select
  169. v-model="addForm.published"
  170. style="padding-right: 1px"
  171. >
  172. <el-option label="发布" value="1" />
  173. <el-option label="草稿" value="0" />
  174. </el-select>
  175. </el-form-item>
  176. <el-form-item label="文章类型">
  177. <el-select
  178. v-model="addForm.type"
  179. style="padding-right: 1px"
  180. >
  181. <el-option label="文章" value="1" />
  182. <el-option label="问题" value="2" />
  183. </el-select>
  184. </el-form-item>
  185. </el-form>
  186. <el-button type="success" style="margin: 5px" @click="onAdd">确定</el-button>
  187. </template>
  188. </el-dialog>
  189. </el-main>
  190. </el-container>
  191. </template>
  192. <script>
  193. import mavonEditor from 'mavon-editor'
  194. import 'mavon-editor/dist/css/index.css'
  195. import {
  196. editBlogPost,
  197. deleteBlogPost,
  198. getBlogCategoryList,
  199. getBlogEditPost,
  200. getBlogPostList,
  201. updateBlogPost, resetBlogPostIndex
  202. } from '@/api/blog'
  203. export default {
  204. name: 'BlogPost',
  205. components: {
  206. 'mavon-editor': mavonEditor.mavonEditor
  207. },
  208. props: {
  209. mdData: {
  210. type: String,
  211. default: ''
  212. }
  213. },
  214. data() {
  215. return {
  216. categoryList: [],
  217. queryInfo: {
  218. pn: 1,
  219. categoryId: null,
  220. title: '',
  221. published: 0
  222. },
  223. // 屏幕宽度, 为了控制分页条的大小
  224. screenWidth: document.body.clientWidth,
  225. currentPage: 1,
  226. pageSize: 10,
  227. totalSize: 0,
  228. dataList: [],
  229. addDialog: false,
  230. addForm: {
  231. articleId: null,
  232. editor: 'markdown',
  233. title: null,
  234. content: '',
  235. categoryId: '',
  236. tags: [],
  237. published: '1',
  238. type: '1'
  239. }
  240. }
  241. },
  242. created() {
  243. const pageNumber = this.$route.query.pn
  244. if (pageNumber !== undefined && pageNumber !== null) {
  245. this.currentPage = parseInt(pageNumber)
  246. this.queryInfo.pn = parseInt(pageNumber)
  247. }
  248. const categoryId = this.$route.query.categoryId
  249. if (categoryId !== undefined && categoryId !== null) {
  250. this.queryInfo.categoryId = parseInt(categoryId)
  251. }
  252. const title = this.$route.query.title
  253. if (title !== undefined && title !== null) {
  254. this.queryInfo.title = title
  255. }
  256. const published = this.$route.query.published
  257. if (published !== undefined && published !== null) {
  258. this.queryInfo.published = published
  259. }
  260. getBlogCategoryList().then(resp => {
  261. if (resp.code === 0) {
  262. this.categoryList = resp.data
  263. }
  264. })
  265. document.title = '文章列表'
  266. this.getData()
  267. },
  268. methods: {
  269. handleCurrentChange(pageNumber) {
  270. this.queryInfo.pn = pageNumber
  271. this.$router.push({
  272. path: '/background/blog/post',
  273. query: this.queryInfo
  274. })
  275. this.getData()
  276. // 回到顶部
  277. scrollTo(0, 0)
  278. },
  279. getData() {
  280. getBlogPostList(this.queryInfo).then(resp => {
  281. if (resp.code === 0) {
  282. const respData = resp.data
  283. this.dataList = respData.list
  284. this.totalSize = respData.totalSize
  285. } else {
  286. this.$message.error(resp.msg)
  287. }
  288. }).catch(error => {
  289. this.$message.error(error.message)
  290. })
  291. },
  292. handleAdd() {
  293. this.addDialog = true
  294. },
  295. handleEdit(index, row) {
  296. const queryInfo = {}
  297. queryInfo.articleId = row.articleId
  298. getBlogEditPost(queryInfo).then(resp => {
  299. if (resp.code === 0) {
  300. this.addForm = resp.data
  301. this.addDialog = true
  302. } else {
  303. this.$message.error(resp.msg)
  304. }
  305. }).catch(error => {
  306. this.$message.error(error.message)
  307. })
  308. },
  309. onAdd() {
  310. const formData = new FormData()
  311. formData.append('articleId', this.addForm.articleId)
  312. formData.append('editor', this.addForm.editor)
  313. formData.append('title', this.addForm.title)
  314. formData.append('content', this.addForm.content)
  315. formData.append('categoryId', this.addForm.categoryId)
  316. formData.append('tags', this.addForm.tags)
  317. formData.append('published', this.addForm.published)
  318. formData.append('type', this.addForm.published)
  319. editBlogPost(formData).then(resp => {
  320. this.addDialog = false
  321. this.$message.info(resp.msg)
  322. }).catch(error => {
  323. this.$message.error(error.message)
  324. })
  325. },
  326. handleDelete(index, row) {
  327. this.$confirm('确定要删除 ' + row.title + '?', '提示', {
  328. confirmButtonText: '确定',
  329. cancelButtonText: '取消',
  330. type: 'warning'
  331. }).then(() => {
  332. const formData = new FormData()
  333. formData.append('articleId', row.articleId)
  334. deleteBlogPost(formData).then(resp => {
  335. this.$message.info(resp.msg)
  336. this.getData()
  337. }).catch(error => {
  338. this.$message.error(error.message)
  339. })
  340. }).catch(() => {
  341. this.$message({
  342. type: 'info',
  343. message: '已取消'
  344. })
  345. })
  346. },
  347. onSelectChange() {
  348. this.currentPage = 1
  349. this.queryInfo.pn = 1
  350. this.$router.push({
  351. path: '/background/blog/post',
  352. query: this.queryInfo
  353. })
  354. this.getData()
  355. },
  356. onEditorChange() { // 也可以接收 value 和 render参数 获取内容
  357. // this.$emit('update:mdData', this.$refs.md.d_value)
  358. // 带有标签的内容
  359. // console.log(this.$refs.md.d_render)
  360. // markdown文本格式
  361. this.addForm.content = this.$refs.md.d_value
  362. },
  363. handleResetIndex() {
  364. this.$confirm('确定要重置文章索引?', '提示', {
  365. confirmButtonText: '确定',
  366. cancelButtonText: '取消',
  367. type: 'warning'
  368. }).then(() => {
  369. resetBlogPostIndex().then(resp => {
  370. this.$message.info(resp.msg)
  371. }).catch(error => {
  372. this.$message.error(error.message)
  373. })
  374. }).catch(() => {
  375. this.$message({
  376. type: 'info',
  377. message: '已取消'
  378. })
  379. })
  380. }
  381. }
  382. }
  383. </script>
  384. <style scoped>
  385. #editor {
  386. margin: auto;
  387. width: 100%;
  388. height: 480px;
  389. }
  390. </style>