index.js 2 KB
import Taro, { Component } from '@tarojs/taro';
import { View } from '@tarojs/components';
import { common as commonModel } from '../../models';
import Focus from '../resource-focus';
import HotSeries from '../resource-hot-series';
import Category from '../resource-category';
import './index.scss';
import getPrivateKey from '../../libs/request/getPrivateKey.js'

export default class Resources extends Component {
	constructor(props) {
		super(props);
    this.state = {
      floors: []
    };
	}

	static defaultProps = {
		floor: [],
		title: '热门系列',
		isShow: false
	}

  async componentDidMount() {
    const pk = await getPrivateKey();
    Taro.setStorage({ key: 'verifyKey', data: pk });
    commonModel.resource(this.props.code).then(res => {
      if (res && res.code === 200) {
        this.setState({
          floors: res.data
        });
      }
    });
  }


	render() {
		let { isShow, floors } = this.props;
    if (isShow) {
      floors = this.state.floors;
    }

		return (
		<View className="resources">
			{
			floors.map((floor, idx) => {
				return (
					<View
						className={
							floor.template_name === 'tfGoodsList' ? 'resource-item no-right-padding' : 'resource-item'
						}
						key={floor.template_id}>
						{
							floor.template_name === 'focus' &&
							<Focus floor={floor.data}
                floorIdx={idx + 1}
                floorName={floor.template_name}
                floorId={floor.template_id}></Focus>
						}
						{
							floor.template_name === 'hotSeries' &&
							<HotSeries isShow={isShow} floor={floor.data}
                floorIdx={idx + 1}
                floorName={floor.template_name}
                floorId={floor.template_id}
                title={floor.template_intro}></HotSeries>
						}
						{

							floor.template_name === 'category' && floor.data.length > 0 &&
							floor.data.map((item, index) => {
								return (
									<Category floor={item} key={index}></Category>
								)
							})
						}
					</View>
				)
			})
			}
		</View>
		)
	}
}