index.js 2.99 KB
import Taro, { Component } from '@tarojs/taro';
import { View, Text, Navigator} from '@tarojs/components';
import contentCode from '../../utils/content-code';
import {Resources} from '../../components';
import {classify as classifyModel} from '../../models';
import {getImgUrl} from '../../utils';
import './index.scss';
import getPrivateKey from '../../libs/request/getPrivateKey.js'

export default class Classify extends Component {
	constructor() {
		super(...arguments);
		this.state = {
            tabs: ['品类', '品牌'],
            actived: '品类',
            brandList: []
		};
	}

	config = {
		navigationBarTitleText: '分类'
	}

	async componentDidMount () {
    const pk = await getPrivateKey();
    Taro.setStorage({ key: 'verifyKey', data: pk });
		classifyModel.getBrandList().then(ret => {
			if (ret && ret.code === 200) {
				this.setState({
					brandList: ret.data && ret.data.brand_list || []
				});
			}
		})
    }
    
    changeTab(item) {
        this.setState({
            actived: item
        });
    }

	render () {
        let {tabs, actived, brandList} = this.state;
        let num = brandList.length - brandList.length % 3;
		return (
			<ScrollView
				className='classify-page'
				scrollY
				scrollWithAnimation
				scrollTop='0'
				lowerThreshold='20'
				upperThreshold='20'
				onScrolltoupper={this.onScrolltoupper}
				onScroll={this.onScroll}>
				<View className="tabs">
                    {
                        tabs.map(item => {
                            return (
                                <Text
                                className={item === actived ? 'actived tab-item' : 'tab-item'}
                                onClick={this.changeTab.bind(this, item)}
                                key={item}>{item}</Text>
                            )
                        })
                    }
                </View>
                {
                    actived === '品类' &&
                    <Resources code={contentCode.classify}></Resources>
                }
                {
                    actived === '品牌' &&
                    <View className="brand-list">
                        {
                            brandList.length > 0 &&
                            brandList.map((item, index) => {
                                return (
                                    <Navigator
                                    className={index < num ? 'brand-item border' : 'brand-item'}
                                    key={index}
                                    hover-class="none"
                                    url={`/pages/searchList/index?brand=${item.id}&name=${item.brand_name}`}>
                                        <Image src={getImgUrl(item.brand_logo, 160, 80)} mode="aspectFill" className="brand-img"></Image>
                                    </Navigator>
                                )
                            })
                        }
                    </View>
                }                
			</ScrollView>
		)
	}
}