togroup.js 12.2 KB
Newer Older
1
import React, { Component } from 'react'
xuzhenghua committed
2
import './togroup.scss'
3
import { HeaderBar, VList } from '../../../common'
xuzhenghua committed
4

5
import { getParam, http, browser} from "@/utils";
6 7 8
import { connect } from "react-redux"
import { Link } from "react-router-dom"
import { WingBlank, WhiteSpace, Flex, Toast } from 'antd-mobile'
9
import {getCourses} from './../../detail/actions';
xuzhenghua committed
10

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

    invitedFriends = () => {
        const {pdd_price, number, data} = this.state;
        if(browser.isWeixin) {
            let share = this.state.share;
            this.setState({
                share: !share,
            });
            let shareData = {
                title: `【仅剩${number}个名额】我${pdd_price}元拼了《${data.course_title}》`,
                desc: data.course_title,                               //  分享描述
37
                // 直接进入课程详情页,isIn: 1
xuzhenghua committed
38
                link: location.origin + `/detail?id=${data.course_id}`, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
39 40
                imgUrl: data.image_name, // 分享图标
            };
zhanghaozhe committed
41
            http.post(`${API['base-api']}/m/sale/signature`).then(res => {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
                const {nonce_str, signature, timestamp} = res.data;
                wx.config({
                    "debug": false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                    "appId": 'wx23dac6775ac82877', // 必填,公众号的唯一标识
                    "timestamp": timestamp, // 必填,生成签名的时间戳
                    "nonceStr": nonce_str, // 必填,生成签名的随机串
                    "signature": signature, // 必填,签名
                    jsApiList: [
                        'hideMenuItems',
                        'showMenuItems',
                        'updateAppMessageShareData',
                        'updateTimelineShareData',
                        'onMenuShareWeibo',
                        'onMenuShareAppMessage',
                    ], // 必填,需要使用的JS接口列表
                });
            });
            wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
                wx.hideAllNonBaseMenuItem();
                wx.showMenuItems({
                    menuList: ['menuItem:share:appMessage' , 'menuItem:share:timeline'] // 要显示的菜单项,所有menu项见附录3
                  });
xuzhenghua committed
64
                wx.updateAppMessageShareData({
65 66 67 68 69 70 71 72 73 74 75 76 77 78
                    ...shareData,
                    success: function () {
                      // 设置成功
                    }
                })
                wx.updateTimelineShareData({
                    ...shareData,
                    success: function () {
                      // 设置成功
                    }
                })
            });
        }else{
            Toast.info('请在微信中使用分享功能!', 2);
xuzhenghua committed
79 80 81
        }
    }

82
    componentDidMount() {
zhanghaozhe committed
83
        Promise.all([http.get(`${API['base-api']}/pdd/goupon_pay_page/${getParam('id')}`), http.get(`${API.home}/m/user_info`)]).then(resultList => {
84 85 86
            console.log(resultList);
            let res = resultList[0],
            userInfo = resultList[1];
wangshuo committed
87 88 89 90 91 92 93 94
            if(res.data.errno !== 0) {
                Toast.info(res.data.msg, 2);
                return;
            }
            if(userInfo.data.code !== 200) {
                Toast.info(res.data.msg, 2);
                return;
            }
95 96 97 98 99 100 101 102 103
            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,
                userInfo,
                number: res.data.data.number,
wangshuo committed
104
                countdown: '',
105 106
            });

wangshuo committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            if(res.data.data.is_success === 0) {
                let date = res.data.data.end_time * 1000,
                hours = 0,
                minutes = 0,
                seconds = 0;
                setInterval(() => {
                    date -= 1000
                    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}`
                    });
                }, 1000)
            }
122 123 124
        });
    }

125 126
    toCourseDetail = (id) => {
        const { dispatch, history } = this.props;
xuzhenghua committed
127
        // dispatch(getCourses(id, () => {
xuzhenghua committed
128
            history.push(`/detail?id=${id}`, {href: '/classify'});
129
            return false;
xuzhenghua committed
130
        // }));
131 132
    }

xuzhenghua committed
133
    render() {
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'>
xuzhenghua committed
153
                <HeaderBar title='拼团' cart={false} toHref={() => this.toCourseDetail(course_id)}></HeaderBar>
154
                {
xuzhenghua committed
155 156 157 158
                    !is_success &&
                    <VList
                        img={this.state.data.image_name}
                        id={this.state.data.course_id}
159 160 161
                        info={Info}
                        toDetail={this.toCourseDetail}
                    />
162
                }
xuzhenghua committed
163

164
                <GorupContent data={this.state} invitedFriends={this.invitedFriends} />
xuzhenghua committed
165

166
                <WhiteSpace></WhiteSpace>
xuzhenghua committed
167 168 169 170 171
                <div className="group-course">
                    <div className="top-title">
                        <span>本周特惠</span>
                        <Link to={`/preferential`} className='more'>更多<i className='iconfont iconiconfront-70'></i></Link>
                    </div>
172 173 174 175
                    {
                        (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">
176 177
                            <p className='title' onClick={() => this.toCourseDetail(item.course_id)}>
                                {/* <Link to={`/detail?id=${item.course_id}`}> */}
178
                                    {item.course_title}
179
                                {/* </Link> */}
180 181 182 183 184 185 186 187 188
                            </p>
                            <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>
                            </div>
189
                            return (
xuzhenghua committed
190 191 192 193
                                <VList
                                    img={item.image_name}
                                    key={item.uid}
                                    id={item.course_id}
194 195 196 197
                                    info={weekInfo}
                                    toDetail={this.toCourseDetail}
                                />
                            )
198 199 200
                        })
                    }

xuzhenghua committed
201 202 203 204 205 206 207
                </div>
            </div>
        )
    }
}

function GorupContent(props) {
208
    let tip, btn, dec, groupTip, shareTip, groupSuccessMbc;
wangshuo committed
209
    const { pdd_price, data, groupon_member, is_success, person_num, userInfo, share, number, countdown } = props.data;
xuzhenghua committed
210

211 212 213 214
    let ary=[],num = number;
    while(num != 0) {
        ary.push(num);
        num--;
xuzhenghua committed
215
    }
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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()}>
                            <img className='imgname' src={item.avatar} alt='' />
                            {
                                item.user_type === 1 ? (
                                    <div className='leaderFlag'>
                                        <span>团长</span>
                                    </div>
                                ) : null
                            }
                        </div>);
                })
            }
            {
                ary.map(item => {
                    return (<div className='imgContainer' key={Math.random()}>
                        <div className='imgname'>
                            <i className='iconfont iconwode-xianxing no-body'></i>
                        </div>
                    </div>)
                })
            }
        </Flex>
    </div>);

    if (groupon_member && groupon_member.length > 0) {
        let uid = '';
xuzhenghua committed
247
        let flag = false;
248
        groupon_member.map(item=>{
xuzhenghua committed
249 250 251
            if(item.uid == userInfo.data.data.uid) {
                // uid = `${item.uid}`;
                flag = true
252 253 254
            }
        })

xuzhenghua committed
255
        if(flag) {
xuzhenghua committed
256
            tip = <p className='tip'>{`拼团省¥${data.price1-pdd_price}元`}</p>
wangshuo committed
257
            btn = <span className='group-btn' onClick={props.invitedFriends}>邀请好友参团 {countdown} 后结束</span>
258 259
            dec = <p className='dec'>分享到3个群后,成团率高达98%</p>
        } else {
xuzhenghua committed
260
            tip = <p className='tip'>{`拼团省¥${data.price1-pdd_price}元`}</p>
261 262 263 264 265
            btn = <Link to={`/order?id=${data.course_id}`} className='group-btn'>一键参团</Link>
        }
    }

    if (is_success === 1) {
xuzhenghua committed
266
        tip = <p className='success'>拼团成功</p>
xuzhenghua committed
267
        btn = <Link to={`/detail?id=${data.course_id}`} className='tostudy'>去学习</Link>
xuzhenghua committed
268 269 270
    }
    return (
        <div className='gorup-content'>
271 272 273 274
            {
                share ? (
                    <div className='groupSuccessMbc' onClick={props.invitedFriends}>
                        <div className='tipContent'>
xuzhenghua committed
275

276 277 278 279 280 281 282 283 284
                            {`还差${number}人,分享到3个群,成团率高达98%`}
                        </div>
                        <div className='tipArrow'>
                            <i className='iconfont iconyindao'></i>
                        </div>
                    </div>
                ) : null
            }

xuzhenghua committed
285
            {tip}
286 287 288 289 290 291
            <WingBlank>
                {groupTip}
            </WingBlank>
            <WingBlank>
                {btn}
            </WingBlank>
xuzhenghua committed
292 293
            {dec}
            {
294
                !is_success &&
xuzhenghua committed
295 296 297 298
                <div className="group-dec">
                    <span>
                        <i className='iconfont iconiconfront-1'></i>
                        ·好友参团  ·人满成交  ·人不满退款
299 300
                        </span>
                    <span className='allNum'>{`累计${person_num}人成团`}</span>
xuzhenghua committed
301 302 303 304 305 306 307
                </div>
            }
        </div>
    )
}

export default ToGroup