view.js 6.04 KB
var $ = require('yoho-jquery');
var send = require('./socket/send');
var serviceApi = require('./service-api');
var socketConf = require('./socket/config');
var conMsg = socketConf.conversationMessage;


// 过程标示
var processSign = {
    encryptedUid: '',
    hasMore: true, // 更多消息
    manual: false, // 人工客服
    promoter: 1, // 评论发起者 1:客户自己 2:客服
    savedEval: false, // 是否保存过评论
    scrollLoad: false, // 滚动加载
    completeClose: false, // 评价完成后关闭
    loadingHistory: false // 加载消息
};

/**
 * 获取评价原因
 */
var _fetchReason = (function() {
    var cache;

    return function(render) {
        // 1:企业端 2:YOHO在线客服
        // 3:BLK在线客服 4:MARS在线客服 5:机器人
        var YOHO_CS = 2;

        if (cache) {
            return render(cache);
        }

        serviceApi.reason({
            type: YOHO_CS
        })
            .done(function(res) {
                if (res && res.code === 401) {
                    window.onbeforeunload = null;
                    return location.href = '//www.yohobuy.com/signin.html?refer=' +   // eslint-disable-line
                        encodeURIComponent(location.href);
                }

                if (res.code === 200) {
                    cache = res.data;
                    render(cache);
                }
            });
    };
}());

/**
 * 评价客服view
 * @constructor
 */
function Evaluate() {
    this.isOpen = false; // 是否已经开启
    this.$view = $('#makeEvaluation');
    this.disTpl = require('hbs/service/discontent-row.hbs');
    this.bindEvents();
    this.submitting = false;
}

/**
 * 星评
 */
function _starChange(e) {
    var i;
    var len;
    var $el;
    var self = this,
        $ct = $(e.currentTarget),
        index = $ct.index(),
        $reason = self.$view.find('.detail-reason'),
        $text = self.$view.find('.star-text'),
        $list = self.$view.find('.star');

    var starText = {
        0: '非常不满意',
        1: '不满意',
        2: '一般',
        3: '满意',
        4: '非常满意'
    };

    for (i = 0, len = $list.length; i < len; i++) {
        $el = $($list[i]);
        $el.index() <= index && $el.addClass('positive');
        $el.index() > index && $el.removeClass('positive');
    }

    index < 3 && $reason.show();
    index >= 3 && $reason.hide();
    $text.text(starText[index]);
}

/**
 * 评价提交
 */
function _evalSubmit() {
    var data,
        reasonIds,
        idArray = [],
        self = this,
        $btnEval = $('.icon.evaluate'),
        $modal = $('#makeEvaluation'),
        reason = $modal.find('textarea').val().trim(),
        star = $modal.find('.star.positive').length,
        reasonTypes = $modal.find('.dis-row .type.chosen');

    if (self.submitting) {
        return;
    }

    self.submitting = true;

    data = {
        conversationId: conMsg.conversationId,
        encryptedUid: processSign.encryptedUid,
        promoter: processSign.promoter,
        stars: star
    };

    if (star < 4) {
        $.each(reasonTypes, function(index, item) {
            idArray.push($(item).data('id'));
        });

        reasonIds = idArray.join(':');
        data.reasonIds = reasonIds;
        data.reasonMsg = reason;
    }

    serviceApi.evaluate(data)
        .done(function(res) {
            $btnEval.hide();
            processSign.savedEval = true;

            if (res && res.code === 401) {
                window.onbeforeunload = null;
                return location.href = '//www.yohobuy.com/signin.html?refer=' +   // eslint-disable-line
                    encodeURIComponent(location.href);
            }

            if (res && res.code === 200) {
                send.completeEval();
                self.close();

                setTimeout(function() {
                    if (processSign.completeClose) {
                        window.close();
                    }
                }, 500);
            }
        })
        .always(function() {
            self.submitting = false;
        });
}

Evaluate.prototype = {
    bindEvents: function() {
        var self = this;

        this.$view.on('click', '.star', _starChange.bind(self))
            .on('click', '.submit', _evalSubmit.bind(self))
            .on('click', '.dis-row .type', function(e) {
                $(e.target).toggleClass('chosen');
            })
            .on('click', '.close', function() {
                self.close();
            });
    },

    open: function(close) {
        var self = this;
        var _resetEval;
        var _show;

        if (self.isOpen) {
            return;
        }
        processSign.completeClose = close;
        if (!processSign.manual) {
            return;
        }

        _resetEval = function() {
            var btnText = close ? '提交并关闭' : '提交';

            self.$view.find('.star-text').html('非常满意');
            self.$view.find('.star').addClass('positive');
            self.$view.find('.other-reason').val('');
            self.$view.find('.detail-reason').hide();
            self.$view.find('.submit').html(btnText);
        };

        _show = function(data) {
            var i;
            var len;
            var dom = '';

            _resetEval();
            self.isOpen = true;
            if (!data || !data.length) {
                return self.$view.modal('show');
            }
            for (i = 0, len = data.length; i < len; i = i + 2) {
                dom += self.disTpl({
                    id1: data[i].id,
                    id1Content: data[i].content,
                    id2: data[i + 1] && data[i + 1].id,
                    id2Content: data[i + 1] && data[i + 1].content
                });
            }

            self.$view.find('.discontent').empty().append(dom);
            self.$view.modal('show');
        };

        _fetchReason(_show);
    },

    close: function() {
        this.isOpen = false;
        this.$view.modal('hide');
    }
};

Evaluate.prototype.constructor = Evaluate;


module.exports = {
    processInfo: processSign,
    Evaluate: Evaluate
};