editor.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <div class="editor">
  3. <el-input show-word-limit v-model="title" class="title" placeholder="请输入文章标题" :maxlength="64"></el-input>
  4. <editor placeholder="支持富文本、支持超链接、插入图片、插入视频,最少100字符,最多10000字符" :content="content" @change="handleChange"></editor>
  5. <h5 class="label">封面图(选填)</h5>
  6. <div class="uploadInfo">
  7. <div class="left">
  8. <el-upload
  9. class="avatar-uploader"
  10. action="#"
  11. :show-file-list="false"
  12. :multiple="false"
  13. accept="image/png, image/jpeg"
  14. :before-upload="handleFileChange"
  15. >
  16. <i v-if="cover_url" class="el-icon-delete avatar-uploader-icon"
  17. @click.stop="handleDeleteFile"></i>
  18. <img v-if="cover_url" :src="cover_url" class="avatar"/>
  19. <div v-else class="noneImage">
  20. <i class="el-icon-plus avatar-uploader-icon"></i>
  21. </div>
  22. </el-upload>
  23. </div>
  24. <div class="right">
  25. <p>建议尺寸: </p>
  26. <p>180*140 大小在500k以内</p>
  27. </div>
  28. </div>
  29. <h5 class="label">添加到合集(选填)</h5>
  30. <el-select v-model="collectionId" placeholder="请选择合集" class="class">
  31. <el-option
  32. v-for="item in collectionList"
  33. :key="item.id"
  34. :label="item.title"
  35. :value="item.id"
  36. ></el-option>
  37. </el-select>
  38. <el-dialog :visible.sync="dialogVisible">
  39. <img width="100%" :src="cover_url" alt />
  40. </el-dialog>
  41. <footer>
  42. <el-button type="primary" @click="publish">发布</el-button>
  43. <el-button @click="cancel">取消</el-button>
  44. </footer>
  45. </div>
  46. </template>
  47. <script>
  48. import editor from "@/components/editor";
  49. export default {
  50. head() {
  51. return {
  52. script: []
  53. };
  54. },
  55. components: {
  56. editor
  57. },
  58. data() {
  59. return {
  60. topicId: "", // 编辑
  61. hashID: null,
  62. title: "",
  63. subTitle: "",
  64. content: "",
  65. cover_url: "",
  66. dialogVisible: false,
  67. disabled: false,
  68. collectionId: "",
  69. collectionList: [],
  70. categoryList: [
  71. {
  72. value: "default",
  73. label: "推荐"
  74. }
  75. ],
  76. fileList: [],
  77. uploading: false
  78. };
  79. },
  80. computed: {},
  81. mounted() {
  82. this.needLogin();
  83. this.needVerify();
  84. this.getHejiList()
  85. if (this.$route.params.id) {
  86. this.loadData();
  87. }
  88. },
  89. methods: {
  90. loadData() {
  91. const data = {
  92. topicId: this.$route.params.id
  93. };
  94. this.$axios
  95. .$post(`/api/community/topic/get_edit_topic`, data)
  96. .then(res => {
  97. console.log(res);
  98. const topic = res.data;
  99. if (topic) {
  100. this.cover_url = topic.cover_url;
  101. this.topicId = topic.id;
  102. this.title = topic.title;
  103. this.subTitle = topic.intro;
  104. this.content = topic.body;
  105. this.hashID = topic.hash_id;
  106. if (this.cover_url) {
  107. this.fileList.push({
  108. name: "cover_image",
  109. url: this.cover_url
  110. });
  111. }
  112. } else {
  113. this.goHome();
  114. }
  115. // TODO go details
  116. });
  117. },
  118. getHejiList() {
  119. this.$axios.post("/api/jishuquan/get_collections", {
  120. uid: this.$store.state.userinfo.uid,
  121. page: 1,
  122. size: 200
  123. }).then(res=>{
  124. if (res.data.status === 1) {
  125. this.collectionList = res.data.data
  126. }
  127. })
  128. },
  129. publish() {
  130. this.needVerify();
  131. if (!this.title) {
  132. this.$message.error("请输入文章标题");
  133. return;
  134. }
  135. if (this.title.length < 2) {
  136. this.$message.error("文章标题不可少于2字符");
  137. return;
  138. }
  139. if (this.title.length > 64) {
  140. this.$message.error("文章标题不可超过64字符,请删减");
  141. return;
  142. }
  143. if (!this.content) {
  144. this.$message.error("请输入文章正文");
  145. return;
  146. }
  147. if (this.content.length < 100) {
  148. this.$message.error("文章正文不可少于100字符");
  149. return;
  150. }
  151. if (this.content.length > 100000) {
  152. this.$message.error("文章正文不可超过1万字符,请删减");
  153. return;
  154. }
  155. const data = {
  156. title: this.title,
  157. intro: this.subTitle,
  158. body: this.content,
  159. provider_id: this.collectionId,
  160. cover_url: this.cover_url
  161. };
  162. if (this.topicId) {
  163. data.topic_id = this.topicId;
  164. this.$axios
  165. .$post(`/api/community/topic/update_topic`, data)
  166. .then(res => {
  167. console.log(res);
  168. if (res.status === -99) {
  169. this.goHome();
  170. }
  171. if (res.status === 1) {
  172. this.$message.success("编辑成功!");
  173. }
  174. // TODO go details
  175. window.location.href = `/p/${this.hashID}.html`;
  176. });
  177. } else {
  178. this.$axios
  179. .$post(`/api/community/topic/create_topic`, data)
  180. .then(res => {
  181. console.log(res);
  182. if (res.status === -99) {
  183. this.goHome();
  184. }
  185. if (res.status === 1) {
  186. this.$message.success("发布成功!");
  187. }
  188. // TODO go details
  189. window.location.href = `/p/${res.data.hash_id}.html`;
  190. });
  191. }
  192. },
  193. cancel() {
  194. this.$router.back();
  195. },
  196. handleChange(val) {
  197. this.content = val;
  198. },
  199. handleDeleteFile() {
  200. this.cover_url = "";
  201. },
  202. handleFileChange(file) {
  203. console.log(file);
  204. if (file.size / 1024 > 500) {
  205. this.$message.error("图片大小不得超过500k,请重新选择");
  206. return false;
  207. }
  208. const formData = new FormData();
  209. formData.append("target", '{ "type": 3 }');
  210. formData.append("id", "WU_FILE_0");
  211. formData.append("name", file.name);
  212. formData.append("type", file.type);
  213. formData.append("lastModifiedDate", file.lastModifiedDate);
  214. formData.append("size", file.size);
  215. formData.append("file", file);
  216. this.$axios
  217. .$post(`/file/proxyUpload`, formData, {
  218. headers: { "Content-Type": "multipart/form-data" }
  219. })
  220. .then(res => {
  221. this.cover_url = (res.data && res.data.url) || "";
  222. this.fileList = [
  223. {
  224. name: "file.name",
  225. url: this.cover_url
  226. }
  227. ];
  228. });
  229. }
  230. }
  231. };
  232. </script>
  233. <style lang="scss">
  234. .editor {
  235. position: relative;
  236. padding: 20px;
  237. width: 740px;
  238. background: #fff;
  239. .title,
  240. .sub-title {
  241. .el-input__inner {
  242. padding: 0;
  243. border: 0;
  244. border-radius: 0;
  245. }
  246. }
  247. .title {
  248. margin: 10px auto 20px;
  249. font-size: 28px;
  250. font-family: PingFangSC-Medium;
  251. font-weight: 500;
  252. color: rgba(29, 42, 58, 1);
  253. line-height: 40px;
  254. }
  255. .sub-title {
  256. margin-bottom: 28px;
  257. min-height: 18px;
  258. font-size: 14px;
  259. font-family: PingFangSC-Regular;
  260. font-weight: 400;
  261. color: rgba(145, 154, 167, 1);
  262. line-height: 18px;
  263. border: 0;
  264. .el-textarea__inner {
  265. height: 18px;
  266. line-height: 18px;
  267. border: 0;
  268. padding-left: 0;
  269. }
  270. }
  271. .label {
  272. margin: 20px auto 10px;
  273. font-size: 13px;
  274. font-family: PingFangSC-Medium;
  275. font-weight: 500;
  276. color: rgba(25, 34, 46, 1);
  277. line-height: 18px;
  278. }
  279. .ql-toolbar {
  280. border-width: 1px 0 0 0 !important;
  281. }
  282. .quill-editor {
  283. height: 450px;
  284. border-left: 0 !important;
  285. border-right: 0 !important;
  286. font-size: 14px;
  287. line-height: 25px;
  288. }
  289. .ql-snow.ql-toolbar::after {
  290. display: inline-block;
  291. }
  292. .class {
  293. width: 320px;
  294. }
  295. .tags {
  296. width: 560px;
  297. }
  298. footer {
  299. margin: 40px 0;
  300. button {
  301. width: 100px;
  302. height: 40px;
  303. border-radius: 0;
  304. }
  305. }
  306. strong {
  307. font-weight: 800 !important;
  308. }
  309. em {
  310. font-style: italic !important;
  311. }
  312. }
  313. .uploadInfo {
  314. display: flex;
  315. align-items: center;
  316. .left {
  317. width: 180px;
  318. flex-shrink: 0;
  319. overflow: hidden;
  320. position: relative;
  321. background: #fff;
  322. .el-icon-delete {
  323. display: none;
  324. }
  325. .avatar-uploader .el-upload {
  326. width: 180px;
  327. height: 140px;
  328. border: 1px dashed #dce1e8;
  329. border-radius: 6px;
  330. cursor: pointer;
  331. position: relative;
  332. overflow: hidden;
  333. img {
  334. width: 100%;
  335. height: auto;
  336. object-fit: contain;
  337. object-position: top left;
  338. }
  339. }
  340. .avatar-uploader .el-upload:hover {
  341. border-color: #409eff;
  342. .el-icon-delete {
  343. display: block;
  344. }
  345. }
  346. .avatar-uploader-icon {
  347. position: absolute;
  348. top: 0;
  349. left: 0;
  350. font-size: 44px;
  351. color: #dce1e8;;
  352. width: 180px;
  353. height: 140px;
  354. line-height: 140px;
  355. text-align: center;
  356. }
  357. .avatar {
  358. width: 180px;
  359. height: 140px;
  360. display: block;
  361. }
  362. .title {
  363. position: absolute;
  364. left: 50%;
  365. bottom: 40px;
  366. transform: translateX(-50%);
  367. font-size: 13px;
  368. font-weight: 500;
  369. color: #dce1e8;
  370. line-height: 18px;
  371. text-decoration: underline;
  372. }
  373. }
  374. .right {
  375. margin-left: 18px;
  376. height:34px;
  377. p {
  378. font-size:12px;
  379. font-weight:400;
  380. color:rgba(145,154,167,1);
  381. line-height:17px;
  382. }
  383. }
  384. }
  385. </style>