messageActions.js 9.19 KB
'use strict';

import ReactNative from 'react-native';
import {Actions} from 'react-native-router-flux';
import MessageService from '../../services/MessageService';
import timeago from '../../utils/timeago';
import {number10KFormater} from '../../utils/numberFormater';


const {

    MESSAGE_INFO_REQUEST,
    MESSAGE_INFO_SUCCESS,
    MESSAGE_INFO_FAILURE,
    MESSAGE_CENTER_REQUEST,
    MESSAGE_CENTER_SUCCESS,
    MESSAGE_CENTER_FAILURE,
    MESSAGE_LIKE_REQUEST,
    MESSAGE_LIKE_SUCCESS,
    MESSAGE_LIKE_FAILURE,
    MESSAGE_SYST_REQUEST,
    MESSAGE_SYST_SUCCESS,
    MESSAGE_SYST_FAILURE,

} = require('../../constants/actionTypes').default;


export function likeMessageRequest(ptr) {
    return {

        payload: ptr,
        type: MESSAGE_LIKE_REQUEST,

    };
}

export function likeMessageSuccess(json) {
    return {

        payload: json,
        type: MESSAGE_LIKE_SUCCESS,

    };
}

export function likeMessageFailue(error) {
    return {

        type: MESSAGE_LIKE_FAILURE,
        payload: error
    };
}

export function messageInfoRequest(ptr) {
    return {
        type: MESSAGE_INFO_REQUEST,
        payload: ptr,
    };
}

export function messageInfoSuccess(json) {
    return {
        type: MESSAGE_INFO_SUCCESS,
        payload: json,
    };
}

export function messageInfoFailue(error) {
    return {
        type: MESSAGE_INFO_FAILURE,
        payload: error,
    };
}

export function replyMessageRequest(ptr) {
    return {
        type: MESSAGE_CENTER_REQUEST,
        payload: ptr,
    };
}

export function replyMessageSuccess(json) {
    return {
        type: MESSAGE_CENTER_SUCCESS,
        payload: json,
    };
}

export function replyMessageFailue(error) {
    return {
        type: MESSAGE_CENTER_FAILURE,
        payload: error,
    };
}


export function systemMessageRequest(ptr) {
    return {
        type: MESSAGE_SYST_REQUEST,
        payload: ptr,
    };
}

export function systemMessageSuccess(json) {
    return {
        type: MESSAGE_SYST_SUCCESS,
        payload: json,
    };
}

export function systemMessageFailue(error) {
    return {
        type: MESSAGE_SYST_FAILURE,
        payload: error,
    };
}



export function likeMessage(ptr = false) {
    return (dispatch, getState) => {
        let {user, message} = getState();

        // 接口请求跳出的条件:
        // 前置条件:下拉刷新优先级高于上拉加载
        if (ptr) {
            //下拉刷新直接执行
        } else {
            // 1.当次请求不是下拉刷新,同时正在进行下拉刷新的请求,跳出
            // 2.当次请求不是下拉刷新,同时接口请求正在加载中, 跳出
            // 3.当次请求不是下拉刷新,数据已全部加载完成,跳出
            if (message.like.ptr || message.like.isFetching || message.like.endReached || message.like.error) {
                return;
            }
        }

        dispatch(likeMessageRequest(ptr));

        let uid = user.profile.uid;
        let lastedTime = 0;
        if (!ptr) {
            lastedTime = message.like.lastedTime;
        }
        let limit = 10;

        return new MessageService().messageList(uid, 2, lastedTime, limit)
        .then(json => {
            let payload = parseMessageList(json);
            if (!ptr) {
                let oldList = message.like.list.toJS();
                let list = [...oldList, ...payload.list];
                payload.list = list;
            }
            dispatch(likeMessageSuccess(payload));
        });

    }
}


export function messageInfo(ptr = false) {
    return (dispatch, getState) => {
        let {user, message} = getState();

        if (message.centerMsg.isFetching) {
			return;
		}

        dispatch(messageInfoRequest(ptr));
        let uid = user.profile.uid;

        return new MessageService().messageInfo(uid)
            .then(json => {
                let payload = parseMessageInfo(json);
                dispatch(messageInfoSuccess(payload));
            })
            .catch(error => {
                dispatch(messageInfoFailue(error));
            });
    };
}

export function replyMessage(ptr = false) {
    return (dispatch, getState) => {
        let {user, message} = getState();
        if (!ptr) {
            if (message.centerMsg.ptr || message.centerMsg.isFetching || message.centerMsg.endReached || message.centerMsg.error) {
                return;
            }
        }

        let limit = 10;
        let uid = user.profile.uid;
        let lastedTime = 0;
        if (!ptr) {
            lastedTime = message.centerMsg.lastedTime;
        }

        dispatch(replyMessageRequest(ptr));

        return new MessageService().messageList(uid, 3, lastedTime, limit)
            .then(json => {
                let payload = parseMessageList(json);

                if (!ptr) {
                    let oldList = message.centerMsg.list.toJS();
                    let list = [...oldList, ...payload.list];
                    payload.list = list;
                }

                dispatch(replyMessageSuccess(payload));
            })
            .catch(error => {
                dispatch(replyMessageFailue(error));
            });
    };
}

export function systemMessage(ptr = false) {
    return (dispatch, getState) => {
        let limit = 10;
        let {user, message} = getState();
        let uid = user.profile.uid;

        let lastedTime = 0;
        if (!ptr) {
            lastedTime = message.system.lastedTime;
        }

        if (!ptr) {
            if (message.system.ptr || message.system.isFetching || message.system.endReached || message.system.error) {
                return;
            }
        }

        dispatch(systemMessageRequest(ptr));

        return new MessageService().messageList(uid, 1, lastedTime, limit)
            .then(json => {
                let payload = parseMessageList(json);
                if (!ptr) {
                    let oldList = message.system.list.toJS();
                    let list = [...oldList, ...payload.list];
                    payload.list = list;
                }
                dispatch(systemMessageSuccess(payload));
            })
            .catch(error => {
                dispatch(systemMessageFailue(error));
            });
    };
}


export function goToLikeMessage() {
    Actions.LikeMessage();
    return {
        type: GO_TO_LIKE_MESSAGE,
        // payload:
    };
}

export function goToSystemMessage() {
    Actions.SystemMessage();
    return {
        type: GO_TO_SYS_MESSAGE,
        // payload:
    };
}

function parseMessageInfo(json) {
    let {praiseMsg, systemMsg} = json;

    let like={} , sys={};

    if (praiseMsg) {
        like = {
            tips: praiseMsg.title || '',
            isRead: (praiseMsg.isRead === 'N')?false:true,
            timeagoStr:timeago(praiseMsg.createTime),
        };
    }

    if (systemMsg) {
        sys = {
            tips: systemMsg.title || '',
            isRead: (systemMsg.isRead === 'N')?false:true,
            timeagoStr:timeago(systemMsg.createTime),
        };
    }
    let result = {
        likeMsg: like,
        sysMsg: sys,
    };

    return result;

}

function parseMessageList(json) {
    let {lastedTime, list} = json;
    let messages = [];

    list && list.map((item, i) => {

        let id = item.id ? item.id : 0;
        let title = item.title ? item.title : '';
        let type = item.type ? item.type : 0;
        let isRead = item.isRead ? item.isRead : 'Y';
        let createTime = item.createTime;
		let timeagoStr = timeago(createTime);

        let subTitle = '';
        let contentType = 0;
        let user = {};
        let post = {};
        let {content} = item;
        if (content) {
            subTitle = content.subTitle ? content.subTitle : '';
            contentType = content.type ? content.type : 0;

            let {userInfo, postInfo} = content;
            if (userInfo) {
                let nickName = userInfo.nickName ? userInfo.nickName : '';
                let avatar = userInfo.headIcon ? userInfo.headIcon : '';
                let uid = userInfo.uid ? userInfo.uid : 0;
                let sign = userInfo.signature ? userInfo.signature : '';
                let backgroundImage = userInfo.bgPic ? userInfo.bgPic : '';
                user = {
                    nickName,
                    avatar,
                    uid,
                    sign,
                    backgroundImage,
                };
            }
            if (postInfo) {
                let sectionId = postInfo.forumCode ? postInfo.forumCode : '';
                let postId = postInfo.postId ? postInfo.postId : 0;
                let sectionName = postInfo.forumName ? postInfo.forumName : '';
                postId = parseInt(postId);
                let thumb = postInfo.content ? postInfo.content : '';
                post = {
                    id: postId,
                    thumb,
                    sectionId,
                    sectionName,
                };
            }
        }

		let message = {
            id,
			title,
            type,
            isRead,
			createTime,
			timeago: timeagoStr,
            user,
            post,
            subTitle,
            contentType,
		};

		messages.push(message);
	});

	let endReached = messages.length == 0;
	return {
		lastedTime,
		list: messages,
		endReached,
	};
}