workIMAction.js 4.9 KB
import * as types from '../constants/ActionTypes';  
import { getRongCloudToken,getUserInfo} from '../../utils/workIMUtil';  
import {message} from 'antd';
import store from 'store2';

export function changeIMShow(flag){
    return {
        type        : types.CHANGE_IM_CHAT_STATE,
        showChat    : flag
    }
}

export function getIMToken(data){  //获取开票类型
    return dispatch => { 
        return getRongCloudToken(data)
            .then(data => { 
                const {token='',app_key=''}=data;
                dispatch({
                    type        : types.LOAD_IM_TOKEN,
                    token       : token,
                    appid       : app_key
                });
            })
            .catch(err => { throw err; });
    };
}  



 
export function addNewMessage(message,userMap,selecedUser,my){ 
    const sdId=message.senderUserId;
    const {senderUserId,content:{content=''},sentTime}=message; 
    return dispatch => {  
        return getUserInfo(senderUserId)
            .then(data => {   
                let count=0;
                if(userMap[senderUserId]){
                    count=userMap[senderUserId].messages.length;
                    if(senderUserId==selecedUser){
                        count=0;
                    }else{
                        count=userMap[senderUserId].count?userMap[senderUserId].count+1:1;
                    }
                    userMap[senderUserId].messages.push({
                        content,
                        sentTime,
                        senderUserId,
                        name:data.name,
                        type:'user'
                    }) 
                }else{   
                    if(senderUserId==selecedUser){
                        count=0;
                    }else{
                        count=count+1;
                    }
                    userMap[senderUserId]=message;
                    userMap[senderUserId].messages=[{
                        content,
                        sentTime,
                        senderUserId, 
                        name:data.name,  
                        type:'user'
                    }]
                }
                userMap[senderUserId].name=data.name; 
                userMap[senderUserId].mobile=data.mobile;  
                userMap[senderUserId].count=count;
                dispatch({
                    type        : types.LOAD_USER_MAP,
                    userMap     : Object.assign({},userMap)
                });
                if(senderUserId==selecedUser){
                    dispatch({ 
                        type            : types.CHANGE_MESSAGES, 
                        messages        : userMap[senderUserId].messages.concat([])
                    });
                } 
                dispatch(changeIMShow(true));
                store(my.id, Object.assign({},userMap));
            })
            .catch(err => { throw err; });  
    };
}


 
export function choiceMessage(message,userMap,dispatch,messages){     
    
}
 
 

export function addMyMessage(message,senderUserId,userMap,user){ 
    return dispatch => {
        return new Promise((resolve, reject)=>{  
            setTimeout(() => {
                if(userMap[senderUserId]&&userMap[senderUserId].messages){
                    userMap[senderUserId].messages.push({
                        content:message.content.content,
                        sentTime:message.sentTime,
                        senderUserId:message.senderUserId,
                        name:user.name,
                        type:'my'
                    });
                    dispatch({
                        type            : types.CHANGE_MESSAGES,
                        selecedUser     : senderUserId,
                        messages        : userMap[senderUserId].messages.concat([])
                    });
                }else if(userMap[selecedUser]){
                    userMap[senderUserId].messages=[{
                        content:message.content.content,
                        sentTime:message.sentTime,
                        senderUserId:message.senderUserId,
                        name:user.name,
                        type:'my'
                    }];
                    dispatch({
                        type            : types.CHANGE_MESSAGES,
                        selecedUser     : senderUserId,
                        messages        : userMap[senderUserId].messages.concat([])
                    });
                }
                dispatch({
                    type        : types.LOAD_USER_MAP,
                    userMap 
                });  
                resolve();
                store(user.id, Object.assign({},userMap));
            }, 200);
        }); 
    };  
}





export function loadStoreMessage(id){ 
    const  tempDate=store(id)||{};   
    return {
        type        : types.LOAD_USER_MAP,
        userMap     : tempDate
    }
}