im.js 1.65 KB
'use strict';

const _ = require('lodash');
const ImAPI = global.yoho.ImAPI;
const crypto = global.yoho.crypto;

const encryptedUid = uid => crypto.encryption(null, uid + '');

/**
 *  新建留言信息
 *  path: {host}/leavemessage/saveLeavemessage
 *
 *  @param {int} uid 用户id
 *  @param {int} conversationId 会话id
 *  @param {str} content 留言内容
 */
exports.saveMessage = (uid, conversationId, content) => {
    let params = {
        uid,
        conversationId,
        content,
        encryptedUid: encryptedUid(uid)
    };


    return ImAPI.post('/api/leavemessage/saveLeavemessage', params);
};



/**
 *  查询用户聊天记录
 *  @param {int} uid 用户uid
 *  @param [int] pageSize 每次加载的聊天记录
 *  @param [int] startTime
 *  @param [int] endTime
 */
exports.fetchImHistory = (uid, pageSize, startTime, endTime) => {
    pageSize = pageSize || 10;

    let params = {
        uid,
        pageSize,
        encryptedUid: encryptedUid(uid)
    };

    _.forEach({startTime, endTime}, (key, val) => {
        val && (params[key] = val);
    });

    return ImAPI.get('/api/conversationMessage/pageList', params)
        .then(result => {
            return _.get(result, 'data', []);
        }, () => []);
};

/**
 *  获取用户订单, 默认最近10笔
 *  @param {int} uid 用户uid
 *  @param {init} createTimeBegin 开始时间 
 */
exports.fetchOrderList = (uid, createTimeBegin) => {
    let params = {
        uid,
        encryptedUid: encryptedUid(uid)
    };

    _.forEach({createTimeBegin}, (key, val) => {
        val && (params[key] = val)
    });

    return ImAPI.get('/api/cs/queryUserLastTenOrdersNgoodsInfo', params);
};