index.js 3.17 KB
import Taro, {Component} from '@tarojs/taro';
import {View} from '@tarojs/components';
import contentCode from '../../utils/content-code';
import { Resources, ProductList, FilterMenu } from '../../components';
import searchImg from '../../static/images/search.png';
import {common as commonModel} from '../../models';
import './index.scss';
import { connect } from '@tarojs/redux';


@connect(({ filterMenu }) => ({
    filterMenu
}))

export default class Index extends Component {
	constructor() {
		super(...arguments)

		this.state = {
			tabs: [
				{
					text: '热门推荐',
					type: 0
				},
				{
					text: '热销',
					type: 1
				},
				{
					text: '即将发售',
					type: 2
				}
			],
			page: {},
			productList: {},
			stopLoading: {}
		}
	}

	config = {
		navigationBarTitleText: 'UFO'
	};

	componentDidMount() {
		let obj = {
			page: {},
			productList: {},
			stopLoading: {}
		};

		// 初始化数据
		this.state.tabs.map(item => {
			let type = item.type;

			obj.page[type] = 1;
			obj.productList[type] = [];
			obj.stopLoading[type] = false;
		});

		this.setState(obj, () => {
			this.getProductData();
		});
	}

	componentWillReceiveProps (nextProps) {
		let curType = nextProps.filterMenu.curType;
		let {productList} = this.state;

        if (productList[curType].length === 0) {
            this.getProductData(nextProps.filterMenu);
        }
    }

	getProductData(obj) {
		let {filterMenu} = this.props;
		let {page, productList, stopLoading} = this.state;
		obj = obj || filterMenu;
		let type = obj.curType;

		commonModel.search({
			limit: 10,
			type: type,
			page: page[type]
		}).then(ret => {
			if (ret && ret.code === 200) {
				let list = ret.data && ret.data.product_list || [];

                if (list.length === 0) {
					this.setState({
						stopLoading: Object.assign(stopLoading, {[type]: true})
					});
                } else {
					this.setState({
						productList: Object.assign(productList, {[type]: [...productList[type], ...list]})
					});
				}
			}
		})
	}

	onScrollToLower() {
		let {filterMenu} = this.props;
		let {page} = this.state;
		let type = filterMenu.curType;

        if (!this.state.stopLoading[type]) {
			this.setState({page: Object.assign(page, {[type]: page[type] + 1})}, () => {
				this.getProductData();
			});
        }
	}

	gotoNative() {
		Taro.navigateTo({
			url: '/pages/nativeTest/nativeTest'
		})
	}

	render() {
		let {tabs, productList} = this.state;
		let {filterMenu} = this.props;
		let list = productList[filterMenu.curType] || [];

		return (
			<ScrollView
				className='index-page'
				scrollY
				scrollWithAnimation
				scrollTop='0'
				lowerThreshold='20'
				onScrollToLower={this.onScrollToLower}>

				<View className="header-nav">
					<Text className="page-label">飞碟好物</Text>
					<Text onClick={this.gotoNative}>跳转原生页面</Text>
					<Navigator url="/pages/search/index" className="search" hover-class="none"><Image className="search-btn" src={searchImg}></Image></Navigator>
				</View>
				
				<Resources code={contentCode.index}></Resources>
				<FilterMenu tabs={tabs} tabClass="border"></FilterMenu>
				{
					list &&
					<ProductList list={list}></ProductList>
				}
			</ScrollView>
		)
	}
}