article-edit.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. "use strict";
  2. const Editor = window.createEditorComponent();
  3. var articleEdit = new Vue({
  4. el: '#articleEdit',
  5. components: {
  6. Editor
  7. },
  8. data: {
  9. // 供应商id
  10. shopId: GLOBAL_SHOP_ID,
  11. // 图片上传
  12. action: $('#coreServer').val() + '/tools/image/upload/multi',
  13. /* 表单提交数据 */
  14. formData: {
  15. // 文章id
  16. articleId: 0,
  17. // 标题
  18. title: '',
  19. // 内容
  20. articleContent: '',
  21. // 引导图
  22. guidanceImage: '',
  23. // seo关键词
  24. keyword: '',
  25. // 标签
  26. label: '',
  27. // 发布人
  28. publisher: '',
  29. // 推荐语
  30. recommendContent: '',
  31. // 来源
  32. source: '',
  33. // 状态
  34. status: 1,
  35. // 文章分类
  36. typeId: ''
  37. },
  38. // 图片列表
  39. articleImageList: [],
  40. // 文章标签列表
  41. articleLabels: [],
  42. // 已选择标签列表
  43. chooseLabels: [],
  44. // 文章分类列表
  45. articleTypeList: [],
  46. // 待添加标签名
  47. addLabelName: '',
  48. /* 表单验证规则 */
  49. rules: {
  50. // 标题
  51. title: [
  52. {required: true, message: '文章标题不能为空', trigger: 'blur'},
  53. {maxLength: 9999, message: '标题字数超过最大限制(9999)', trigger: 'blur'},
  54. ],
  55. // 内容
  56. articleContent: [
  57. {required: true, message: '文章内容不能为空', trigger: 'change'},
  58. {maxLength: 999999, message: '标题字数超过最大限制(999999)', trigger: 'change'},
  59. ],
  60. // 引导图
  61. guidanceImage: [
  62. {required: true, message: '请上传文章引导图', trigger: 'change'}
  63. ],
  64. // seo关键词
  65. keyword: [
  66. {required: true, message: 'SEO关键词不能为空', trigger: 'blur'}
  67. ],
  68. // 标签
  69. label: [
  70. {required: true, message: '文章标签不能为空', trigger: ['change', 'blur']}
  71. ],
  72. // 发布人
  73. publisher: [
  74. {required: true, message: '发布人信息不能为空', trigger: 'blur'}
  75. ],
  76. // 推荐语
  77. recommendContent: [
  78. {required: true, message: '推荐语/描述不能为空', trigger: 'blur'}
  79. ],
  80. // 文章分类
  81. typeId: [
  82. {required: true, message: '请选择文章所属分类', trigger: 'change'}
  83. ]
  84. },
  85. },
  86. watch: {
  87. chooseLabels: function chooseLabels(newVal) {
  88. this.formData.label = newVal.join(',');
  89. },
  90. 'formData.label': function formDataLabel(newVal, oldVal) {
  91. if (!newVal.trim()) {
  92. return this.chooseLabels = [];
  93. }
  94. if (newVal === oldVal) return;
  95. this.chooseLabels = newVal.split(',');
  96. }
  97. },
  98. created: function created() {
  99. this.init();
  100. },
  101. mounted: function mounted() {
  102. $('.navLayout').find('.navList').removeClass("on").find('.con').hide().find('a').removeClass("on");
  103. $('.navLayout').find('.navList').eq(2).addClass("on").find('.con').show().find('a').eq(0).addClass("on");
  104. },
  105. beforeDestroy: function beforeDestroy() {
  106. localStorage.removeItem('articleId');
  107. },
  108. methods: {
  109. // 页面初始化
  110. init: function init() {
  111. var articleId = localStorage.getItem('articleId') || 0;
  112. this.formData.articleId = articleId;
  113. this.fetchArticleCatagory(); // 文章id就是修改文章
  114. this.fetchFormList();
  115. },
  116. // 获取标签列表和分类列表
  117. fetchFormList: function fetchFormList() {
  118. var that = this;
  119. SupplierApi.GetArticleForm({
  120. articleId: this.formData.articleId
  121. }, function (res) {
  122. if (res.code === 0) {
  123. console.log(res);
  124. that.articleLabels = res.data.articleLabels.split(','); // 初始化表单数据
  125. if (res.data.shopArticle) {
  126. that.setFormData(res.data.shopArticle);
  127. }
  128. } else {
  129. CAIMEI.Alert(res.msg, '确定', false);
  130. }
  131. });
  132. },
  133. // 设置表单初始数据
  134. setFormData: function setFormData(shopArticle) {
  135. for (var key in this.formData) {
  136. this.formData[key] = shopArticle[key];
  137. }
  138. // 初始化引导图
  139. if (this.formData.guidanceImage) {
  140. this.articleImageList = [{url: this.formData.guidanceImage}]
  141. }
  142. },
  143. // 获取文章分类
  144. fetchArticleCatagory: function fetchArticleCatagory() {
  145. var that = this;
  146. SupplierApi.ArticleCategory({}, function (res) {
  147. if (res.code === 0) {
  148. // console.log(res);
  149. that.articleTypeList = res.data;
  150. } else {
  151. CAIMEI.Alert(res.msg, '确定', false);
  152. }
  153. });
  154. },
  155. // 选择文章标签
  156. labelClick: function labelClick(index) {
  157. var newLabel = this.articleLabels[index];
  158. var popIndex = this.chooseLabels.indexOf(newLabel);
  159. if (popIndex > -1) {
  160. this.chooseLabels.splice(popIndex, 1);
  161. } else {
  162. this.chooseLabels.push(newLabel);
  163. }
  164. },
  165. // 手动添加文章标签
  166. addLabel: function addLabel() {
  167. if (!this.addLabelName.trim()) return;
  168. if (this.chooseLabels.indexOf(this.addLabelName) > -1) return this.addLabelName = '';
  169. this.chooseLabels.push(this.addLabelName);
  170. this.addLabelName = '';
  171. },
  172. // 判断标签是否被选
  173. checkLabel: function checkLabel(index) {
  174. return this.chooseLabels.indexOf(this.articleLabels[index]) > -1;
  175. },
  176. // 图片上传成功
  177. handleUploadSuccess: function handleUploadSuccess(response, file, fileList) {
  178. this.formData.guidanceImage = response.data;
  179. this.articleImageList = fileList;
  180. },
  181. // 图片移除
  182. handleImageRemove: function handleImageRemove(response, file, fileList) {
  183. this.formData.guidanceImage = "";
  184. this.articleImageList = [];
  185. },
  186. // 保存 提交表单
  187. handleSave: function handleSave() {
  188. var _this = this;
  189. this.$refs.ruleForm.validate(valide=>{
  190. if(!valide) return;
  191. _this.save();
  192. });
  193. },
  194. // 保存接口
  195. save: function save() {
  196. var _this = this;
  197. this.formData.shopId = this.shopId;
  198. SupplierApi.ArticleSubmitSave(this.formData, function (res) {
  199. if (res.code === 0) {
  200. CAIMEI.dialog('保存成功');
  201. setTimeout(function () {
  202. _this.handleBack();
  203. }, 2000);
  204. } else {
  205. CAIMEI.Alert(res.msg, '确定', false);
  206. }
  207. });
  208. },
  209. // 表单数据转为对象
  210. serializeArrayToObj: function serializeArrayToObj(serializeArray) {
  211. var obj = Object.create(null);
  212. serializeArray.forEach(function (item) {
  213. obj[item.name] = item.value;
  214. });
  215. return obj;
  216. },
  217. // 返回文章列表页面
  218. handleBack: function handleBack() {
  219. localStorage.removeItem('articleId');
  220. window.open('/supplier/article/list.html', 'supplier-article-list');
  221. window.close();
  222. }
  223. }
  224. });