blogDetails.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <div class="page-box">
  3. <div class="blogDetails-box">
  4. <div class="name">
  5. {{ blogDetail.name }}
  6. </div>
  7. <div class="info-box">
  8. <div class="info">
  9. <div class="author">发布人: {{ blogDetail.author }}</div>
  10. <div class="createTime">发布时间: {{ blogDetail.createTime }}</div>
  11. <div class="likes">
  12. 点赞量:
  13. <i class="el-icon-thumb icon" @click="like"
  14. >&nbsp;{{ blogDetail.likes }}</i
  15. >
  16. </div>
  17. </div>
  18. <div class="search-box">
  19. <el-input
  20. placeholder="请输入内容"
  21. v-model="searchText"
  22. @keyup.enter="search()"
  23. >
  24. <el-button slot="append" @click="search()">搜索</el-button>
  25. </el-input>
  26. </div>
  27. </div>
  28. <div class="content-box">
  29. <div v-html="blogDetail.content" />
  30. </div>
  31. <div class="files-box">
  32. <div class="title">附件:</div>
  33. <div class="file" v-for="item in files" :key="item.id">
  34. <i class="el-icon-document icon" @click="download(item)"></i>
  35. </div>
  36. </div>
  37. </div>
  38. <div class="comments-box">
  39. <div class="title">评论: {{ total }}</div>
  40. <div class="comments">
  41. <div class="item-box" v-for="item in comments" :key="item.id">
  42. <div class="content-box">
  43. <div class="id">#{{ item.id }}</div>
  44. <div class="content">
  45. <div v-html="item.content"></div>
  46. </div>
  47. </div>
  48. <div class="info-box">
  49. <div class="author">评论用户: {{ item.author }}</div>
  50. <div class="createTime">评论时间: {{ item.createTime }}</div>
  51. <div class="likes">
  52. 点赞量:
  53. <i class="el-icon-thumb icon" @click="likeComment(item)"
  54. >&nbsp;{{ item.likes }}</i
  55. >
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. <div class="pagination-box">
  61. <el-pagination
  62. background
  63. layout="prev, pager, next"
  64. :current-page="size"
  65. :total="total"
  66. @current-change="sizeChange"
  67. :pager-count="this.pageSize"
  68. >
  69. </el-pagination>
  70. </div>
  71. <div class="editor-box" :key="tinymceKey">
  72. <vue2-tinymce-editor
  73. :options="options"
  74. ref="tinymce"
  75. v-model="content"
  76. ></vue2-tinymce-editor>
  77. <el-button class="push-comment" type="primary" @click="pushComment"
  78. >发布</el-button
  79. >
  80. </div>
  81. </div>
  82. <loginCom ref="loginCom" />
  83. </div>
  84. </template>
  85. <script>
  86. import { Vue2TinymceEditor } from "vue2-tinymce-editor";
  87. import loginCom from "../../components/login.vue";
  88. import {
  89. getBlogDetail,
  90. getComments,
  91. getFiles,
  92. like,
  93. likeComment,
  94. pushComment,
  95. } from "@/api";
  96. export default {
  97. name: "blogDetail",
  98. data() {
  99. return {
  100. blogDetail: {},
  101. searchText: "",
  102. total: 0,
  103. comments: [],
  104. files: [],
  105. pageSize: 15,
  106. size: 1,
  107. content: "",
  108. options: {
  109. language_url: "./tinymce/lang/zh-Hans.js",
  110. language: "zh-Hans", // 注意大小写
  111. },
  112. tinymceKey: "12131",
  113. };
  114. },
  115. components: { Vue2TinymceEditor, loginCom },
  116. mounted() {
  117. this.getBlogDetailFn();
  118. this.getFilesFn();
  119. this.getCommentsFn();
  120. },
  121. methods: {
  122. async getBlogDetailFn() {
  123. const res = await getBlogDetail(this.$route.query);
  124. console.log(17, res);
  125. this.blogDetail = res.data;
  126. },
  127. async getFilesFn() {
  128. const res = await getFiles(this.$route.query);
  129. this.files = res.data;
  130. console.log(17, res);
  131. },
  132. async getCommentsFn() {
  133. const params = {
  134. ...this.$route.query,
  135. size: this.size,
  136. pageSize: this.pageSize,
  137. };
  138. const res = await getComments(params);
  139. this.total = res.data.total;
  140. this.comments = res.data.list;
  141. console.log(17, res);
  142. },
  143. // 喜欢文章
  144. like() {
  145. like({
  146. id: this.blogDetail.id,
  147. }).then(() => {
  148. this.blogDetail.likes++;
  149. });
  150. },
  151. likeComment(item) {
  152. likeComment({
  153. id: item.id,
  154. }).then(() => {
  155. item.likes++;
  156. });
  157. },
  158. // 搜索
  159. search() {
  160. this.$router.push({
  161. name: "home",
  162. query: {
  163. search: this.searchText,
  164. },
  165. });
  166. },
  167. // 分页
  168. sizeChange(size) {
  169. console.log(200, size);
  170. this.size = size;
  171. this.getCommentsFn();
  172. },
  173. download(item) {
  174. console.log(167, item);
  175. window.open(item.download, "_blank");
  176. },
  177. async pushComment() {
  178. if (!this.content.length) {
  179. this.$message.error("请输入评论内容");
  180. return;
  181. }
  182. if (window.localStorage.getItem("token")) {
  183. let userInfo = window.localStorage.getItem("userInfo");
  184. if (typeof userInfo === "string" && userInfo) {
  185. userInfo = JSON.parse(userInfo);
  186. }
  187. const params = {
  188. userId: userInfo?.userId || "999",
  189. blogId: this.blogDetail?.id,
  190. content: this.content,
  191. };
  192. const res = await pushComment(params);
  193. console.log(res);
  194. this.pageSize = 15;
  195. this.size = 1;
  196. this.content = "";
  197. this.tinymceKey = new Date().getTime();
  198. this.getCommentsFn();
  199. } else {
  200. // 提示登陆
  201. this.$refs.loginCom.show();
  202. }
  203. },
  204. },
  205. };
  206. </script>
  207. <style lang="less" scoped>
  208. .page-box {
  209. .blogDetails-box {
  210. background-color: #ffffff;
  211. border-radius: 4px;
  212. padding: 0 24px;
  213. .name {
  214. padding-top: 24px;
  215. }
  216. .info-box {
  217. display: flex;
  218. justify-content: space-between;
  219. align-items: center;
  220. padding-bottom: 24px;
  221. .info {
  222. display: flex;
  223. font-size: 14px;
  224. flex: 1;
  225. align-items: center;
  226. .author {
  227. margin-right: 10px;
  228. }
  229. .createTime {
  230. margin-right: 10px;
  231. }
  232. .likes {
  233. .icon {
  234. user-select: none;
  235. cursor: pointer;
  236. }
  237. }
  238. }
  239. .search-box {
  240. width: 300px;
  241. }
  242. }
  243. .content-box {
  244. padding-bottom: 24px;
  245. }
  246. .files-box {
  247. display: flex;
  248. .title {
  249. font-weight: bold;
  250. }
  251. .file {
  252. margin-right: 5px;
  253. .icon {
  254. user-select: none;
  255. cursor: pointer;
  256. &:hover {
  257. color: #409eff;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. .comments-box {
  264. margin-top: 24px;
  265. background-color: #ffffff;
  266. border-radius: 4px;
  267. .title {
  268. text-align: left;
  269. font-weight: 600;
  270. padding-left: 24px;
  271. padding-top: 24px;
  272. }
  273. .comments {
  274. .item-box {
  275. margin-bottom: 24px;
  276. .content-box {
  277. display: flex;
  278. .id {
  279. width: 80px;
  280. }
  281. .content {
  282. flex: 1;
  283. text-align: left;
  284. }
  285. }
  286. .info-box {
  287. display: flex;
  288. padding-left: 24px;
  289. font-size: 14px;
  290. .author {
  291. margin-right: 10px;
  292. }
  293. .createTime {
  294. margin-right: 10px;
  295. }
  296. .icon {
  297. cursor: pointer;
  298. user-select: none;
  299. }
  300. }
  301. }
  302. }
  303. .pagination-box {
  304. //margin-bottom: 24px;
  305. }
  306. .editor-box {
  307. padding: 24px;
  308. position: relative;
  309. z-index: 1;
  310. .push-comment {
  311. position: absolute;
  312. right: 25px;
  313. top: 25px;
  314. z-index: 2;
  315. }
  316. }
  317. }
  318. }
  319. </style>