index.js 3.26 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'
import router from '../../router/index.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
      });
  }

  goToList(brand) {
    router.go('productList', {
      brand: brand.id,
      name: brand.brand_name
    });
  }

    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 (
                                    <View
                                        className={index < num ? 'brand-item border' : 'brand-item'}
                                        key={index}
                                        hover-class="none"
                                        onClick={this.goToList.bind(this, item)}
                                    >
                                        <Image src={ getImgUrl(item.brand_logo, 160, 80) } mode ="aspectFit" className="brand-img"></Image>
                                    </View>
                                )
                            })
                        }
                    </View>
                }     
            </ScrollView>
        )
    }
}