Blame view

apps/components/comments/comment-placeholder.js 4.06 KB
yyq authored
1
import {get, trim} from 'lodash';
陈峰 authored
2
import {createNamespacedHelpers} from 'vuex';
陈峰 authored
3
TaoHuang authored
4
import YAS from 'utils/yas-constants';
ityuany authored
5 6

const {mapActions: articleMapActions, mapMutations: articleMapMutations, mapState: articleMapState} = createNamespacedHelpers('article');
陈峰 authored
7
陈峰 authored
8 9 10 11 12 13 14 15 16 17
export default {
  name: 'CommentPlaceholder',
  props: {
    tag: String,
    destId: Number,
    addType: Number,
    columnType: {
      type: Number,
      default: 1001
    },
陈峰 authored
18 19 20
    user: {
      type: String,
      default: ''
陈峰 authored
21
    },
TaoHuang authored
22 23
    share: Boolean,
    posId: Number,
yyq authored
24 25 26 27 28
    articleId: Number,
    rootId: Number,
    autoUpdate: {
      type: Boolean,
      default: true
ityuany authored
29
    }
陈峰 authored
30
  },
陈峰 authored
31 32 33 34 35
  data() {
    return {
      posting: false
    };
  },
ityuany authored
36 37 38 39 40 41 42 43 44 45 46
  computed: {
    ...articleMapState(['comentInputVisable'])
  },
  watch: {
    comentInputVisable: function() {
      if (this.comentInputVisable ?? false) {
        this.openComentInput();
        this.INIT_COMENT_INPUT?.();
      }
    }
  },
陈峰 authored
47
  methods: {
陈峰 authored
48
    ...articleMapActions(['fetchArticleUpdate']),
ityuany authored
49
    ...articleMapMutations(['UPDATE_ARTICLE_COMMENT_COUNT', 'INIT_COMENT_INPUT']),
陈峰 authored
50
    async openComentInput() {
陈峰 authored
51 52 53
      if (this.share) {
        return this.$links.toDownloadApp();
      }
陈峰 authored
54
      const user = await this.$sdk.getUser();
陈峰 authored
55
陈峰 authored
56
      if (user && user.uid) {
yyq authored
57
        const hint = this.user ? `回复 ${this.user}:` : '写评论...';
陈峰 authored
58 59 60 61 62 63 64

        if (this.$yoho.isYohoBuy) {
          this.$yoho.getInput({hint}, (content) => {
            this.comment(content);
          }, (e) => {
            console.error(e);
          });
陈峰 authored
65
        } else if (process.env.NODE_ENV !== 'production') {
陈峰 authored
66 67 68 69 70 71 72 73 74 75
          this.$createDialog({
            type: 'prompt',
            title: '测试-输入',
            prompt: {
              placeholder: hint
            },
            onConfirm: (e, promptValue) => {
              this.comment(promptValue);
            }
          }).show();
陈峰 authored
76 77
        } else {
          return this.$links.toDownloadApp();
陈峰 authored
78
        }
陈峰 authored
79
      } else {
陈峰 authored
80
        this.$sdk.goLogin();
陈峰 authored
81 82 83
      }
    },
    async comment(content) {
yyq authored
84 85 86 87 88 89 90 91 92 93 94
      content = trim(content);

      if (!content) {
        this.$createToast({
          txt: '请输入评论内容',
          type: 'warn',
          time: 1000
        }).show();
        return;
      }
陈峰 authored
95 96 97 98
      if (this.posting) {
        return;
      }
      this.posting = true;
陈峰 authored
99 100 101 102 103 104
      const result = await this.postComment({
        content: content,
        destId: this.addType === 0 ? this.destId : void 0,
        commentId: this.addType === 1 ? this.destId : void 0,
        addType: this.addType,
        columnType: this.columnType
陈峰 authored
105
      });
陈峰 authored
106
陈峰 authored
107
      this.posting = false;
yyq authored
108 109 110 111 112 113 114

      let toastInfo = {
        txt: '评论成功',
        type: 'success',
        time: 1000
      };
陈峰 authored
115
      if (result.code === 200) {
yyq authored
116 117
        this.UPDATE_ARTICLE_COMMENT_COUNT({articleId: this.destId});
yyq authored
118 119 120
        if (this.autoUpdate && this.addType === 0) {
          await this.fetchArticleUpdate({articleId: this.destId});
        }
陈峰 authored
121
陈峰 authored
122 123
        this.$emit('on-comment', {
          destId: this.destId,
yyq authored
124
          parentId: this.rootId || void 0,
陈峰 authored
125
          addType: this.addType,
yyq authored
126 127 128 129 130 131 132 133
          columnType: this.columnType,
          comment: {
            content,
            destId: get(result, 'data', ''),
            columnType: this.columnType,
            parentId: this.addType === 1 ? this.destId : void 0,
            rootId: this.rootId || void 0
          }
陈峰 authored
134
        });
TaoHuang authored
135 136

        this.reportComment(this.destId);
陈峰 authored
137
      } else {
yyq authored
138
        Object.assign(toastInfo, {
陈峰 authored
139 140
          txt: result.message || '服务器开小差了',
          type: 'warn',
yyq authored
141
        });
陈峰 authored
142
      }
yyq authored
143 144

      this.$createToast(toastInfo).show();
TaoHuang authored
145 146 147 148
    },
    reportComment(comId) {
      this.$store.dispatch('reportYas', {
        params: {
TaoHuang authored
149
          appop: YAS.eventName.comment,
TaoHuang authored
150 151 152 153 154 155 156
          param: {
            POS_ID: this.posId,
            ARTICLE_ID: this.articleId,
            COM_ID: this.addType === 1 ? comId : void 0
          }
        }
      });
陈峰 authored
157 158 159 160 161 162 163 164 165 166 167 168
    }
  },
  render(h) {
    return h(this.tag || 'div', {
      on: {
        click: () => {
          this.openComentInput();
        }
      }
    }, this.$slots.default);
  }
};