comment-placeholder.js 1.75 KB
import {createNamespacedHelpers} from 'vuex';
const {mapActions} = createNamespacedHelpers('comment');

export default {
  name: 'CommentPlaceholder',
  props: {
    tag: String,
    destId: Number,
    addType: Number,
    columnType: {
      type: Number,
      default: 1001
    },
    user: {
      type: String,
      default: ''
    }
  },
  methods: {
    ...mapActions(['postComment']),
    openComentInput() {
      const hint = this.user ? `回复${this.user}:` : '';

      if (this.$yoho.isYohoBuy) {
        this.$yoho.getInput({hint}, (content) => {
          this.comment(content);
        }, (e) => {
          console.error(e);
        });
      } else {
        this.$createDialog({
          type: 'prompt',
          title: '测试-输入',
          prompt: {
            placeholder: hint
          },
          onConfirm: (e, promptValue) => {
            this.comment(promptValue);
          }
        }).show();
      }
    },
    async comment(content) {
      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
      });

      if (result.code === 200) {
        this.$emit('on-comment', {
          destId: this.destId,
          addType: this.addType,
          columnType: this.columnType
        });
      } else {
        this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }
    }
  },
  render(h) {
    return h(this.tag || 'div', {
      on: {
        click: () => {
          this.openComentInput();
        }
      }
    }, this.$slots.default);
  }
};