togroup.js 12.9 KB
Newer Older
xuzhenghua committed
1
import React, {Component} from 'react'
xuzhenghua committed
2
import './togroup.scss'
xuzhenghua committed
3 4 5 6 7
import {HeaderBar, VList} from '../../../common'
import {getParam, http, browser, wxShare} from "@/utils"
import {connect} from "react-redux"
import {Link} from "react-router-dom"
import {WingBlank, WhiteSpace, Flex, Toast, Modal} from 'antd-mobile'
xuzhenghua committed
8

9 10

@connect(state => ({user: state.user}))
xuzhenghua committed
11 12 13 14
class ToGroup extends Component {
    constructor(props) {
        super(props)
        this.state = {
15 16 17 18 19 20 21
            data: {},
            share: false,
            pdd_price: 0,
            groupon_member: [],
            person_num: 0, // 共多少人成团
            is_success: false, // 是否拼团成功
            number: 0, // 差几人成团
22 23
            isBuy: false,
            modal: false,
xuzhenghua committed
24
            pddOrderId: ''
25 26 27 28
        }
    }

    invitedFriends = () => {
xuzhenghua committed
29
        const {pdd_price, number, data} = this.state
30
        if (browser.isWeixin) {
xuzhenghua committed
31
            let share = this.state.share
xuzhenghua committed
32 33
            this.setState({
                share: !share,
xuzhenghua committed
34
            })
xuzhenghua committed
35
            wxShare({
36
                title: `【仅剩${number}个名额】我${pdd_price}元拼了《${data.course_title}》`,
xuzhenghua committed
37
                desc: data.course_title,
xuzhenghua committed
38
                link: location.href + '&is_originator=1',
xuzhenghua committed
39
                imgUrl: data.image_name,
xuzhenghua committed
40
            })
xuzhenghua committed
41

42
        } else {
xuzhenghua committed
43
            Toast.info('请在微信中使用分享功能!', 2)
xuzhenghua committed
44 45 46
        }
    }

47
    componentDidMount() {
48 49
        http.get(`${API['base-api']}/pdd/info/${getParam('id')}`).then(res => {
            if (res.data.errno !== 0) {
xuzhenghua committed
50 51
                Toast.info(res.data.msg, 2)
                return
wangshuo committed
52
            }
53 54 55 56 57 58 59 60
            this.setState({
                data: res.data.data.course_info,
                pdd_price: res.data.data.pdd_price,
                groupon_member: res.data.data.groupon_member,
                is_success: res.data.data.is_success,
                person_num: res.data.data.person_num,
                course_list: res.data.data.course_list,
                number: res.data.data.number,
xuzhenghua committed
61
                pddOrderId: res.data.data.pdd_order_id,
wangshuo committed
62
                countdown: '',
63
                isBuy: res.data.data.is_buy
xuzhenghua committed
64
            })
65

66
            if (res.data.data.is_success === 0) {
wangshuo committed
67
                let date = res.data.data.end_time * 1000,
xuzhenghua committed
68
                    day = 0,
69 70
                    hours = 0,
                    minutes = 0,
xuzhenghua committed
71
                    seconds = 0
wangshuo committed
72 73
                setInterval(() => {
                    date -= 1000
xuzhenghua committed
74 75 76 77 78
                    if (res.data.data.end_time > 86400) {
                        day = `${parseInt(date / (3600000 * 24))}`.padStart(2, 0)
                        hours = `${parseInt((date - day * 3600000 * 24) / 3600000)}`.padStart(2, 0)
                        minutes = `${parseInt((date - day * 3600000 * 24 - hours * 3600000) / 60000)}`.padStart(2, 0)
                        this.setState({
xuzhenghua committed
79
                            countdown: `${day}${hours}${minutes}分`
xuzhenghua committed
80 81 82 83 84 85 86 87 88
                        })
                    } else {
                        hours = `${parseInt(date / (60 * 60 * 1000))}`.padStart(2, 0)
                        minutes = `${parseInt((date - hours * 3600000) / 60000)}`.padStart(2, 0)
                        seconds = `${parseInt((date - hours * 3600000 - minutes * 60000) / 1000)}`.padStart(2, 0)
                        this.setState({
                            countdown: `${hours}:${minutes}:${seconds}`
                        })
                    }
wangshuo committed
89 90
                }, 1000)
            }
xuzhenghua committed
91
        })
92 93
    }

94
    toCourseDetail = (id) => {
xuzhenghua committed
95 96 97
        const {history} = this.props
        history.push(`/detail?id=${id}`, {href: '/classify'})
        return false
98 99
    }

100
    handleToGroup = () => {
xuzhenghua committed
101 102
        const {isBuy = false, data = {}} = this.state
        const {user, history, location} = this.props
103
        if (user.hasError) {
FE committed
104 105 106
            if (browser.isWeixin) {
                let redirectURI = window.location.href
                if (redirectURI.includes('code=') && redirectURI.includes('state=STATE')) {
xuzhenghua committed
107 108
                    let index = redirectURI.lastIndexOf('code=')
                    redirectURI = redirectURI.substr(0, index - 1)
FE committed
109 110 111 112 113
                }
                window.location.assign(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23dac6775ac82877&redirect_uri=${encodeURIComponent(redirectURI)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`)
            } else {
                history.push('/passport', {from: location})
            }
114 115 116 117 118
            return
        }


        if (!isBuy) {
119 120 121
            history.push(
                `/order?id=${data.course_id}`,
                {
xuzhenghua committed
122
                    group: 1,
xuzhenghua committed
123
                    pdd_order_id: this.state.pddOrderId
124 125
                }
            )
126
        } else {
127 128 129 130 131 132
            this.setState({
                modal: true
            })
        }
    }

xuzhenghua committed
133
    render() {
xuzhenghua committed
134
        const {data: {course_id, course_title, simpledescription, price0, price1}, pdd_price, is_success, person_num, groupon_member, course_list} = this.state
xuzhenghua committed
135 136
        const Info = (
            <div className="info">
137 138
                <p className='title' onClick={() => this.toCourseDetail(course_id)}>
                    {/* <Link to={`/detail?id=${course_id}`}> */}
139
                    {course_title}
140
                    {/* </Link> */}
xuzhenghua committed
141
                </p>
142
                <p className='contact text-overflow-2'>{simpledescription}</p>
xuzhenghua committed
143 144
                <div className='des'>
                    <p className="course-price">
145 146
                        <span className="new">¥{price1}</span>
                        <span className="old">¥{price0}</span>
xuzhenghua committed
147 148 149 150 151 152
                    </p>
                </div>
            </div>
        )
        return (
            <div className='to-group-box'>
FE committed
153 154
                <HeaderBar title='拼团' cart={false} toHref={'/'}></HeaderBar>
                <Modal
155 156 157 158 159 160 161 162 163
                    transparent
                    visible={this.state.modal}
                    title="提示"
                    footer={[
                        {
                            text: '确定',
                            onPress: () => {
                                this.setState({
                                    modal: false
xuzhenghua committed
164 165
                                })
                                this.props.history.push('/')
166
                            }
167
                        }
168
                    ]}
169 170 171
                >
                    <p>您已经购买过该课程~</p>
                </Modal>
172
                {
xuzhenghua committed
173 174 175 176
                    !is_success &&
                    <VList
                        img={this.state.data.image_name}
                        id={this.state.data.course_id}
177 178 179
                        info={Info}
                        toDetail={this.toCourseDetail}
                    />
180
                }
xuzhenghua committed
181

FE committed
182 183
                <GorupContent
                    data={this.state}
184 185
                    invitedFriends={this.invitedFriends}
                    handleToGroup={this.handleToGroup}
186
                    userInfo={this.props.user}
187
                />
xuzhenghua committed
188

189
                <WhiteSpace></WhiteSpace>
xuzhenghua committed
190 191 192
                <div className="group-course">
                    <div className="top-title">
                        <span>本周特惠</span>
193 194
                        <Link to={`/preferential`} className='more'>更多<i
                            className='iconfont iconiconfront-70'></i></Link>
xuzhenghua committed
195
                    </div>
196 197 198 199
                    {
                        (course_list && course_list.length > 0) && (course_list[0].course && course_list[0].course.length > 0) && course_list[0].course.slice(0, 4).map(item => {

                            let weekInfo = <div className="info">
200 201
                                <p className='title' onClick={() => this.toCourseDetail(item.course_id)}>
                                    {/* <Link to={`/detail?id=${item.course_id}`}> */}
202
                                    {item.course_title}
203
                                    {/* </Link> */}
204
                                </p>
205 206 207 208 209 210 211
                                <p className='contact text-overflow-2'>{item.simpledescription}</p>
                                <div className='des'>
                                    <p className="course-price">
                                        <span className="new">¥{item.price1}</span>
                                        <span className="old">¥{item.price0}</span>
                                    </p>
                                </div>
212
                            </div>
213
                            return (
xuzhenghua committed
214 215
                                <VList
                                    img={item.image_name}
xuzhenghua committed
216
                                    key={item.course_id}
xuzhenghua committed
217
                                    id={item.course_id}
218 219 220 221
                                    info={weekInfo}
                                    toDetail={this.toCourseDetail}
                                />
                            )
222 223 224
                        })
                    }

xuzhenghua committed
225 226 227 228 229 230 231
                </div>
            </div>
        )
    }
}

function GorupContent(props) {
xuzhenghua committed
232 233
    let tip, btn, dec, groupTip, shareTip, groupSuccessMbc
    const {pdd_price, data, groupon_member, is_success, person_num, share, number, countdown} = props.data
234
    const {userInfo} = props
xuzhenghua committed
235 236
    let ary = [], num = number

237
    while (num != 0) {
xuzhenghua committed
238 239
        ary.push(num)
        num--
xuzhenghua committed
240
    }
241 242 243 244 245 246
    groupTip = (<div className='group-user-list'>
        <Flex justify='center' className='imgList'>
            {
                groupon_member.map(item => {
                    return (
                        <div className='imgContainer' key={Math.random()}>
247
                            <img className='imgname' src={item.avatar} alt=''/>
248 249 250 251 252 253 254
                            {
                                item.user_type === 1 ? (
                                    <div className='leaderFlag'>
                                        <span>团长</span>
                                    </div>
                                ) : null
                            }
xuzhenghua committed
255
                        </div>)
256 257 258 259 260 261 262 263 264 265 266 267
                })
            }
            {
                ary.map(item => {
                    return (<div className='imgContainer' key={Math.random()}>
                        <div className='imgname'>
                            <i className='iconfont iconwode-xianxing no-body'></i>
                        </div>
                    </div>)
                })
            }
        </Flex>
xuzhenghua committed
268
    </div>)
269 270

    if (groupon_member && groupon_member.length > 0) {
xuzhenghua committed
271
        let flag = false
272 273
        !userInfo.hasError && groupon_member.map(item => {
            if (item.uid == userInfo.data.uid) {
xuzhenghua committed
274
                flag = true
xuzhenghua committed
275
                return
276 277
            }
        })
278 279
        if (flag) {
            tip = <p className='tip'>{`拼团省¥${data.price1 - pdd_price}元`}</p>
wangshuo committed
280
            btn = <span className='group-btn' onClick={props.invitedFriends}>邀请好友参团 {countdown} 后结束</span>
281 282
            dec = <p className='dec'>分享到3个群后,成团率高达98%</p>
        } else {
xuzhenghua committed
283
            if (getParam('is_originator') == 1) {
FE committed
284 285 286 287 288 289
                tip = <p className='tip'>{`拼团省¥${data.price1 - pdd_price}元`}</p>
                btn = <a href="javascript:;" className='group-btn' onClick={props.handleToGroup}>一键参团</a>
            } else {
                tip = <p className='tip'>{`拼团省¥${data.price1 - pdd_price}元`}</p>
                btn = <span className='group-btn' onClick={props.invitedFriends}>邀请好友参团 {countdown} 后结束</span>
                dec = <p className='dec'>分享到3个群后,成团率高达98%</p>
FE committed
290
            }
291 292 293 294
        }
    }

    if (is_success === 1) {
xuzhenghua committed
295
        tip = <p className='success'>拼团成功</p>
xuzhenghua committed
296
        btn = <Link to={`/detail?id=${data.course_id}`} className='tostudy'>去学习</Link>
xuzhenghua committed
297 298 299
    }
    return (
        <div className='gorup-content'>
300 301 302 303
            {
                share ? (
                    <div className='groupSuccessMbc' onClick={props.invitedFriends}>
                        <div className='tipContent'>
xuzhenghua committed
304

305 306 307 308 309 310 311 312 313
                            {`还差${number}人,分享到3个群,成团率高达98%`}
                        </div>
                        <div className='tipArrow'>
                            <i className='iconfont iconyindao'></i>
                        </div>
                    </div>
                ) : null
            }

xuzhenghua committed
314
            {tip}
315 316 317 318 319 320
            <WingBlank>
                {groupTip}
            </WingBlank>
            <WingBlank>
                {btn}
            </WingBlank>
xuzhenghua committed
321 322
            {dec}
            {
323
                !is_success &&
xuzhenghua committed
324 325 326 327
                <div className="group-dec">
                    <span>
                        <i className='iconfont iconiconfront-1'></i>
                        ·好友参团  ·人满成交  ·人不满退款
328 329
                        </span>
                    <span className='allNum'>{`累计${person_num}人成团`}</span>
xuzhenghua committed
330 331 332 333 334 335 336
                </div>
            }
        </div>
    )
}

export default ToGroup