blog.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div class="blog-page">
  3. <div class="title">撰写博客</div>
  4. <div class="edit-box">
  5. <div class="nav">
  6. <div class="nav-item">
  7. <div class="label">选择分类</div>
  8. <div class="value">
  9. <el-select
  10. size="mini"
  11. v-model="typeValue"
  12. multiple
  13. class="dom"
  14. placeholder="请选择"
  15. >
  16. <el-option
  17. v-for="item in typeList"
  18. :key="item"
  19. :label="item"
  20. :value="item"
  21. >
  22. </el-option>
  23. </el-select>
  24. </div>
  25. </div>
  26. <div class="nav-item">
  27. <div class="label">添加标签</div>
  28. <div class="value">
  29. <el-select
  30. class="dom"
  31. size="mini"
  32. v-model="tagValue"
  33. multiple
  34. placeholder="请选择"
  35. >
  36. <el-option
  37. v-for="item in tags"
  38. :key="item.name"
  39. :label="item.name"
  40. :value="item.id"
  41. >{{ item.name }}
  42. </el-option>
  43. </el-select>
  44. </div>
  45. </div>
  46. </div>
  47. <div class="edit">
  48. <vue2-tinymce-editor
  49. :options="options"
  50. ref="tinymce"
  51. v-model="content"
  52. ></vue2-tinymce-editor>
  53. <div class="btn">
  54. <el-button class="push-blog" type="primary" @click="pushBlog"
  55. >发布</el-button
  56. >
  57. </div>
  58. </div>
  59. <div class="upload-box">
  60. <el-upload
  61. class="upload-demo"
  62. ref="upload"
  63. action="string"
  64. :before-upload="onBeforeUploadImage"
  65. :http-request="UploadImage"
  66. :on-change="fileChange"
  67. :on-remove="removeFile"
  68. :file-list="fileList"
  69. >
  70. <el-button size="small" type="primary">点击上传</el-button>
  71. <div slot="tip" class="el-upload__tip">
  72. 只能上传jpg/png文件,且不超过500kb
  73. </div>
  74. </el-upload>
  75. </div>
  76. </div>
  77. </div>
  78. </template>
  79. <script>
  80. import { Vue2TinymceEditor } from "vue2-tinymce-editor";
  81. import {
  82. getBlogDetail,
  83. getTags,
  84. getTypes,
  85. putBlogDetail,
  86. getFiles,
  87. uploadFile,
  88. } from "@/api";
  89. export default {
  90. props: {
  91. userInfo: {
  92. type: Object,
  93. default: () => {},
  94. },
  95. },
  96. data() {
  97. return {
  98. typeValue: [],
  99. typeList: [],
  100. tagValue: [],
  101. tags: [],
  102. content: "",
  103. options: {
  104. language_url: "./tinymce/lang/zh-Hans.js",
  105. language: "zh-Hans", // 注意大小写
  106. },
  107. tinymceKey: "12131",
  108. // 记录旧的博文信息
  109. blogDetail: {},
  110. fileList: [], // 上传文件
  111. };
  112. },
  113. components: { Vue2TinymceEditor },
  114. mounted() {
  115. this.pageInit();
  116. },
  117. methods: {
  118. async pageInit() {
  119. const tags = await getTags({
  120. type: "all",
  121. userId: this.userInfo.id,
  122. token: window.localStorage.getItem("token"),
  123. });
  124. console.log(858585, tags);
  125. this.tags = tags.data;
  126. const typeList = await getTypes();
  127. this.typeList = typeList.data;
  128. if (this.$route.query?.id) {
  129. const res = await getBlogDetail(this.$route.query);
  130. console.log(93, res);
  131. this.blogDetail = res.data;
  132. this.content = res.data.content;
  133. this.typeValue = res.data.types;
  134. this.tagValue = res.data.tags;
  135. this.getFiles();
  136. }
  137. },
  138. async getFiles() {
  139. if (this.$route.query?.id) {
  140. const res = await getFiles({
  141. blogId: this.$route.query?.id,
  142. });
  143. // res.
  144. this.fileList = res.data;
  145. }
  146. },
  147. async pushBlog() {
  148. if (!this.userInfo?.id) return;
  149. const params = {
  150. ...this.blogDetail,
  151. types: this.typeValue,
  152. tags: this.tagValue,
  153. content: this.content,
  154. userId: this.userInfo.id,
  155. token: window.localStorage.getItem("token"),
  156. fileList: this.fileList,
  157. };
  158. console.log(110, params);
  159. const res = await putBlogDetail(params);
  160. console.log(res);
  161. this.$message({
  162. message: "发布成功",
  163. type: "success",
  164. });
  165. },
  166. onBeforeUploadImage(file) {
  167. const isIMAGE = file.type === "image/jpeg" || "image/jpg" || "image/png";
  168. const isLt1M = file.size / 1024 / 1024 < 1;
  169. if (!isIMAGE) {
  170. this.$message.error("上传文件只能是图片格式!");
  171. }
  172. if (!isLt1M) {
  173. this.$message.error("上传文件大小不能超过 1MB!");
  174. }
  175. return isIMAGE && isLt1M;
  176. },
  177. UploadImage(param) {
  178. const formData = new FormData();
  179. formData.append("file", param.file);
  180. formData.append("userId", this.userInfo?.id);
  181. uploadFile(formData)
  182. .then((response) => {
  183. console.log(response);
  184. console.log("上传图片成功");
  185. this.fileList.push(response.data);
  186. param.onSuccess(); // 上传成功的图片会显示绿色的对勾
  187. // 但是我们上传成功了图片, fileList 里面的值却没有改变,还好有on-change指令可以使用
  188. })
  189. .catch((response) => {
  190. console.log("图片上传失败");
  191. param.onError();
  192. });
  193. },
  194. fileChange(file) {
  195. // console.log(194, file);
  196. // this.$refs.upload.clearFiles(); // 清除文件对象
  197. // this.logo = file.raw; // 取出上传文件的对象,在其它地方也可以使用
  198. // this.fileList = [{ name: file.name, url: file.url }]; // 重新手动赋值filstList, 免得自定义上传成功了, 而fileList并没有动态改变, 这样每次都是上传一个对象
  199. },
  200. removeFile(file, fileList) {
  201. this.fileList = fileList;
  202. },
  203. },
  204. };
  205. </script>
  206. <style lang="less" scoped>
  207. .blog-page {
  208. //margin-left: 0;
  209. //width: 100vw;
  210. background-color: #f8f9fb;
  211. padding: 24px;
  212. //background-color: #fff;
  213. //margin-top: 350px;
  214. .title {
  215. text-align: left;
  216. }
  217. .edit-box {
  218. .nav {
  219. display: flex;
  220. margin-bottom: 24px;
  221. .nav-item {
  222. display: flex;
  223. font-size: 14px;
  224. margin-right: 10px;
  225. .label {
  226. margin-right: 5px;
  227. }
  228. .value {
  229. width: 300px;
  230. .dom {
  231. width: 100%;
  232. }
  233. }
  234. }
  235. }
  236. .edit {
  237. .btn {
  238. text-align: right;
  239. margin-top: 5px;
  240. }
  241. }
  242. }
  243. }
  244. </style>