BrandContainer.js 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
'use strict'

import React, {Component} from 'react';
import ReactNative, {
    StyleSheet,
    Dimensions,
    Platform,
    View,
    Text,
    NativeModules,
    InteractionManager,
    NativeAppEventEmitter,
} from 'react-native'

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import * as brandActions from '../reducers/brand/brandActions';

import Brand from '../components/brand/Brand';


const actions = [
    brandActions,
];

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 BrandContainer extends Component {
    constructor(props) {
        super(props);

        this._onPressSlideItem = this._onPressSlideItem.bind(this);
        this._onPressRecommendItem = this._onPressRecommendItem.bind(this);
        this._onPressFilter = this._onPressFilter.bind(this);
        this._onSelectChannel = this._onSelectChannel.bind(this);
        this._onPressSearch = this._onPressSearch.bind(this);
        this._onPressClearHistory = this._onPressClearHistory.bind(this);
        this._onClickCancelSearch = this._onClickCancelSearch.bind(this);
        this._onSearchTextChange = this._onSearchTextChange.bind(this);
        this._onPressKeyword = this._onPressKeyword.bind(this);
        this._onPressBrandSearchItem = this._onPressBrandSearchItem.bind(this);
        this._setBrandData = this._setBrandData.bind(this);
        this._setInitialListSize = this._setInitialListSize.bind(this);
        this._onPressBrandItem = this._onPressBrandItem.bind(this);
        this._onPressSearchHistoryItem = this._onPressSearchHistoryItem.bind(this);

    }

    componentDidMount() {

        let {selectedChannelId} = this.props.brand;

        this.props.actions.getBrandList(selectedChannelId);
        this.props.actions.getBrandResource(selectedChannelId);
        this.props.actions.searchHistory();
        this.props.actions.hotKeyword();
    }

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

    //原生跳转
    _onPressBrandItem(data, fromSearch=false){
        //blk品牌跳转需要:如果发现是店铺,全部跳基础店铺。----顺老板。
        if (data&&data.type == '2') {
            data.is_red_shop == '0';
            data.shop_template_type = '1';
        }
        ReactNative.NativeModules.YH_CommonHelper.blkPushBrandVC(data);
        // let eventName = '';
        // let params = {};
        //
        // if (this.props.brand.brandFliter == 1 || this.props.brand.brandFliter == 2) {
        //     eventName = 'YB_CATEGORY_BRAND_LIST_IMAGE';
        // } else {
        //     eventName = 'YB_CATEGORY_BRAND_LIST_TEXT';
        // }
        //
        // if (fromSearch) {
        //     eventName = 'YB_CATEGORY_BRAND_LIST_TEXT';
        // }
        //
        // let brandId = data.shop_id ? data.shop_id : '';
        // // 0-girl 1-boy 2-child 3-lifestyle
        // let gender = 0;
        // if (this.props.brand.selectedChannelId == 1) {
        //     gender = 1;
        // } else if (this.props.brand.selectedChannelId == 2) {
        //     gender = 0;
        // } else if (this.props.brand.selectedChannelId == 3) {
        //     gender = 2;
        // } else if (this.props.brand.selectedChannelId == 4) {
        //     gender = 3;
        // }
        // if (!brandId) {
        //     brandId = data.id ? data.id : '';
        // }
        // params = {
        //     BR_ID: brandId,
        //     GDER: gender + '',
        // };
        // // console.log(eventName)
        // // console.log(params)
        // NativeModules.YH_CommonHelper.logEvent(eventName, params);
    }

    //URL 拼装跳转规则跳转
    _onPressSlideItem(url, index=0) {

        let newUrl = this._handleBLKBannerURL(url);
        if (newUrl.length) {
            url = newUrl;
        }
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);

        // // 0-girl 1-boy 2-child 3-lifestyle
        // let gender = 0;
        // if (this.props.brand.selectedChannelId == 1) {
        //     gender = 1;
        // } else if (this.props.brand.selectedChannelId == 2) {
        //     gender = 0;
        // } else if (this.props.brand.selectedChannelId == 3) {
        //     gender = 2;
        // } else if (this.props.brand.selectedChannelId == 4) {
        //     gender = 3;
        // }
        //
        // let params = {
        //     BAN_NUM: parseInt(index) + 1 + '',
        //     TOURL: url,
        //     GDER: gender + '',
        // };
        //
        // NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_LIST_BAN', params);
    }

    _onPressRecommendItem(data, index=0) {
        let url = data.url;
        let newUrl = this._handleBLKBannerURL(url);
        if (newUrl.length) {
            url = newUrl;
        }
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);

        // let brandId = brandId = data.id ? data.id : '';
        // // 0-girl 1-boy 2-child 3-lifestyle
        // let gender = 0;
        // if (this.props.brand.selectedChannelId == 1) {
        //     gender = 1;
        // } else if (this.props.brand.selectedChannelId == 2) {
        //     gender = 0;
        // } else if (this.props.brand.selectedChannelId == 3) {
        //     gender = 2;
        // } else if (this.props.brand.selectedChannelId == 4) {
        //     gender = 3;
        // }
        // let params = {
        //     BAN_NUM: parseInt(index) + 1 + '',
        //     BR_ID: brandId,
        //     GDER: gender + '',
        // };
        // NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_LIST_BR', params);
    }

    _handleBLKBannerURL(url) {
        let strs= new Array();
        let dataString = '';
        if (url.indexOf('yohobuy=') !== -1) {
            strs = url.split("yohobuy=");
            if (strs.length == 1) {
                dataString = strs[0];
            } else {
                dataString = strs[1];
            }
        } else {
            strs = url.split("yohobuy=");
            if (strs.length == 1) {
                dataString = strs[0];
            } else {
                dataString = strs[1];
            }
        }
        try {
            var obj = {};
            if (dataString.length > 1) {
                obj = JSON.parse(dataString); //由JSON字符串转换为JSON对象
            }
            console.log(obj);
            if (obj.action == 'go.brand') {
                obj.parames.jumptoshop = 'N';
                obj.parames.type = '';
            } else if (obj.action == 'go.shop') {
                obj.params.is_red_shop = '0';
                obj.params.shop_template_type = '1';
            }
            let totalUrlWithType = "yohobuy=" + JSON.stringify(obj);
            if (strs.length > 1) {
                totalUrlWithType = strs[0] + totalUrlWithType;
            }

            return totalUrlWithType;
        } catch (e) {
            return '';
        }
        return '';
    }

    _onPressFilter(value) {
        this.props.actions.setBrandFilter(value);

        // let params = {
        //     CAT_NUM: parseInt(value) + 1 + '',
        // };
        //
        // NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_LIST_SG', params);
    }

    _setInitialListSize(count){
        this.props.actions.setInitialListSize(count);
    }

    _onSelectChannel(channel){
        let {
            brandListForBoy,
            brandListForGirl,
            brandListForKid,
            brandListForLifeStyle,
            reourceForBoy,
            reourceForGirl,
            reourceForKid,
            reourceForLifeStyle,
        } = this.props.brand;

        let channelId = channel.id;
        let brandListNeedRequest;
        let reourceNeedRequest;
        if (channelId == 1) {
            brandListNeedRequest = brandListForBoy.get('hasSuccess');
            reourceNeedRequest = reourceForBoy.get('hasSuccess');
        }else if (channelId == 2) {
            brandListNeedRequest = brandListForGirl.get('hasSuccess');
            reourceNeedRequest = reourceForGirl.get('hasSuccess');
        }else if (channelId == 3) {
            brandListNeedRequest = brandListForKid.get('hasSuccess');
            reourceNeedRequest = reourceForKid.get('hasSuccess');
        }else if (channelId == 4) {
            brandListNeedRequest = brandListForLifeStyle.get('hasSuccess');
            reourceNeedRequest = reourceForLifeStyle.get('hasSuccess');
        }

        if (!brandListNeedRequest) {
            this.props.actions.getBrandList(channelId);
        }
        if (!reourceNeedRequest) {
            this.props.actions.getBrandResource(channelId);
        }
        this.props.actions.setInitialListSize(0);
        this.props.actions.setBrandSelectedChannel(channelId);

        // let params = {
        //     CAT_NUM: channelId + '',
        // };
        //
        // NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_LIST_CH', params);
    }

    _onPressSearch() {
        this.props.actions.setShowSearch(!this.props.brand.showSearch);

        // if (!this.props.brand.showSearch) {
        //     let params = {
        //         SEARCH_POS: '2',
        //     };
        //
        //     NativeModules.YH_CommonHelper.logEvent('YB_SEARCH_CLICK', params);
        // }
    }

    _onPressClearHistory() {
        this.props.actions.clearSearchHistory();
    }

    _onClickCancelSearch() {
        this.props.actions.setShowSearch(!this.props.brand.showSearch);
        this.props.actions.setBrandKeyword('');
        this.props.actions.filteBrandForKeyword('');
    }

    _onSearchTextChange(text) {
        this.props.actions.setBrandKeyword(text);
        this.props.actions.filteBrandForKeyword(text);
    }

    _setBrandData(data, selectedChannelId) {
        this.props.actions.setBrandData(data, selectedChannelId);
    }

    _onPressKeyword(keyword) {
        this._onSearchTextChange(keyword);
    }

    _onPressBrandSearchItem(data) {
        this._onPressBrandItem(data, true);
        this.props.actions.insertSearchHistory(data);
        this._onClickCancelSearch();

        // let keyword = this.props.brand.search.keyword ? this.props.brand.search.keyword : '';
        // let params = {
        //     KEYWORD: keyword,
        //     POS_ID: '0',
        //     FLR_INDEX: '0',
        // };
        // // console.log('YB_BRAND_KEYWORD_SEARCH_C')
        // // console.log(params)
        // NativeModules.YH_CommonHelper.logEvent('YB_BRAND_KEYWORD_SEARCH_C', params);
        // NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_LIST_SC', {});
    }

    _onPressSearchHistoryItem(keyword, androiddata, index, section){
        // 关闭了android点击搜索历史直接跳转至相关页面,现调整为与IOS效果一致   by chenlin 2016.12.19
        this._onSearchTextChange(keyword);


        // //兼容IOS只需要搜索关键字,IOS设置搜索框内容为keyword即可,android需要点击直接跳转至相关页面
        // if (Platform.OS === 'ios') {
        //     this._onSearchTextChange(keyword);
        // }
        // else{
            // //先判断是否有androiddata数据,若有则直接调用原生方法,若无则可能是热门搜索信息,设置搜索框文字内容
            // if(androiddata){
            //     this._onPressBrandItem(androiddata);
            //     this.props.actions.insertSearchHistory(androiddata);
            // }
            // else
            //     this._onSearchTextChange(keyword);
        // }

        // let params = {
        //     KEYWORD: keyword,
        //     POS_ID: parseInt(section) + 1 + '',
        //     FLR_INDEX: parseInt(index) + 1 + '',
        // };
        // // console.log('YB_BRAND_KEYWORD_SEARCH_C')
        // // console.log(params)
        // NativeModules.YH_CommonHelper.logEvent('YB_BRAND_KEYWORD_SEARCH_C', params);
    }

    render() {
        let {
            showSearch,
            initialListSize,
            isFetching,
            selectedChannelId,
            brandFliter,
            brandListForBoy,
            brandListForGirl,
            brandListForKid,
            brandListForLifeStyle,
            reourceForBoy,
            reourceForGirl,
            reourceForKid,
            reourceForLifeStyle,
            search,
        } = this.props.brand;

        //ab版本部分元素高度有所区别
        let {abversion} = this.props.app;
        return (
            <View style={styles.container}>
                <Brand
                    isFetching={isFetching}
                    initialListSize={initialListSize}
                    selectedChannelId={selectedChannelId}
                    abversion={abversion}
                    brandFliter={brandFliter}
                    brandListForBoy={brandListForBoy}
                    brandListForGirl={brandListForGirl}
                    brandListForKid={brandListForKid}
                    brandListForLifeStyle={brandListForLifeStyle}
                    reourceForBoy={reourceForBoy}
                    reourceForGirl={reourceForGirl}
                    reourceForKid={reourceForKid}
                    reourceForLifeStyle={reourceForLifeStyle}
                    onPressFilter={this._onPressFilter}
                    onPressSlideItem={this._onPressSlideItem}
                    onPressRecommendItem={this._onPressRecommendItem}
                    onSelectChannel={this._onSelectChannel}
                    showSearch={showSearch}
                    search={search}
                    onPressSearch={this._onPressSearch}
                    onPressClearHistory={this._onPressClearHistory}
                    onClickCancel={this._onClickCancelSearch}
                    onTextChange={this._onSearchTextChange}
                    onPressBrandSearchItem={this._onPressBrandSearchItem}
                    setBrandData={this._setBrandData}
                    setInitialListSize={this._setInitialListSize}
                    onPressBrandItem={this._onPressBrandItem}
                    onPressSearchHistoryItem={this._onPressSearchHistoryItem}
                />
            </View>
        );
    }
}

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

});

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