MessageContainer.js 8.72 KB
'use strict';

import React, {Component} from 'react';

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import * as allianceActions from '../reducers/alliance/allianceActions';
import {StyleSheet, View, Dimensions} from "react-native";
import MessageList from "../components/MessageList";
import LoadingIndicator from '../../common/components/LoadingIndicator';
import ReactNative from "react-native";
import { 
    shareGroupPurchaseDetail, 
    shareH5, 
    shareGoodsDetail, 
    shareGroupPurchaseList,
    shareStrollDetail
} from '../../common/utils/commonShareUtils'
import Request from '../../common/services/NativeRequest';
import {getSlicedUrl} from '../../classify/utils/Utils';

const actions = [
    allianceActions,
];

function mapStateToProps(state) {
    return {
        ...state
    };
}

function mapDispatchToProps(dispatch) {

    const creators = Map()
        .merge(...actions)
        .filter(value => typeof value === 'function')
        .toObject();

    return {
        actions: bindActionCreators(creators, dispatch),
        dispatch
    };
}

class MessageContainer extends Component {
    constructor(props) {
        super(props);
        this._resourceJumpWithUrl = this._resourceJumpWithUrl.bind(this);
        this._shareAction = this._shareAction.bind(this);

        let baseURL = 'http://api.yoho.cn';
        this.api = new Request(baseURL);
    }

    componentDidMount() {
        this.props.actions.getMessageResourceInfo();
    }

    componentWillUnmount() {

    }

    _resourceJumpWithUrl(url, type, params) {
        if (!url) {
            return;
        }
        ReactNative.NativeModules.YH_CommonHelper.logEvent('YB_CPS_MESSAGE_ENTER_C', params);
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    async _shareAction(params) {
        const union_type = ReactNative.NativeModules.YH_CommonHelper.unionType();
        switch(params.type) {
            // 商品详情
            case 1: {
                let shareInfo = {
                    productInfo: params.productInfo,
                    union_type
                }
                let shareParam = shareGoodsDetail(shareInfo);
                ReactNative.NativeModules.YH_CommonHelper.shareInfoWithParam(shareParam);
                break;
            }
            // H5
            case 2: {
                let shareId = params.someKey;
                this.api.get({
                    url: '/operations/api/v5/webshare/getShare',
                    body: {
                        shareId,
                        fromPage: 'iFP_H5'
                    }
                }).then((json) => {
                    let bigImage = json.data.bigImage
                    let shareInfo = {
                        ...params,
                        data: {
                            bigImage: bigImage ? getSlicedUrl(bigImage, 150 * DEVICE_WIDTH_RATIO, 120 * DEVICE_WIDTH_RATIO, 2) : '',
                            content: json.data.content,
                            title: json.data.title,
                            shareUrl: json.data.url,
                            pic: json.data.pic
                        },
                        union_type
                    }
                    let shareParam = shareH5(shareInfo);
                    ReactNative.NativeModules.YH_CommonHelper.shareInfoWithAnyParam(shareParam);
                    return json;
                }).catch((err) => {
                    let url = `${params.url.split('?')[0]}?shareId=${shareId}`
                    let shareInfo = {
                        ...params,
                        data: {
                            bigImage: params.image,
                            content: params.content,
                            title: params.content,
                            shareUrl: url,
                            pic: params.image
                        },
                        union_type,
                        isCatch: true
                    }
                    let shareParam = shareH5(shareInfo);
                    ReactNative.NativeModules.YH_CommonHelper.shareInfoWithAnyParam(shareParam);
                    throw(err);
                });
                break;
            }
            // 资讯    
            case 4: {
                let json = await this.api.get({
                    url: '/guang/api/v1/share/guang',
                    body: {
                        id: params.someKey,
                        fromPage: 'iFP_GuangDetail',
                        yh_channel: '1'
                    }
                }).catch((error) => {
                    let param = {
                        url: params.url.split('?')[0],
                        title: params.content,
                        content: params.content,
                        pic: params.image,
                        minSortName: 0,
                        wechatShareImgUrl: params.image,
                        union_type
                    };
                    let shareParam = shareStrollDetail(param);
                    ReactNative.NativeModules.YH_CommonHelper.shareInfoWithGuangParam(shareParam);
                    throw(error);
                });
                let data = json;
                let param = {
                    ...data,
                    someKey: params.someKey,
                    union_type
                }
                let shareParam = shareStrollDetail(param);
                ReactNative.NativeModules.YH_CommonHelper.shareInfoWithGuangParam(shareParam);
                break;
            }
            // 拼团列表     
            case 5: {
                this.api.get({
                    url: '/operations/api/v5/webshare/getShare',
                    body: {
                        shareId: 5409,
                    }
                }).then((json) => {
                    let shareInfo = {
                        activityId: params.someKey,
                        shareCodeInfo: json.data,
                        union_type
                    }
                    let shareParam = shareGroupPurchaseList(shareInfo);
                    ReactNative.NativeModules.YH_CommonHelper.shareInfoWithAnyParam(shareParam);
                    return json;
                }).catch((error) => {
                    let json = {
                        "bigImage": "http://img12.static.yhbimg.com/taobaocms/2018/09/13/09/020e504e869fcad106f9687aec57f263d1.jpeg",
                        "content": "有货福利团!精选好货,拼团省钱!",
                        "pic": "http://img13.static.yhbimg.com/taobaocms/2018/09/07/17/02f7cc420cfde1aeff46a9239b07221319.jpeg",
                        "praise_num": 0,
                        "title": "有货福利团!精选好货,拼团省钱!",
                        "url": "https://m.yohobuy.com/?userUid=59100243"
                    };
                    let shareInfo = {
                        activityId: params.someKey,
                        shareCodeInfo: json,
                        union_type
                    };
                    let shareParam = shareGroupPurchaseList(shareInfo);
                    ReactNative.NativeModules.YH_CommonHelper.shareInfoWithAnyParam(shareParam);
                    throw(error);
                })
                break;
            }
            // 拼团详情
            case 6: {
                let shareInfo = {
                    activityId: params.someKey,
                    productInfo: params.productInfo,
                    union_type
                }
                let shareParam = shareGroupPurchaseDetail(shareInfo);
                ReactNative.NativeModules.YH_CommonHelper.shareInfoWithParam(shareParam);
                break;
            }
            default:
                break;                                           
        }
    }

    render() {
        let {messageList} = this.props.alliance;
        let isFetching = messageList.isFetching;
        return (
            <View style={styles.container}>
                <MessageList
                    messageList={messageList}
                    resourceJumpWithUrl={this._resourceJumpWithUrl}
                    shareAction={this._shareAction}
                />
                <LoadingIndicator isVisible={isFetching}/>
            </View>
        )
    }
}

let { width } = Dimensions.get('window');
const DEVICE_WIDTH_RATIO = width / 375;

let styles = StyleSheet.create({
    container: {
        flex: 1,
    },
});

export default connect(mapStateToProps, mapDispatchToProps)(MessageContainer);