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

'use strict';
const aes = require('./aes-pwd');
const common = require('../../../config/common');
const clientApi = require('../models/client-api');
const clientService = require('../models/client-service');

/**
 * 在线客服客户端
 */
const index = (req, res, next) => {
    let type = 2;
    let reg = /MSIE\s?[987]\.0/i;
    let userAgent = req.headers['user-agent'];
    let unSupport = reg.test(userAgent);
    let encryptedUid = aes.encryptionUid(req.user.uid);
    let data = {
        encryptedUid,
        layout: false
    };

    if (unSupport) {
        res.render('unsupport', {
            layout: false
        });
    } else {
        clientService.getClientData(type, encryptedUid)
            .then(result => {
                res.render('client', Object.assign(data, result));
            }).catch(next);
    }
};

/**
 * 在线客服客户端
 */
const domains = (req, res) => {
    // 返回当前环境的配置信息
    res.json(common.domains);
};

/**
 * 消息记录
 * @param req
 * @param res
 * @param next
 */
const history = (req, res, next) => {
    const encId = aes.encryptionUid(req.user.uid);
    const endTime = req.body.endTime;

    clientApi.getMsgHistory(encId, endTime)
        .then(result => {
            res.json(result);
        }).catch(next);
};

/**
 * 评价
 * @param req
 * @param res
 * @param next
 */
const saveEval = (req, res, next) => {
    const params = {};

    params.stars = req.body.stars;
    params.promoter = req.body.promoter;
    params.encryptedUid = req.body.encryptedUid;
    params.conversationId = req.body.conversationId;

    if (req.body.reasonIds) {
        params.reasonIds = req.body.reasonIds;
    }

    if (req.body.reasonMsg) {
        params.reasonMsg = req.body.reasonMsg;
    }

    clientApi.saveEval(params)
        .then(result => {
            res.json(result);
        }).catch(next);
};

/**
 * 评价原因
 * @param req
 * @param res
 * @param next
 */
const queryReason = (req, res, next) => {
    const cvId = req.body.conversationId;

    clientApi.queryReason(cvId)
        .then(result => {
            res.json(result);
        }).catch(next);
};

/**
 * 留言
 * @param req
 * @param res
 * @param next
 */
const saveMessage = (req, res, next) => {
    const content = req.body.content;
    const encId = req.body.encryptedUid;
    const cvId = req.body.conversationId;

    clientApi.saveMessage(content, encId, cvId)
        .then(result => {
            res.json(result);
        }).catch(next);
};


module.exports = {
    index,
    domains,
    history,
    saveEval,
    queryReason,
    saveMessage
};