ShareDetailContainer.js 5.96 KB
import React, { Component } from 'react';
import ReactNative,{ StyleSheet, View, NativeModules, Dimensions, Platform, NativeAppEventEmitter} from "react-native";
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { Map } from 'immutable';
import queryString from 'query-string';

import * as shareDetailActions from '../reducers/shareDetail/shareDetailActions';
import ShareDetail from '../components/ShareDetail';
import { getSlicedUrl } from '../../classify/utils/Utils';

const actions = [
  shareDetailActions,
];

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 ShareDetailContainer extends Component {
  constructor(props) {
      super(props);
      this.showShareView = this.showShareView.bind(this);
      this._goBack = this._goBack.bind(this);
      this._addFavorite = this._addFavorite.bind(this);
      this._jumpWithUrl = this._jumpWithUrl.bind(this);
      this._onEndReached = this._onEndReached.bind(this);
      this._onPressProduct = this._onPressProduct.bind(this);

      this.subscription = NativeAppEventEmitter.addListener(
        'ProductShareDetailEvent',
        () => {
          let { shareDetail } = this.props;
          let shareDetailInfo = shareDetail ? shareDetail.toJS(): {};
          let productInfo = shareDetailInfo.productInfo;
          if (!productInfo || productInfo == {}) {
            return
          }
          this.showShareView(productInfo);
        }
    );
  }

  render() {
    let { shareDetail } = this.props;
    return (
      <View style={styles.container}>
        <ShareDetail
          shareDetail={shareDetail}
          showShareView={this.showShareView}
          goBack={this._goBack}
          addFavorite={this._addFavorite}
          jumpWithUrl={this._jumpWithUrl}
          onEndReached={this._onEndReached}
          onPressProduct={this._onPressProduct}
          collage_activity_id={this.props.collage_activity_id}
          collage_rebates_amount={this.props.collage_rebates_amount}
          collage_price={this.props.collage_price}
        />
      </View>
    )
  }

  componentDidMount() {
    this.props.actions.fetchShareCodeInfo(this.props.product_skn);
    this.props.actions.fetchShareDetail({product_skn: this.props.product_skn});
    this.props.actions.fetchFavoriteInfo({id: this.props.product_id, type: 'product'});
    this.props.actions.getProductList(false,this.props.product_skn);
  }

  componentWillUnmount() {
    this.subscription && this.subscription.remove();
  }

  _onPressProduct(product) {

      let productSkn = product && product.get('product_skn', 0);
      let product_id = product && product.get('product_id', 0);

      //拼团需要传递的字段
      let collage_activity_id = product && product.get('collage_activity_id', -1);
      let collage_price = product && product.get('collage_price', 0);

      if (!productSkn) {
          return;
      }

      let pageName = 'iFP_Alliance';
      if (Platform.OS === 'android') {
          pageName = 'aFP_Alliance';
      }

      let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.minealliance","params":{"type":"shareDetail","title":"有赚商品详情", "product_skn":"${productSkn}", "product_id": "${product_id}" ,"from_page_name":"${pageName}", "collage_activity_id":"${collage_activity_id}", "collage_price":"${collage_price}"}}`;

      ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
  }

  _onEndReached() {
    this.props.actions.getProductList(false,this.props.product_skn);
  }

  _addFavorite = () => {
    this.props.actions.addFavorite({id: this.props.product_id, type: 'product'});
  }

  showShareView = (productInfo) => {
    let { host } = this.props.app;
    let { shareDetail } = this.props;
    let shareDetailJS = shareDetail ? shareDetail.toJS(): null;
    let goodsList = productInfo.goodsList;
    let productPriceBo = productInfo.productPriceBo;
    let goodsInfo = goodsList ? goodsList[0] : {};
    let productSkn = productPriceBo.productSkn;
    let productUrl = productInfo.productUrl;
    let shortUrl = shareDetailJS && shareDetailJS.shareCodeInfo ? shareDetailJS.shareCodeInfo.shortUrl : '';
    let unionType = ReactNative.NativeModules.YH_CommonHelper.unionType();
    let qrCode;
    if (unionType) {
      let params = {
        productSkn,
        union_type: unionType,
      }
      productUrl = shortUrl;
      let paramsString = encodeURIComponent(JSON.stringify(params));
      qrCode = host + '/wechat/miniapp/img-check.jpg?param=' + paramsString + '&miniQrType=7';
    }else {
      qrCode = host + '/wechat/miniapp/img-check.jpg?param=' + productSkn + '&miniQrType=7';
    }

    let shareParam = {
        title: productInfo.productName ? productInfo.productName : '',
        content: productInfo.productName ? productInfo.productName : '',
        image: goodsInfo.colorImage,
        url: productUrl,
        miniProgramPath: 'pages/goodsDetail/goodsDetail?productSkn=' + productSkn,
        miniProgramQCodeUrl: qrCode,
        product_name: productInfo.productName ? productInfo.productName : '',
        sales_price: productPriceBo.originalSalesPrice + "",
        productDefaultImage: goodsInfo.colorImage,
        fromType: 'productDetail',
    };

    ReactNative.NativeModules.YH_CommonHelper.shareInfoWithParam(shareParam);
  }

  _goBack = () => {
    NativeModules.YH_CommonHelper.goBack();
  }

  _jumpWithUrl = () => {
    let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${this.props.product_skn}"}}`;
    NativeModules.YH_CommonHelper.jumpWithUrl(url);
  }
}

const DEVICE_WIDTH_RATIO = Dimensions.get('window').width / 375;
const styles = StyleSheet.create({
  container: {
    flex: 1,
  }
})

export default connect(mapStateToProps, mapDispatchToProps)(ShareDetailContainer)