edit.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <div class="divBox">
  3. <pages-header
  4. ref="pageHeader"
  5. :title="$route.params.id ? '编辑文章' : '添加文章'"
  6. backUrl="/marketing/content/articleManager"
  7. ></pages-header>
  8. <el-card class="box-card mt14" shadow="never" :bordered="false" :body-style="{ padding: '40px 50px' }">
  9. <div class="components-container">
  10. <el-form ref="pram" label-width="81px" :model="pram" size="small">
  11. <el-form-item
  12. label="标题:"
  13. prop="title"
  14. :rules="[{ required: true, message: '请填写标题', trigger: ['blur', 'change'] }]"
  15. >
  16. <el-input v-model.trim="pram.title" class="from-ipt-width" placeholder="标题" maxlength="100" />
  17. </el-form-item>
  18. <el-form-item
  19. label="作者:"
  20. prop="author"
  21. :rules="[{ required: true, message: '请填作者', trigger: ['blur', 'change'] }]"
  22. >
  23. <el-input v-model.trim="pram.author" class="from-ipt-width" placeholder="作者" maxlength="20" />
  24. </el-form-item>
  25. <el-form-item
  26. label="文章分类:"
  27. :rules="[{ required: true, message: '请选择分类', trigger: ['blur', 'change'] }]"
  28. >
  29. <el-select v-model.trim="pram.cid" placeholder="请选择" class="from-ipt-width">
  30. <el-option v-for="item in categoryTreeData" :key="item.id" :label="item.name" :value="item.id">
  31. </el-option>
  32. </el-select>
  33. </el-form-item>
  34. <el-form-item
  35. label="图文封面:"
  36. prop="cover"
  37. :rules="[{ required: true, message: '请上传图文封面', trigger: 'change' }]"
  38. >
  39. <div class="upLoadPicBox" @click="modalPicTap(false)">
  40. <div v-if="pram.cover" class="pictrue"><img :src="pram.cover" /></div>
  41. <div v-else class="upLoad">
  42. <i class="el-icon-camera cameraIconfont" />
  43. </div>
  44. </div>
  45. </el-form-item>
  46. <el-form-item
  47. label="文章简介:"
  48. prop="synopsis"
  49. :rules="[{ required: true, message: '请填写文章简介', trigger: ['blur', 'change'] }]"
  50. >
  51. <el-input
  52. v-model.trim="pram.synopsis"
  53. maxlength="100"
  54. type="textarea"
  55. :rows="2"
  56. resize="none"
  57. class="from-ipt-width"
  58. placeholder="文章简介"
  59. />
  60. </el-form-item>
  61. <el-form-item
  62. label="文章内容:"
  63. prop="content"
  64. :rules="[{ required: true, message: '请填写文章内容', trigger: ['blur', 'change'] }]"
  65. >
  66. <Tinymce v-model.trim="pram.content"></Tinymce>
  67. </el-form-item>
  68. <el-form-item label="排序:">
  69. <el-input-number v-model.trim="pram.sort" :min="0" :max="10" label="排序"></el-input-number>
  70. </el-form-item>
  71. <el-form-item label="是否Banner:">
  72. <el-switch v-model.trim="pram.isBanner" active-text="是" inactive-text="否" />
  73. </el-form-item>
  74. <el-form-item label="是否热门:">
  75. <el-switch v-model.trim="pram.isHot" active-text="是" inactive-text="否" />
  76. </el-form-item>
  77. <el-form-item>
  78. <el-button
  79. type="primary"
  80. :loading="loading"
  81. @click="handerSubmit('pram')"
  82. v-hasPermi="['merchant:article:update', 'merchant:article:save']"
  83. >保存</el-button
  84. >
  85. </el-form-item>
  86. </el-form>
  87. </div>
  88. </el-card>
  89. </div>
  90. </template>
  91. <script>
  92. // +---------------------------------------------------------------------
  93. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  94. // +---------------------------------------------------------------------
  95. // | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
  96. // +---------------------------------------------------------------------
  97. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  98. // +---------------------------------------------------------------------
  99. // | Author: CRMEB Team <admin@crmeb.com>
  100. // +---------------------------------------------------------------------
  101. import Tinymce from '@/components/Tinymce/index';
  102. import * as articleApi from '@/api/article.js';
  103. import { getToken } from '@/utils/auth';
  104. import { Debounce } from '@/utils/validate';
  105. export default {
  106. components: { Tinymce },
  107. data() {
  108. return {
  109. loading: false,
  110. constants: this.$constants,
  111. categoryTreeData: [],
  112. categoryProps: {
  113. value: 'id',
  114. label: 'name',
  115. children: 'child',
  116. expandTrigger: 'hover',
  117. checkStrictly: true,
  118. emitPath: false,
  119. },
  120. pram: {
  121. author: null,
  122. cid: null,
  123. content: '', //<span>My Document\'s Title</span>
  124. cover: '',
  125. isBanner: false,
  126. isHot: null,
  127. shareSynopsis: null,
  128. shareTitle: null,
  129. sort: 0,
  130. synopsis: null,
  131. title: null,
  132. id: null,
  133. // mediaId: null
  134. },
  135. editData: {},
  136. myHeaders: { 'X-Token': getToken() },
  137. editorContentLaebl: '',
  138. };
  139. },
  140. created() {
  141. this.tempRoute = Object.assign({}, this.$route);
  142. },
  143. mounted() {
  144. if (localStorage.getItem('articleClass')) {
  145. this.categoryTreeData = JSON.parse(localStorage.getItem('articleClass'));
  146. } else {
  147. this.handlerGetCategoryTreeData();
  148. }
  149. if (this.$route.params.id) {
  150. this.getInfo();
  151. this.setTagsViewTitle();
  152. }
  153. },
  154. methods: {
  155. getInfo() {
  156. articleApi.InfoArticle(this.$route.params.id).then((data) => {
  157. this.editData = data;
  158. this.hadlerInitEditData();
  159. });
  160. },
  161. modalPicTap(multiple) {
  162. const _this = this;
  163. this.$modalUpload(
  164. function (img) {
  165. if (!img) return;
  166. _this.pram.cover = img[0].sattDir;
  167. },
  168. multiple,
  169. 'content',
  170. );
  171. },
  172. hadlerInitEditData() {
  173. if (!this.$route.params.id) return;
  174. const { author, cid, content, cover, isBanner, isHot, shareSynopsis, shareTitle, sort, synopsis, title, id } =
  175. this.editData;
  176. this.pram.author = author;
  177. this.pram.cid = Number.parseInt(cid);
  178. this.pram.content = content;
  179. this.pram.cover = cover;
  180. this.pram.isBanner = isBanner;
  181. this.pram.isHot = isHot;
  182. this.pram.shareSynopsis = shareSynopsis;
  183. this.pram.shareTitle = shareTitle;
  184. this.pram.sort = sort;
  185. this.pram.synopsis = synopsis;
  186. this.pram.title = title;
  187. this.pram.id = id;
  188. },
  189. handlerGetCategoryTreeData() {
  190. articleApi.articleCategoryListApi().then((data) => {
  191. this.categoryTreeData = data;
  192. let list = data.filter((item) => {
  193. return item.status;
  194. });
  195. localStorage.setItem('articleClass', JSON.stringify(list));
  196. });
  197. },
  198. handerSubmit: Debounce(function (form) {
  199. this.$refs[form].validate((valid) => {
  200. if (!valid) return;
  201. if (!this.$route.params.id) {
  202. this.handlerSave();
  203. } else {
  204. this.handlerUpdate();
  205. }
  206. });
  207. }),
  208. handlerUpdate() {
  209. this.loading = true;
  210. this.pram.shareTitle = this.pram.title;
  211. this.pram.shareSynopsis = this.pram.synopsis;
  212. articleApi
  213. .UpdateArticle(this.pram)
  214. .then((data) => {
  215. this.$message.success('编辑文章成功');
  216. this.loading = false;
  217. this.$router.push({ path: '/marketing/content/articleManager' });
  218. })
  219. .catch(() => {
  220. this.loading = false;
  221. });
  222. },
  223. handlerSave() {
  224. this.loading = true;
  225. //this.pram.cid = Array.isArray(this.pram.cid) ? this.pram.cid[0] : this.pram.cid
  226. this.pram.shareTitle = this.pram.title;
  227. this.pram.shareSynopsis = this.pram.synopsis;
  228. articleApi
  229. .AddArticle(this.pram)
  230. .then((data) => {
  231. this.$message.success('新增文章成功');
  232. this.loading = false;
  233. this.$router.push({ path: '/marketing/content/articleManager' });
  234. })
  235. .catch(() => {
  236. this.loading = false;
  237. });
  238. },
  239. setTagsViewTitle() {
  240. const title = '编辑文章';
  241. const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.$route.params.id}` });
  242. this.$store.dispatch('tagsView/updateVisitedView', route);
  243. },
  244. },
  245. };
  246. </script>
  247. <style scoped></style>