You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

add.vue 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <a-modal v-model:visible="openAddModel" :title="title" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
  3. @cancel="cancelModal" width="50%">
  4. <a-form :model="createForm" layout="vertical">
  5. <a-row gutter="20">
  6. <a-col span="24">
  7. <a-form-item required label="文章标题" name="title">
  8. <a-input v-model:value="createForm.title" placeholder="请输入文章标题" />
  9. </a-form-item>
  10. </a-col>
  11. <a-col span="24">
  12. <a-form-item required label="封面图">
  13. <upload-one upload_txt="上传封面图" @uploadSuccess="uploadSuccess" :success_image="success_img"
  14. images_length="1"></upload-one>
  15. </a-form-item>
  16. </a-col>
  17. <a-col span="24">
  18. <a-form-item label="上传文件">
  19. <a-switch v-model:checked="showUplaod"> </a-switch>
  20. <div v-if="showUplaod" style="margin-top: 10px;">
  21. <upload-file @uploadSuccess="uploadDocSuccess"></upload-file>
  22. <a-typography-paragraph v-if="createForm.doc_url" :copyable="{ text: imageprefix+createForm.doc_url}">
  23. {{imageprefix+createForm.doc_url}}
  24. </a-typography-paragraph>
  25. </div>
  26. </a-form-item>
  27. </a-col>
  28. <a-col span="24">
  29. <a-form-item required label="上级栏目" name="parent_id">
  30. <search-select placeholder="请选择上级栏目" :list="section_list" :select_value="addOtherForm.name"
  31. @searchData="sectionSearch" @getSelectValue="getSectionValue"
  32. :select_disabled="false"></search-select>
  33. </a-form-item>
  34. </a-col>
  35. <a-col span="24">
  36. <a-form-item required label="文章内容" name="content">
  37. <QuillEditor theme="snow" :options="options" toolbar="full"
  38. v-model:content="addOtherForm.content" @update:content="onEditorUpdate($event)"
  39. contentType="html" />
  40. </a-form-item>
  41. </a-col>
  42. <a-col span="24">
  43. <a-form-item required label="是否置顶" name="stick_top">
  44. <a-radio-group v-model:value="createForm.stick_top" button-style="solid">
  45. <a-radio-button :value="1">否</a-radio-button>
  46. <a-radio-button :value="2">是</a-radio-button>
  47. </a-radio-group>
  48. </a-form-item>
  49. </a-col>
  50. <a-col span="24">
  51. <a-form-item label="成为热门" name="hot">
  52. <a-radio-group v-model:value="createForm.hot" button-style="solid">
  53. <a-radio-button :value="1">普通</a-radio-button>
  54. <a-radio-button :value="2">热门推荐</a-radio-button>
  55. </a-radio-group>
  56. </a-form-item>
  57. </a-col>
  58. <a-col span="24">
  59. <a-form-item label="作者" name="author">
  60. <a-input v-model:value="createForm.author" placeholder="请输入作者" />
  61. </a-form-item>
  62. </a-col>
  63. </a-row>
  64. </a-form>
  65. </a-modal>
  66. </template>
  67. <script setup lang="ts">
  68. import { ref, onMounted, computed, defineProps, watch, defineEmits } from 'vue';
  69. import { addArticle, updateArticle, listSection } from '@/apis/models';
  70. import he from 'he';
  71. import { dataForm, otherDataForm, reset } from '@/views/information/section/add/data.ts';
  72. import { useCommon } from '@/hooks/useCommon';
  73. let { store, openAddModel, hideModal, message, richOption, imageprefix } = useCommon();
  74. const emit = defineEmits();
  75. let props = defineProps(['edit_record']);
  76. let title = ref<String>('新增文章');
  77. let selectDisabled = ref<Boolean>(false)
  78. let success_img = ref<String>('')
  79. const options = ref(richOption)
  80. let showUplaod = ref<Boolean>(false)
  81. let createForm = ref<InformationArticleType.addFormType>(dataForm)
  82. let addOtherForm = ref<InformationArticleType.addOtherFormType>(otherDataForm)
  83. onMounted(() => {
  84. sectionSearch({ page: 1, pagesize: 100 })
  85. })
  86. // 上传图片
  87. const uploadSuccess = (data : Object) => {
  88. success_img.value = imageprefix + data
  89. createForm.value.cover_img = data
  90. }
  91. // 上传文件
  92. const uploadDocSuccess = (data : Object) => {
  93. createForm.value.doc_url = data
  94. }
  95. // 选择栏目/频道
  96. let section_list = ref<Object[]>([])
  97. const sectionSearch = (val : Object) => {
  98. listSection(val).then((res : object) => {
  99. const data = res.data.sections.map((item : object) => ({
  100. label: item.name,
  101. value: item.id,
  102. }));
  103. section_list.value = data;
  104. })
  105. }
  106. const getSectionValue = (val : Object) => {
  107. createForm.value.parent_id = val.key;
  108. }
  109. // 富文本
  110. const onEditorUpdate = (data) => {
  111. createForm.value.content = data
  112. }
  113. const sumbitForm = () => {
  114. if (createForm.value.title && createForm.value.cover_img && createForm.value.section_id && createForm.value.content) {
  115. if (!createForm.value.id) {
  116. addArticle(createForm.value).then(res => {
  117. message.success('新增文章成功');
  118. hideModal();
  119. resetForm();
  120. emit('successAdd');
  121. }).catch(err => {
  122. })
  123. } else {
  124. updateArticle(createForm.value).then(res => {
  125. message.success('修改文章成功');
  126. hideModal();
  127. resetForm();
  128. emit('successAdd');
  129. }).catch(err => {
  130. })
  131. }
  132. } else {
  133. message.warning('请补充完整信息');
  134. }
  135. }
  136. const resetForm = () => {
  137. addOtherForm.value = reset().otherDataForm;
  138. createForm.value = reset().dataForm;
  139. }
  140. const cancelModal = () => {
  141. emit('closeAdd');
  142. resetForm();
  143. hideModal();
  144. }
  145. openAddModel = computed(() => {
  146. return store.state.openAddModel;
  147. })
  148. watch(() => props.edit_record, (newVal) => {
  149. if (newVal) {
  150. title.value = '编辑文章';
  151. addOtherForm.value = {
  152. name: newVal.section_id,
  153. content: he.decode(newVal.content)
  154. }
  155. success_img.value = imageprefix + newVal.cover_img;
  156. createForm.value = {
  157. id: newVal.id,
  158. title: newVal.title,
  159. cover_img: newVal.cover_img,
  160. section_id: newVal.section_id,
  161. content: newVal.content,
  162. author: newVal.author,
  163. stick_top:newVal.stick_top,
  164. hot: newVal.hot,
  165. }
  166. } else {
  167. title.value = '新增文章';
  168. }
  169. })
  170. </script>
  171. <style>
  172. </style>