add.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <script>
  2. // import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  3. // Vue.use( CKEditor );
  4. import RoundedTitle from "../../../components/public/roundedTitle.vue";
  5. import PopCard from "../../../components/public/popCard.vue";
  6. import ImageTable from "../../../components/public/imageTable.vue";
  7. import Pop from "../../../components/public/pop.vue";
  8. import TableSelect from "../../../components/public/tableSelect.vue";
  9. import dbField_esm from "../../../map/dbField_esm";
  10. import {checkFormItem, initForm} from "../../../until/formTool";
  11. import InputRow from "../../../components/public/form/inputRow.vue";
  12. import ImageViewer from "../../../components/public/imageViewer.vue";
  13. import {handle} from "../../../until/handle";
  14. import {apiMap} from "../../../map/apiMap";
  15. import {rCode} from "../../../map/rcodeMap_esm";
  16. let ClassicEditor;
  17. // let MyCustomPlugin;
  18. if (process.client) {
  19. ClassicEditor = require('../../../model/ckeditor/ckeditor');
  20. // ClassicEditor = require('@ckeditor/ckeditor5-build-classic');
  21. console.log(ClassicEditor);
  22. // MyCustomPlugin = require('../../../until/customCkeditorPlugin');
  23. }
  24. export default {
  25. name: "add",
  26. components: {
  27. ImageViewer,
  28. InputRow,
  29. TableSelect,
  30. Pop,
  31. ImageTable,
  32. PopCard,
  33. RoundedTitle
  34. // Use the <ckeditor> component in this view.
  35. },
  36. computed: {
  37. // 产品类型数据
  38. newsTypes(){
  39. let arr = [];
  40. if (this.form.pType.val === dbField_esm.db_base.newsType.all){
  41. arr = this.$store.getters.allNewsTypes;
  42. }else if (this.form.pType.val === dbField_esm.db_base.newsType.news){
  43. arr = this.$store.getters.newsTypes;
  44. }else if (this.form.pType.val === dbField_esm.db_base.newsType.solution){
  45. arr = this.$store.getters.solutionTypes;
  46. }
  47. if (arr[0].key !== 'all'){
  48. // 添加 all
  49. arr.unshift({text: '全部', key: 'all' });
  50. }
  51. return arr;
  52. },
  53. },
  54. data(){
  55. return {
  56. editor: ClassicEditor,
  57. editorData: '<p>在此输入文章内容</p>',
  58. editorConfig: {
  59. language: 'zh',
  60. customText: "插入图片",
  61. },
  62. imagePopShow: false,
  63. imagePopLoading: false,
  64. isCoverSelect: false,
  65. form: {
  66. pType: {
  67. val: dbField_esm.db_base.newsType.all,
  68. oldVal: dbField_esm.db_base.newsType.all,
  69. init: dbField_esm.db_base.newsType.all,
  70. msg: '',
  71. state: 0,
  72. options: [
  73. {text: '全部', key: dbField_esm.db_base.newsType.all},
  74. {text: '新闻', key: dbField_esm.db_base.newsType.news},
  75. {text: '解决方案', key: dbField_esm.db_base.newsType.solution},
  76. ]
  77. },
  78. type: {
  79. val: '',
  80. init: '',
  81. msg: '',
  82. state: 0,
  83. options: []
  84. },
  85. title: {
  86. val: '',
  87. init: '',
  88. msg: '',
  89. state: 0
  90. },
  91. author: {
  92. val: '',
  93. init: '',
  94. msg: '',
  95. state: 0
  96. },
  97. source: {
  98. val: '',
  99. init: '',
  100. msg: '',
  101. state: 0
  102. },
  103. cover: {
  104. val: '',
  105. init: '',
  106. msg: '',
  107. state: 0
  108. },
  109. remark: {
  110. val: '',
  111. init: '',
  112. msg: '',
  113. state: 0
  114. },
  115. }
  116. }
  117. },
  118. beforeMount() {
  119. // 插入toolbar
  120. this.editorConfig.customFunction = () => {
  121. this.openImageSelect();
  122. };
  123. this.initForm();
  124. },
  125. mounted() {
  126. },
  127. methods:{
  128. initForm(){
  129. this.form.pType.val = dbField_esm.db_base.newsType.all;
  130. this.form.type.options = this.newsTypes;
  131. initForm(this.form);
  132. },
  133. openCoverSelect(){
  134. console.log('打开文章封面选择窗口');
  135. this.isCoverSelect = true;
  136. this.imagePopShow = true;
  137. },
  138. openImageSelect(){
  139. console.log('尝试打开图片选择窗口');
  140. this.isCoverSelect = false;
  141. this.imagePopShow = true;
  142. },
  143. selectImageHandle(fileData){
  144. console.log('okHandle');
  145. console.log(fileData);
  146. this.imagePopShow = false;
  147. if(this.isCoverSelect){
  148. // 选择封面图片
  149. this.form.cover.val = fileData.filePath;
  150. this.form.cover.init = fileData.filePath;
  151. this.form.cover.msg = '';
  152. this.form.cover.state = 0;
  153. this.form.cover.showText = fileData.filePath;
  154. return;
  155. }
  156. // 将图片新增至编辑器中,允许调整图片大小
  157. this.editorData += `<img src="${fileData.filePath}" alt="${fileData.fileName}">`;
  158. console.log(this.editorData);
  159. },
  160. onPTypeChange(){
  161. console.log('文章主类型改变');
  162. console.log(this.newsTypes);
  163. this.form.type.val = this.newsTypes[0].key;
  164. this.form.type.oldVal = this.newsTypes[0].key;
  165. this.form.type.init = this.newsTypes[0].key;
  166. this.form.type.options = this.newsTypes;
  167. },
  168. async onSubmitHandle(){
  169. // 提交文章
  170. console.log('提交文章');
  171. let formData = {};
  172. let isPass = checkFormItem(this.form);
  173. if(!isPass){
  174. return console.log('数据验证不通过');
  175. }
  176. formData.content = this.editorData;
  177. formData.type = this.form.type.val;
  178. formData.title = this.form.title.val;
  179. formData.author = this.form.author.val;
  180. formData.source = this.form.source.val;
  181. formData.cover = this.form.cover.val;
  182. formData.remark = this.form.remark.val;
  183. formData.pType = this.form.pType.val;
  184. console.log(formData);
  185. // 发送请求至后台
  186. let [err,res] = await handle(this.$axios.post(
  187. apiMap.newsAdd.path,
  188. formData
  189. ));
  190. if(err){
  191. console.log(err);
  192. return this.$message.error('新增文章失败');
  193. }
  194. let result = res.data;
  195. if (result.code === rCode.OK){
  196. this.$message.success('新增文章成功');
  197. }else{
  198. this.$message.error(`新增文章失败,${result.msg}`);
  199. }
  200. }
  201. }
  202. }
  203. </script>
  204. <template>
  205. <div class="w-full p-2">
  206. <rounded-title class="text-xl">
  207. <span>新增文章</span>
  208. <a href="/manger/news"
  209. class="px-10 h-full ml-5 rounded bg-blue-400 text-white cursor-pointer
  210. hover:text-orange-500 ">文章中心</a>
  211. </rounded-title>
  212. <div class="page-content-box w-full mt-2 p-0.5 rounded bg-white">
  213. <div class="flex pb-1 border-b border-gray-300 border-dashed">
  214. <a-radio-group class="flex flex-shrink-0 items-center" v-model="form.pType.val" @change="onPTypeChange">
  215. <a-radio-button class="" :value="item.key" v-for="item in form.pType.options" :key="item.key">{{item.text}}</a-radio-button>
  216. </a-radio-group>
  217. <table-select
  218. class="w-48 !mx-3 flex-shrink-0"
  219. :options="form.type.options"
  220. v-model="form.type.val"
  221. />
  222. </div>
  223. <input-row class="mt-2" :msg="form.author.msg"
  224. label="作者">
  225. <a-input v-model="form.author.val" placeholder="请输入作者"></a-input>
  226. </input-row>
  227. <input-row class="mt-2" :msg="form.source.msg"
  228. label="来源">
  229. <a-input v-model="form.source.val" placeholder="请输入来源"></a-input>
  230. </input-row>
  231. <input-row class="mt-2" :msg="form.title.msg"
  232. label="标题">
  233. <a-input v-model="form.title.val" placeholder="请输入标题"></a-input>
  234. </input-row>
  235. <input-row class="mt-2" :msg="form.remark.msg"
  236. remark="文章的简介摘要部分,用于外部展示"
  237. label="摘要" >
  238. <a-input v-model="form.remark.val" placeholder="请输入摘要"></a-input>
  239. </input-row>
  240. <input-row class="my-2" :msg="form.cover.msg"
  241. label="文章封面">
  242. <div class="rounded relative" style="width: 524px;height: 360px" >
  243. <image-viewer class="" :src="form.cover.showText"></image-viewer>
  244. <div class="absolute w-full h-full left-0 top-0
  245. justify-center text-white bg-gray-400
  246. items-center text-2xl flex opacity-0 hover:opacity-70"
  247. @click="openCoverSelect"
  248. >
  249. 点击选择封面图片
  250. </div>
  251. </div>
  252. </input-row>
  253. <ckeditor class="mt-2" ref="ckeditor" :editor="editor" v-model="editorData" :config="editorConfig"></ckeditor>
  254. <div class="mt-2">
  255. <a-button type="primary" @click="onSubmitHandle">新增文章</a-button>
  256. </div>
  257. </div>
  258. <!-- 图片选择弹窗 -->
  259. <pop :show="imagePopShow" :loading="imagePopLoading">
  260. <pop-card>
  261. <template slot="header">
  262. <span>选择需要插入的图片</span>
  263. </template>
  264. <image-table
  265. class="w-full h-full"
  266. @cancel="imagePopShow = false"
  267. @ok="selectImageHandle">
  268. </image-table>
  269. </pop-card>
  270. </pop>
  271. </div>
  272. </template>
  273. <style scoped>
  274. .page-content-box{
  275. min-height: 500px;
  276. }
  277. </style>