courselist.js 11.5 KB
Newer Older
2  
xuzhenghua committed
1 2 3
import React, { Component } from 'react'
import { VList } from '../../common'
import { Tabs, WhiteSpace, Toast } from 'antd-mobile'
xuzhenghua committed
4
import './courselist.scss'
xuzhenghua committed
5
import HeaderSearch from '../../common/HeaderSearch/index'
2  
xuzhenghua committed
6
import { http, getParam } from "@/utils"
xuzhenghua committed
7
import Loading from '@/common/Loading'
2  
xuzhenghua committed
8
import { connect } from 'react-redux';
FE committed
9
import { StickyContainer, Sticky } from "react-sticky";
2  
xuzhenghua committed
10 11 12 13 14


function stopScroll(e) {
    e.preventDefault()
}
xuzhenghua committed
15

16 17 18
@connect(({user}) => ({
    user
}))
xuzhenghua committed
19
class Classify extends Component {
xuzhenghua committed
20 21
    constructor(props) {
        super(props)
xuzhenghua committed
22 23 24 25 26
        this.state = {
            ispull: false,
            display: 'none',
            arr: [{basics: []}, {advanced: []}],
            allClass: [],
xuzhenghua committed
27
            data: [],
xuzhenghua committed
28
            activeTab: decodeURIComponent(getParam('name')),
FE committed
29 30
            isLoading: true,
            top: 44
xuzhenghua committed
31 32
        }

xuzhenghua committed
33 34
    }

xuzhenghua committed
35 36
    componentDidMount() {
        this.getTabs()
xuzhenghua committed
37
        this.getList()
FE committed
38 39 40 41 42

        const el = document.querySelector('.search-nav');
        this.setState({
            top: el.offsetHeight
        });
xuzhenghua committed
43
    }
xuzhenghua committed
44

2  
xuzhenghua committed
45 46 47 48 49
    componentWillUnmount() {
        document.removeEventListener('touchmove', stopScroll)
    }


xuzhenghua committed
50 51 52
    // 获取tabs接口
    getTabs = () => {
        let data = 0
zhanghaozhe committed
53
        http.get(`${API.home}/m/course/classify/${data}`)
xuzhenghua committed
54 55 56 57 58 59 60 61 62 63 64 65
            .then((res) => {
                const _this = this
                if (res.data.code === 200) {
                    if (res.data.data.common.length > 0) {
                        let arr = ['basics', 'advanced']
                        let arr2 = [{basics: []}, {advanced: []}]
                        let arr3 = []
                        arr.forEach(function (item, index) {
                            arr2[item] = res.data.data.common[index]
                            res.data.data.common[index].list.forEach(function (item, index) {
                                arr3.push({'title': item.c_name, 'id': item.c_id})
                            })
xuzhenghua committed
66
                        })
xuzhenghua committed
67 68 69 70 71
                        _this.setState({
                            arr: arr2,
                            allClass: arr3
                        })
                    }
xuzhenghua committed
72 73
                } else {
                    Toast.info(res.data.msg, 2)
xuzhenghua committed
74
                }
xuzhenghua committed
75

xuzhenghua committed
76 77 78 79
            })
            .catch(err => {
                console.log(err)
            })
xuzhenghua committed
80
    }
xuzhenghua committed
81

xuzhenghua committed
82
    // 获取课程接口
xuzhenghua committed
83
    getList = () => {
xuzhenghua committed
84
        const _this = this
wangshuo committed
85 86 87
        _this.setState((state, props)=>({
            isLoading: true
        }));
zhanghaozhe committed
88
        http.get(`${API.home}/m/course/list/${getParam('id')}`).then((res) => {
xuzhenghua committed
89
            if (res.data.code === 200) {
xuzhenghua committed
90
                _this.setState({
xuzhenghua committed
91
                    data: res.data.data,
xuzhenghua committed
92 93
                    isLoading: false
                })
xuzhenghua committed
94 95 96 97
            }
        })
    }

xuzhenghua committed
98

xuzhenghua committed
99 100
    // 点击横向滚动tab查询
    ontabclick = (tab) => {
xuzhenghua committed
101 102 103 104 105
        this.props.history.push(`/courselist?id=${tab.id}&name=${tab.title}`)
        this.getList()
        this.setState({
            activeTab: decodeURIComponent(getParam('name'))
        });
xuzhenghua committed
106 107 108 109
    }

    // 上下展示
    pulldown = () => {
xuzhenghua committed
110 111
        this.setState(status => ({
            ispull: !status.ispull,
xuzhenghua committed
112
            display: status.ispull ? 'none' : 'block'
2  
xuzhenghua committed
113 114 115 116 117
        }), () => {
            this.state.ispull ? document.addEventListener('touchmove', stopScroll, {
                passive: false
            }) : document.removeEventListener('touchmove', stopScroll)
        });
xuzhenghua committed
118 119
    }

xuzhenghua committed
120
    // 弹窗里面tab点击查询
xuzhenghua committed
121 122 123
    labelclick = (item) => {
        this.props.history.push(`/courselist?id=${item.c_id}&name=${item.c_name}`)
        this.getList()
xuzhenghua committed
124 125
        this.setState(status => ({
            ispull: !status.ispull,
xuzhenghua committed
126 127
            display: status.ispull ? 'none' : 'block',
            activeTab: decodeURIComponent(getParam('name'))
xuzhenghua committed
128
        }))
xuzhenghua committed
129 130
    }

131
    toCourseDetail = (id) => {
2  
xuzhenghua committed
132
        const {dispatch, history} = this.props;
133
        // dispatch(getCourses(id, () => {
2  
xuzhenghua committed
134
        history.push(`/detail?id=${id}`)
135
        // }));
136 137
    }

2  
xuzhenghua committed
138 139
    toClassify = () => {
        this.props.history.replace('/classify');
xuzhenghua committed
140 141
    }

xuzhenghua committed
142
    render() {
2  
xuzhenghua committed
143 144
        const {user = {}} = this.props;
        let isLogin = user.data && user.data.uid ? true : false;
xuzhenghua committed
145 146 147 148 149 150
        const bottom = (
            <i className={'iconfont iconiconfront-69 pull-down'}></i>
        )
        const top = (
            <i className={'iconfont iconiconfront-71 pull-down'}></i>
        )
xuzhenghua committed
151

xuzhenghua committed
152
        let page = this.state.allClass.findIndex((item) => item.title === this.state.activeTab)
xuzhenghua committed
153
        return (
xuzhenghua committed
154
            <div className='class-child'>
2  
xuzhenghua committed
155
                <HeaderSearch
156 157 158
                    isLogin={isLogin}
                    toHref={this.toClassify}
                />
xuzhenghua committed
159 160
                <Loading isLoading={this.state.isLoading}>
                    <div className='class-content'>
FE committed
161
                        {/* <WhiteSpace/> */}
xuzhenghua committed
162 163 164
                        <div onClick={this.pulldown.bind(this)}>
                            {this.state.ispull ? top : bottom}
                        </div>
2  
xuzhenghua committed
165
                        <StickyContainer>
FE committed
166
                            {/* <Tabs
2  
xuzhenghua committed
167 168 169 170 171 172 173
                                tabs={this.state.allClass}
                                animated={false}
                                page={page}
                                onChange={(tab) => this.ontabclick(tab)}
                                renderTabBar={props => <div className={'custom-render-bar'}>
                                    <Tabs.DefaultTabBar {...props}/>
                                </div>}
FE committed
174 175 176 177 178 179 180 181 182 183 184
                            > */}
                            <Tabs
                                tabs={this.state.allClass}
                                animated={false}
                                page={page}
                                onChange={(tab) => this.ontabclick(tab)}
                                renderTabBar={props => {
                                    return (
                                        <Sticky>
                                            {({ style }) => {
                                                return (
FE committed
185
                                                    <div style={{ ...style, top: `${this.state.top}px`, zIndex: 1 }}>
FE committed
186 187 188 189 190 191 192
                                                        <Tabs.DefaultTabBar {...props} />
                                                    </div>
                                                )
                                            }}
                                        </Sticky>
                                    )
                                }}
2  
xuzhenghua committed
193 194 195 196 197 198 199 200
                            >
                                <div className='tabs'>
                                    <ul>
                                        {this.state.data && this.state.data.length > 0 && this.state.data.map((item, index) => {
                                            const Info = (
                                                <div className="info">
                                                    <p className='title'
                                                       onClick={() => this.toCourseDetail(item.course_id)}>
xuzhenghua committed
201 202
                                                        {item.course_title}
                                                    </p>
2  
xuzhenghua committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                                                    <p className='contact text-overflow-2'>{item.desc}</p>
                                                    <div className='des'>
                                                        {!item.is_buy && <p className="course-price">
                                                            <span className="new">¥{item.price1}</span>
                                                            <span className="old">¥{item.price0}</span>
                                                        </p>
                                                        }
                                                        {item.is_buy &&
                                                        <span className="isbuy">已购买</span>
                                                        }
                                                    </div>
                                                </div>
                                            )
                                            const status = (
                                                !item.is_buy &&
                                                <div>
                                                    {item.bargain_num === 0 && item.groupon_num !== 0 &&
                                                    <p className='course-status'>拼团减{item.groupon_num}</p>
xuzhenghua committed
221
                                                    }
2  
xuzhenghua committed
222 223 224 225 226
                                                    {item.bargain_num !== 0 && item.groupon_num === 0 &&
                                                    <p className='course-status'>砍价减{item.bargain_num}</p>
                                                    }
                                                    {
                                                        item.is_aist && <span className='return_cash'></span>
xuzhenghua committed
227 228
                                                    }
                                                </div>
2  
xuzhenghua committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
                                            )
                                            return (
                                                <VList
                                                    key={index}
                                                    status={status}
                                                    img={item.image_name}
                                                    id={item.course_id}
                                                    info={Info}
                                                    toDetail={this.toCourseDetail}
                                                />
                                            )
                                        })}
                                    </ul>
                                </div>
                            </Tabs>

                        </StickyContainer>
xuzhenghua committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259
                        <WhiteSpace/>
                    </div>
                    <div className='mbc-box' style={{display: this.state.display}}>
                        {
                            this.state.arr.basics &&
                            <div className="tabcontent">
                                <ClassCourse activeTab={this.state.activeTab} data={this.state.arr.basics.list}
                                             title={this.state.arr.basics.name} labelclick={this.labelclick}/>
                                <ClassCourse activeTab={this.state.activeTab} data={this.state.arr.advanced.list}
                                             title={this.state.arr.advanced.name} labelclick={this.labelclick}/>
                            </div>
                        }
                    </div>
                </Loading>
xuzhenghua committed
260 261 262 263 264 265
            </div>
        )
    }

}

xuzhenghua committed
266
function ClassCourse(props) {
xuzhenghua committed
267 268
    return (
        <div className="class-course">
xuzhenghua committed
269
            <p className='course-items-title'>{props.title}</p>
xuzhenghua committed
270 271
            <div className='items-box'>
                {
xuzhenghua committed
272
                    props.data && props.data.length > 0 && props.data.map((item, index) => {
xuzhenghua committed
273
                        return (
xuzhenghua committed
274 275
                            <span className={props.activeTab === item.c_name ? 'active-label' : 'item-label'}
                                  key={index} onClick={e => props.labelclick(item)}>{item.c_name}</span>
xuzhenghua committed
276 277 278 279 280 281 282 283
                        )
                    })
                }
            </div>
        </div>
    )
}

xuzhenghua committed
284
export default Classify;