chat.js 4.75 KB
'use strict';
const orderModel = require('../../home/models/order');
const crypto = global.yoho.crypto;
const imApi = require('../models/im-api');
const userApi = require('../../passport/models/auth-helper');
const imModel = require('../models/im');
const helpers = global.yoho.helpers;

exports.appAdapter = (req, res, next) => {
    if (req.yoho.isApp && !req.user.uid) {
        req.user.uid = req.query.uid;
    }
    next();
};

exports.page = (req, res, next) => {
    let uid = req.user.uid || req.query.uid;

    userApi.profile(req.user.uid)
        .then(userinfo => {
            userinfo = userinfo.data || {};

            res.render('chat/index', {
                module: 'service',
                page: 'chat',
                pageStyle: 'service-chat',
                width750: true,
                localCss: true,
                imCs: global.yoho.config.domains.imCs,
                imSocket: global.yoho.config.domains.imSocket,
                userData: {
                    encrypteduid: crypto.encryption(null, uid + ''),
                    avatar: userinfo.head_ico ? helpers.image(userinfo.head_ico, 100, 100) : '',
                    uname: userinfo.profile_name
                }
            });
        })
        .catch(next);

};

exports.getOrders = (req, res, next) => {
    let uid = req.user.uid || req.query.uid;

    orderModel.getOrders({
        type: req.query.type || 1,
        page: req.query.page || 1,
        gender: req.query.gender || '1,3',
        yh_channel: req.query.channel || 1,
        uid
    }).then(result => {
        res.render('chat/chat-order', {
            layout: false,
            orders: result
        });
    }).catch(next);
};

/**
 *  获取历史消息
 *  Request:
 *
 */
exports.fetchHistory = (req, res) => {
    const uid = req.user.uid || req.query.uid;
    const encryptedUid = req.body.encryptedUid;
    const endTime = req.body.endTime;

    imApi.fetchImHistory(uid, encryptedUid, endTime).then(result => {
        res.json(result);
    });
};

/**
 *  Request: 发表留言
 *  type: POST
 *  params:
 *      conversationId  会话id
 *      content    留言内容
 */
exports.saveMSG = (req, res) => {
    const uid = req.user.uid || req.query.uid;
    const encryptedUid = req.body.encryptedUid;
    const conversationId = req.body.conversationId;
    const content = req.body.content;

    imApi.saveMessage(uid, encryptedUid, conversationId, content)
        .then(result => {
            res.json(result);
        });
};

/**
 *  Request:    获取订单
 *  type:   GET
 *
 *  TODO:
 *      1.  分页, 目前接口不支持
 *      2.  失败情况
 */
exports.fetchOrders = (req, res) => {
    const uid = req.user.uid || req.query.uid;
    const encryptedUid = req.body.encryptedUid;

    imApi.fetchOrderList(uid, encryptedUid)
        .then(result => {
            imModel.handleOrderList(result.data, 128, 170);
            res.json(result);
        }).catch(() =>{
            return res.json({
                code: 500,
                data: []
            });
        });
};

exports.saveEvalute = (req, res) => {
    const uid = req.user.uid || req.query.uid;
    const params = { uid };

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

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

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

    imApi.saveEvalute(params)
        .then(result => {
            return res.json(result);
        }).catch(() => {
            return res.json({
                code: 500,
                message: '评价失败'
            });
        });
};


exports.queryGlobalOrder = (req, res) => {
    const uid = req.user.uid || req.query.uid;
    const encryptedUid = req.body.encryptedUid;

    let emptyOrder = {
        code: 200,
        data: [],
        message: '获取失败'
    };

    imApi.queryGlobalOrder(uid, encryptedUid)
        .then(result=> {
            if (result.code === 200) {
                imModel.handleOrderList(result.data, 128, 170);
            }

            res.json(result);
        }, () => {
            res.json(emptyOrder);
        });
};

/**
 *  Request: 获取评价原因
 *  type: GET
 *  params:
 *      cvId  会话id
 */
exports.queryReasons = (req, res) => {
    const type = req.body.type;
    const uid = req.user.uid || req.query.uid;
    const encryptedUid = crypto.encryption(null, uid + '');

    imApi.queryReasons(uid, encryptedUid, type)
        .then(result=> {
            res.json(result);
        })
        .catch(() => {
            return res.json({
                code: 500,
                message: '拉取评价原因失败'
            });
        });
};