chengjie 4 months ago
parent
commit
90e71a586b
1 changed files with 66 additions and 4 deletions
  1. 66 4
      public/mg/yjbdc_articles.html

+ 66 - 4
public/mg/yjbdc_articles.html

@@ -452,9 +452,9 @@
452 452
                     <span class="json-string editable"
453 453
                         @dblclick="editValue(selectedArticle, 'ArticleStart', 'ArticleStart')">
454 454
                         <template v-if="editingKey === 'ArticleStart'">
455
-                            <input type="text" class="json-editor-input"
455
+                            <textarea class="json-editor-input" rows="4"
456 456
                                 v-model="selectedArticle.ArticleStart" @blur="finishEdit" @keyup.enter="finishEdit"
457
-                                v-focus>
457
+                                v-focus></textarea>
458 458
                         </template>
459 459
                         <template v-else>
460 460
                             "{{ selectedArticle.ArticleStart }}"
@@ -572,7 +572,7 @@
572 572
                     <span class="json-string editable"
573 573
                         @dblclick="editValue(selectedArticle, 'JSONString', 'JSONString')">
574 574
                         <template v-if="editingKey === 'JSONString'">
575
-                            <textarea class="json-editor-input" v-model="selectedArticle.JSONString"
575
+                            <textarea class="json-editor-input" rows="20" v-model="selectedArticle.JSONString"
576 576
                                 @blur="finishEdit" @keyup.enter="finishEdit" v-focus></textarea>
577 577
                         </template>
578 578
                         <template v-else>
@@ -654,15 +654,77 @@
654 654
                     this.selectedArticle = null;
655 655
                 },
656 656
                 editValue(obj, key, editingKey) {
657
-                    this.editingKey = editingKey;
657
+                    // 如果是编辑JSON字符串,则格式化显示
658
+                    if (key === 'JSONString' && obj[key]) {
659
+                        try {
660
+                            // 先设置编辑状态,确保文本框显示
661
+                            this.editingKey = editingKey;
662
+                            // 延迟执行格式化,确保文本框已渲染
663
+                            this.$nextTick(() => {
664
+                                try {
665
+                                    // 尝试解析JSON并格式化
666
+                                    let jsonObj;
667
+                                    try {
668
+                                        // 尝试双重解析(处理可能被双重编码的情况)
669
+                                        jsonObj = JSON.parse(JSON.parse(obj[key]));
670
+                                    } catch (err) {
671
+                                        // 如果双重解析失败,尝试单次解析
672
+                                        jsonObj = JSON.parse(obj[key]);
673
+                                    }
674
+                                    // 使用Vue的$set方法确保响应式更新
675
+                                    this.$set(obj, key, JSON.stringify(jsonObj, null, 4));
676
+                                } catch (e) {
677
+                                    // 如果解析失败,保持原样
678
+                                    console.error('JSON解析失败:', e);
679
+                                }
680
+                            });
681
+                        } catch (e) {
682
+                            // 如果解析失败,保持原样
683
+                            console.error('JSON解析失败:', e);
684
+                            this.editingKey = editingKey;
685
+                        }
686
+                    } else {
687
+                        // 非JSON字段直接设置编辑状态
688
+                        this.editingKey = editingKey;
689
+                    }
658 690
                 },
659 691
                 finishEdit() {
692
+                    // 如果是编辑JSON字符串,则在完成编辑时转回紧凑格式
693
+                    if (this.editingKey === 'JSONString' && this.selectedArticle && this.selectedArticle.JSONString) {
694
+                        try {
695
+                            // 尝试解析格式化的JSON并转回紧凑格式
696
+                            const jsonObj = JSON.parse(this.selectedArticle.JSONString);
697
+                            // 使用Vue的$set方法确保响应式更新
698
+                            // 保存时需要确保JSON字符串被正确转义
699
+                            this.$set(this.selectedArticle, 'JSONString', JSON.stringify(jsonObj));
700
+                        } catch (e) {
701
+                            // 如果解析失败,保持原样
702
+                            console.error('JSON解析失败:', e);
703
+                        }
704
+                    }
660 705
                     this.editingKey = null;
661 706
                 },
662 707
                 saveArticle() {
663 708
                     if (!this.selectedArticle) return;
664 709
                     this.isDetailLoading = true;
665 710
                     this.selectedArticle.Source = "web";
711
+                    
712
+                    // 确保JSONString是紧凑格式
713
+                    if (this.selectedArticle.JSONString) {
714
+                        try {
715
+                            const jsonObj = JSON.parse(this.selectedArticle.JSONString);
716
+                            // 使用Vue的$set方法确保响应式更新
717
+                            // 保存时需要确保JSON字符串被正确转义
718
+                            this.$set(this.selectedArticle, 'JSONString', JSON.stringify(jsonObj));
719
+                        } catch (e) {
720
+                            console.error('JSON解析失败:', e);
721
+                            // 如果JSON格式不正确,提示用户并中止保存
722
+                            this.isDetailLoading = false;
723
+                            this.showToast('JSON格式不正确,请检查后重试', 'error');
724
+                            return;
725
+                        }
726
+                    }
727
+                    
666 728
                     $.ajax({
667 729
                         url: 'api/UpdateYJBDCArticle',
668 730
                         method: 'POST',