homeActions.js 8.44 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import HomeService from '../../services/HomeService';
import timeago from '../../utils/timeago';
import {number10KFormater} from '../../utils/numberFormater';
import {loginThenSyncUserInfo} from '../user/userActions';
import SlicedImage from '../../../common/components/SlicedImage';

const {

	HOME_BNS_REQUEST,
    HOME_BNS_SUCCESS,
    HOME_BNS_FAILURE,

	HOME_RECOMMENDATION_REQUEST,
    HOME_RECOMMENDATION_SUCCESS,
    HOME_RECOMMENDATION_FAILURE,

	POST_LIKE_REQUEST,
    POST_LIKE_SUCCESS,
    POST_LIKE_FAILURE,
    POST_UNLIKE_REQUEST,
    POST_UNLIKE_SUCCESS,
    POST_UNLIKE_FAILURE,

	SYNC_USER_REQUEST,
    SYNC_USER_SUCCESS,
    SYNC_USER_FAILURE,

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

export function likeOperation(post) {
	return (dispatch, getState) => {
		let operation = () => {
			if (post.isLike) {
				dispatch(postUnlike(post.id, post.section.id));
			} else {
				dispatch(postLike(post.id, post.section.id));
			}
		};

		let {user} = getState();
		if (user.profile.uid !== 0) {
			operation();
		} else {
			dispatch(loginThenSyncUserInfo(operation));
		}
	};
}

export function postLikeRequest(json) {
    return {
        type: POST_LIKE_REQUEST,
        payload: json
    };
}

export function postLike(postId, sectionId) {
	return (dispatch, getState) => {
		dispatch(postLikeRequest(postId));
		let {user} = getState();
        return new HomeService().postLike(postId, user.profile.uid, sectionId)
            .then(json => {
				dispatch({
					type: POST_LIKE_SUCCESS
				})
                __DEV__ && console.log(json);
            })
            .catch(error => {
				__DEV__ && console.log(error);
            });
	};
}

export function postUnlikeRequest(json) {
    return {
        type: POST_UNLIKE_REQUEST,
        payload: json
    };
}

export function postUnlike(postId, sectionId) {
	return (dispatch, getState) => {
		dispatch(postUnlikeRequest(postId));
		let {user} = getState();
        return new HomeService().postUnlike(postId, user.profile.uid, sectionId)
            .then(json => {
				dispatch({
					type: POST_UNLIKE_SUCCESS
				})
				__DEV__ && console.log(json);
            })
            .catch(error => {
				__DEV__ && console.log(error);
            });
	};
}

export function bnsRequest() {
    return {
        type: HOME_BNS_REQUEST,
    };
}

export function bnsSuccess(json) {
    return {
        type: HOME_BNS_SUCCESS,
        payload: json
    };
}

export function bnsFailure(error) {
    return {
        type: HOME_BNS_FAILURE,
        payload: error
    };
}

export function bannerNoticeSection() {
	return (dispatch, getState) => {

		let {app, home} = getState();
		if (home.isFetching) {
			return;
		}
		dispatch(bnsRequest());
        return new HomeService().bannerNoticeSection(app.container)
            .then(json => {
				let payload = parseBNS(json);
                dispatch(bnsSuccess(payload));
            })
            .catch(error => {
                dispatch(bnsFailure(error));
            });
	};
}

export function recommendationRequest(ptr) {
    return {
        type: HOME_RECOMMENDATION_REQUEST,
		payload: ptr,
    };
}

export function recommendationSuccess(json) {
    return {
        type: HOME_RECOMMENDATION_SUCCESS,
        payload: json
    };
}

export function recommendationFailure(error) {
    return {
        type: HOME_RECOMMENDATION_FAILURE,
        payload: error
    };
}

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

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

		dispatch(recommendationRequest(ptr));

		let uid = user.profile.uid;
		let lastedTime = 0;
		if (!ptr) {
			lastedTime = home.recommendation.lastedTime;
		}
		let limit = 10;
        return new HomeService().recommendation(uid, lastedTime, limit)
            .then(json => {
				let payload = parseRecommendation(json);
				if (!ptr) {
					let oldList = home.recommendation.list.toJS();
					let list = [...oldList, ...payload.list];
					payload.list = list;
				}
                dispatch(recommendationSuccess(payload));
            })
            .catch(error => {
                dispatch(recommendationFailure(error));
            });
	};
}

function parseBNS(json) {
	let {resourceList, forumInfo} = json;
	let {advertList, textNoticeList} = resourceList;

	let bannerList = advertList ? advertList : [];
	let bannerDuration = '3';

	let noticeList = [];
	let noticeDuration = '3';
	let noticeOpen = 'N';
	if (textNoticeList && textNoticeList.list) {
		noticeList = textNoticeList.list;
		if (textNoticeList.time) {
			noticeDuration = textNoticeList.time;
		};
		if (textNoticeList.open) {
			noticeOpen = textNoticeList.open;
		}
	}

	let section = [];
	forumInfo.map((item, i) => {
		let {hotPost, newPost} = item;

		let hotAvatar = hotPost && hotPost.user && hotPost.user.headIcon ? hotPost.user.headIcon : '';
		hotAvatar = SlicedImage.getSlicedUrl(hotAvatar, 30, 30);
		let newAvatar = newPost && newPost.user && newPost.user.headIcon ? newPost.user.headIcon : '';
		newAvatar = SlicedImage.getSlicedUrl(newAvatar, 30, 30);
		let sectionItem = {
			header: {
				id: item.forumCode ? item.forumCode : 0,
				logo: item.forumPic ? item.forumPic : '',
				title: item.forumName ? item.forumName : '',
				desc: item.forumDesc ? item.forumDesc : '',
				post: number10KFormater(item.postsNum),
				comment: number10KFormater(item.commentsNum),
				like: number10KFormater(item.praiseNum),
			},
			hot: {
				avatar: hotAvatar,
				content: hotPost && hotPost.contentData ? hotPost.contentData : '',
				hasImg: hotPost && hotPost.hasImgFlg ? hotPost.hasImgFlg : '',
				postId: hotPost && hotPost.postId ? hotPost.postId : 0,
				title: hotPost && hotPost.postsTitle ? hotPost.postsTitle : '',
			},
			new: {
				avatar: newAvatar,
				content: newPost && newPost.contentData ? newPost.contentData : '',
				hasImg: newPost && newPost.hasImgFlg ? newPost.hasImgFlg : '',
				postId: newPost && newPost.postId ? newPost.postId : 0,
				title: newPost && newPost.postsTitle ? newPost.postsTitle : '',
			},
			num: {
				onedayAddNum: item.oneDayAddNum,
			},
		};

		if (sectionItem.hot.title === '') {
			sectionItem.hot.title = sectionItem.hot.content;
		}

		if (sectionItem.new.title === '') {
			sectionItem.new.title = sectionItem.new.content;
		}

		section.push(sectionItem);
	});

	return {
		banner: {
			list: bannerList,
			duration: parseFloat(bannerDuration),
		},
		notice: {
			list: noticeList,
			duration: parseFloat(noticeDuration),
			open: noticeOpen,
		},
		section,
	};
}

function parseRecommendation(json) {
	let {lastedTime, list} = json;
	let posts = [];
	list && list.map((item, i) => {
		let {authorInfo, blocks} = item;

		let desc = '';
		let thumbs = [];
		blocks && blocks.map((item, i) => {
			let contentData = item.contentData ? item.contentData : '';
			if (desc === '' && item.templateKey === 'text') {
				desc = contentData;
			}

			if (item.templateKey === 'image') {
				let thumb = {
					src: contentData,
				};
				thumbs.push(thumb);
			}
		});

		let avatar = authorInfo && authorInfo.headIcon ? authorInfo.headIcon : '';
		let uid = authorInfo && authorInfo.uid ? authorInfo.uid : 0;
		let name = authorInfo && authorInfo.nickName ? authorInfo.nickName : '';
		let createTime = item.createTime;
		let timeagoStr = timeago(createTime);
		let isLike = item.hasPraise === 'Y' ? true : false;
		let title = item.postsTitle ? item.postsTitle : '';
		if (title === '') {
			title = desc;
			desc = '';
		}
		let id = item.id ? item.id : 0;

		let post = {
			author: {
				avatar,
				uid,
				name,
			},
			createTime,
			timeago: timeagoStr,
			isTop: item.isIndexTop === 0 ? false : true,
			isLike,
			id,
			title,
			desc,
			thumbs,
			section: {
				id: item.forumCode,
				name: item.forumName,
			},
			commentCount: item.comment,
			likeCount: item.praise,
		}

		posts.push(post);
	});

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