client-service.js 2.33 KB
/**
 * 客服用户端 service
 *
 * @author: liqi <qi.li@yoho.cn>
 * @date: 2016/11/4
 */

'use strict';

const _ = require('lodash');
const Promise = require('bluebird');
const clientAPI = require('./client-api');

/**
 * 在线客服客服端页面
 * @param { number } type
 * @param { string } encryptedUid
 * @return { Object } 客服设置
 */
const getClientData = (uid, type, encryptedUid) => {
    const logoSize = '136x40';
    const qcSize = '135x135';
    const advSize = '160x335';
    const regExp = /\{width}x\{height}/g;

    let apiMethod = [
        clientAPI.getCsSetting(type),
        clientAPI.getMsgHistory(uid, encryptedUid),
        clientAPI.getLastTenOrders(uid, encryptedUid),
        clientAPI.getQas(uid, encryptedUid)
    ];

    return Promise.all(apiMethod)
        .then(res => {
            let csSetting = {};
            let orderList = [];
            let qasList = [];
            let records = [];
            let hasHistory = false;

            if (res[1].code === 401 ||
                res[2].code === 401 ||
                res[3].code === 401) {
                return {
                    code: 401
                };
            }

            if (res[0] && res[0].code === 200) {
                if (res[0].data.config) {
                    csSetting = res[0].data.config;
                    csSetting.qrCode = csSetting.qrCode.replace(regExp, qcSize);
                    csSetting.pcAdImg = csSetting.pcAdImg.replace(regExp, advSize);
                    csSetting.windowLogo = csSetting.windowLogo.replace(regExp, logoSize);
                }
            }

            if (res[1] && res[1].code === 200) {
                records = res[1].data.records;
                hasHistory = records.length > 0;
            }

            if (res[2] && res[2].code === 200) {
                orderList = res[2].data;
            }

            if (res[3] && res[3].code === 200) {

                _.each(res[3].data, function(item) {
                    item.content = (item.content || '').replace(/src=["']https?:/ig, 'src="');
                });

                qasList = res[3].data;
            }

            return {
                csSetting: csSetting,
                hasHistory: hasHistory,
                orders: orderList,
                qas: qasList
            };
        });
};

module.exports = {
    getClientData
};