area-comment.js 11.5 KB
import Page from './yoho-page';

const $ = require('jquery');
const lazyload = require('./plugins/lazyload');
const Swiper = require('swiper/dist/js/swiper');

window.jQuery = $;

require('./common/area-common');

class Comment extends Page {
    constructor() {
        super();

        this.selector = {
            $openMore: $('.open-more'),
            $closeMore: $('.close-more'),
            $showBig: $('.show-big')
        };

        this.page = 1;
        this.limit = 5;
        this.loading = false;
        this.end = false;

        this.init();
    }

    init() {
        this.bindEvents();
        this.scroll();
    }

    bindEvents() {
        $('.comment-list').on('click', '.open-more', this.openMore.bind(this));
        $('.comment-list').on('click', '.close-more', this.closeMore.bind(this));
        $('.comment-list').on('click', '.show-big', this.showBig.bind(this));
    }

    scroll() {
        $(window).scroll(() => {
            if (($(window).scrollTop() + $(window).height() >= $(document).height() * 0.8)) {
                this.doMore();
            }
        });
    }

    doMore() {
        if (!this.loading && !this.end) {
            this.page++;
            this.getMore();
        }
    }

    getMore() {
        this.loading = true;
        $.ajax({
            method: 'GET',
            url: `${window.$ajaxPath}/TblStoreAssessmentRest/commentsforh5`,
            data: {
                store_id: $('#storeId').val(),
                page: this.page || 1,
                limit: this.limit || 5
            },
            success: (result) => {
                let list = [];
                let $commentList = $('.comment-list');

                if (result && result.data && result.data.list && result.data.list.length > 0) {
                    list = result.data.list;
                    list.forEach((val) => {
                        let $html = $(`<div class="comment-item-c clearfix">
                            <div class="comment-item comment-main clearfix">
                                <div class="left"></div>
                                <div class="right">
                                    <div class="name">
                                        <span class="user-name"></span>
                                        <div class="ufo"></div>
                                        <span class="time">
                                            <span class="clock-icon ada-icon"></span>
                                            ${val.publish_time_str}
                                        </span>
                                    </div>
                                    <div class="price"></div>
                                    <div class="comment-words">${val.description}</div>
                                    <div class="pic-list"></div>
                                </div>
                            </div>
                            <div class="comment-follow"></div>
                        </div>`);

                        if (val.user && val.user.level) {
                            // 用户等级

                            $html.find('.ufo').addClass(`ufo-${val.user.level}`);
                        }

                        if (val.consume && val.consume !== '0') {
                            // 用户消费
                            let $consume = $(`<span class="cny">CNY</span>
                            <span>${val.consume} /人</span>`);

                            $html.find('.comment-main .price').append($consume);
                        }

                        if (val.user) {
                            // 用户图片-名称
                            let headpic = val.user.headpic.split('?')[0];
                            let $userPic = $(`<img class="head-pic lazy" alt="${val.user.nickname}" 
                            data-original="${headpic}?imageView2/1/w/132/h/132" />`);
                            let userName = val.user.nickname;

                            $html.find('.comment-main .left').append($userPic);
                            $html.find('.comment-main .user-name').text(userName);
                        }

                        if (val.img) {
                            // 评论图片
                            if (val.img.length === 1) {
                                let imgUrl = val.img[0].url.split('?')[0];
                                let $img = $(`<img class="lazy show-big" alt="alt" 
                                data-original="${imgUrl}?imageView2/1/w/360/h/360" />`);

                                $html.find('.comment-main .pic-list').append($img).addClass('single');
                            }
                            if (val.img.length > 1) {
                                val.img.forEach((item) => {
                                    let imgUrl = item.url.split('?')[0];
                                    let $img = $(`<img class="lazy show-big" alt="alt" 
                                    data-original="${imgUrl}?imageView2/1/w/360/h/360" />`);

                                    $html.find('.comment-main .pic-list').append($img);
                                });
                            }
                        }

                        if (val.comment && val.comment.length > 0) {
                            // 二级评论
                            val.comment.forEach((item) => {
                                let $comment = $(`<div class="comment-item clearfix">
                                    <div class="left"></div>
                                    <div class="right">
                                        <div class="name">
                                            <span class="user-name"></span>
                                            <span class="time">${item.create_time_str}</span>
                                        </div>
                                        <div class="comment-words">${item.content}</div>
                                    </div>
                                </div>`);

                                if (item.user) {
                                    let userName = item.user.nickname || '';

                                    if (item.user.headpic) {
                                        let headpic = item.user.headpic.split('?')[0];
                                        let $userPic = $(`<img class="head-pic lazy" alt="${userName}" 
                                                    data-original="${headpic}?imageView2/1/w/152/h/152" />`);

                                        $comment.find('.left').append($userPic);
                                    }

                                    $comment.find('.user-name').text(userName);
                                }
                                $html.find('.comment-follow').append($comment);
                            });
                        }

                        if (val.comment_num > 3) {
                            // 更多评论
                            let commentNum = parseInt(val.comment_num, 10) > 10 ? 10 : parseInt(val.comment_num, 10);
                            let $moreComment = $(`
                            <div class="more-comment open-more" id="${val.id}">所有${commentNum}条评论&nbsp;</div>
                            <div class="more-comment close-more">收起评论&nbsp;</div>`);

                            $html.append($moreComment);
                        }

                        $commentList.append($html);
                        lazyload($html.find('img.lazy'), {
                            threshold: 1000,
                            q: 80
                        });
                        this.loading = false;
                    });
                } else {
                    this.end = true;
                }
            }
        });
    }

    showBig(e) {
        $('body').append(`<div class="big-pic-c">
                            <div class="bg"></div>
                            <div class="swiper-container">
                                <div class="swiper-wrapper">
                                </div>
                            </div>
                        </div>`);

        let $imgIndex = $(e.currentTarget).index();
        let $img = $(e.currentTarget).parent('.pic-list').find('img');

        let swiper = new Swiper('.swiper-container', {
            observer: true,
            observeParents: true
        });

        swiper.slideTo($imgIndex, 1, false);

        $img.each(function() {
            $('.swiper-wrapper').append(`<div class="swiper-slide"><img src='${$(this).attr('src')}' /></div>`);
        });

        $('.big-pic-c').on('click', this.hideBig.bind(this));
    }

    hideBig() {
        $('.big-pic-c').remove();
    }

    openMore(e) {
        let $this = $(e.currentTarget);

        if ($this.hasClass('opened')) {
            $this.parents('.comment-item-c').find('.comment-follow .comment-item').show();
            $this.hide();
            $this.next('.close-more').show();
            return false;
        } else {
            $this.addClass('opened');
        }

        $.ajax({
            method: 'GET',
            url: `${window.$ajaxPath}/TblStoreAssessmentRest/subcommentsforh5`,
            data: {
                comment_id: $this.attr('id'),
                page: 1,
                limit: 10
            },
            success: (result) => {
                if (result && result.data && result.data.list && result.data.list.length > 0) {
                    let list = result.data.list;

                    $this.parents('.comment-item-c').find('.comment-follow').empty();
                    list.forEach((item) => {
                        let $comment = $(`<div class="comment-item clearfix">
                            <div class="left"></div>
                            <div class="right">
                                <div class="name">
                                    <span class="user-name"></span>
                                    <span class="time">${item.create_time_str}</span>
                                </div>
                                <div class="comment-words">${item.content}</div>
                            </div>
                        </div>`);

                        if (item.user) {
                            let userName = item.user.nickname || '';

                            if (item.user.headpic) {
                                let headpic = item.user.headpic.split('?')[0];
                                let $userPic = $(`<img class="head-pic lazy" alt="${userName}" 
                                            data-original="${headpic}?imageView2/1/w/152/h/152" />`);

                                $comment.find('.left').append($userPic);
                            }

                            $comment.find('.user-name').text(userName);
                        }
                        $this.parents('.comment-item-c').find('.comment-follow').append($comment);
                        lazyload($this.parents('.comment-item-c').find('.comment-follow').find('img.lazy'), {
                            threshold: 1000,
                            q: 80
                        });
                    });
                    $this.hide();
                    $this.next('.close-more').show();
                }
            }
        });
    }

    closeMore(e) {
        let $this = $(e.currentTarget);

        $this.parents('.comment-item-c').find('.comment-follow .comment-item:gt(2)').hide();
        $this.prev('.open-more').show();
        $this.hide();
    }
}

$(() => {
    new Comment();
});