chat.js 2.23 KB
'use strict';
const orderModel = require('../../home/models/order');
const crypto = global.yoho.crypto;
const imApi = require('../../serverAPI/im');
const imModel = require('../models/im');
const helpers = global.yoho.helpers;

exports.appAdapter = (req, res, next) => {
    if (req.yoho.isApp) {
        let uid = (req.query.uid || '').trim();

        req.user.uid = uid;
    }

    next();
};

exports.page = (req, res) => {
    res.render('chat/index', {
        module: 'service',
        page: 'chat',
        pageStyle: 'service-chat',
        width750: true,
        localCss: true,
        imServer: global.yoho.config.domains.imServer,
        userData: {
            uid: req.user.uid,
            encrypteduid: req.user.ENCRYPTION_UID || (crypto.encryption(null, req.user.uid + '' || '0')),
            avatar: helpers.image(req.user.AVATAR, 100, 100),
            uname: req.user.NAME
        }
    });
};

exports.getOrders = (req, res, next) => {
    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: req.user.uid
    }).then(result => {
        res.render('chat/chat-order', {
            layout: false,
            orders: result
        });
    }).catch(next);
};

/**
 *  获取历史消息
 *  Request:
 *
 */
exports.msghistory = (req, res) => {
    const uid = req.user.uid;

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

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

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

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

    imApi.fetchOrderList(uid)
        .then(result => {
            imModel.handleOrderList(result.data);
            res.json(result);
        });
};