index.js 34.6 KB
Newer Older
FE committed
1
import React, { PureComponent } from 'react';
zhanghaozhe committed
2
import { CopyToClipboard } from 'react-copy-to-clipboard';
FE committed
3 4
import classnames from 'classnames';
import { isEmpty } from 'lodash';
zhanghaozhe committed
5 6
import { browser } from 'src/utils';
import { http, getParam, validateTel } from "src/utils";
FE committed
7
import { Formik, Form, Field } from 'formik';
FE committed
8
import { Toast } from 'antd-mobile';
zhanghaozhe committed
9
import CaptchaAli from "src/common/Captcha-ali"
FE committed
10
import FollowQRcode from './../followQRcode';
FE committed
11
import './index.scss';
zhanghaozhe committed
12
import cookie from 'js-cookie'
zhanghaozhe committed
13 14
import { Link } from "react-router-dom";

FE committed
15 16 17 18 19

class RedPacket extends PureComponent {

  constructor(props) {
    super(props),
zhanghaozhe committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      this.state = {

        // 弹窗类型:0: 无弹出,1:非微信,2:微信
        type: 0,
        isCopy: false,
        validate: '',
        captchaInstance: null,
        seconds: 60,
        isFirst: true,
        timer: null,
        isTimer: false, // 是否开始倒计时
        shareInfo: {
          url: '',
          share_code: '',
          command: '【七月在线送你一个红包】https://www.julyedu.com/'
        },
        doneInfo: { //领取后的状态,1:领取成功,2:领取超时
          status: 2,
          txt: '手机号绑定超时,红包已失效!',
          desc: ''
        },
        money: '', // 红包金额,
        endTime: 10, // 手机绑定时限
        countdownTimer: null,
        countdown: '00分00秒', // 绑定时间
        accountInfo: {},
        bindInfo: {},
        country: {
          num: '86'
        },
        validationData: null,
51
      }
FE committed
52 53 54
  }

  componentDidMount() {
55
    // console.log(this.props);
56 57 58 59
    this.judgePopupTypeFromCountry();

    // 分享链接进入
    this.judgePopupType();
zhanghaozhe committed
60 61

    window.addEventListener('beforeunload', this.removeStatisticsCookie)
zhanghaozhe committed
62

zhanghaozhe committed
63

64 65
    // 微信进入
    const isWechat = getParam('wechat');
zhanghaozhe committed
66 67
    const {userInfo} = this.props;
    if (!userInfo || !userInfo.uid) {
zhanghaozhe committed
68 69
      this.setRelativeCookie()
    }
zhanghaozhe committed
70 71
    if (isWechat === '1' && !browser.isWeixin) {
      if (userInfo && userInfo.uid) {
72
        const type = window.localStorage.getItem('redpacket-click');
zhanghaozhe committed
73
        if (type === 'split') {
74
          http.get(`${API.home}/sys/redPacket/shareUrl/${getParam('id')}`).then(res => {
zhanghaozhe committed
75 76
            const {code, data} = res.data;
            if (code === 200) {
zhanghaozhe committed
77

78 79 80 81 82 83
              // 检查收否领取过
              http.post(
                `${API.home}/sys/redPacket/split`,
                {
                  action: 'check',
                  share_code: data.share_code
84
                }
85
              ).then(res => {
zhanghaozhe committed
86 87
                const {code, data} = res.data;
                if (code === 200) {
88
                  window.localStorage.removeItem('redpacket-click');
zhanghaozhe committed
89
                  // is_receive	是否领取过 0-否 1-是
zhanghaozhe committed
90
                  if (data.is_receive) {
91
                    this.judgeReceiveStatus(data, 8);
zhanghaozhe committed
92
                  } else {
93 94 95 96 97 98 99 100
                    this.setState({
                      type: 3
                    });
                  }
                }
              })
            }
          });
zhanghaozhe committed
101
        } else {
102 103 104 105 106
          this.setState({
            type: 1
          });
          window.localStorage.removeItem('redpacket-click');
        }
zhanghaozhe committed
107
      } else {
zhanghaozhe committed
108
        this.setRelativeCookie()
109 110 111 112
        this.setState({
          type: 1
        });
      }
113
    }
FE committed
114 115 116
    this.fetchShareInfo();
  }

zhanghaozhe committed
117 118 119 120 121 122 123 124 125
  removeStatisticsCookie = () => {
    cookie.remove('share_code', {path: '/', domain: '.julyedu.com'})
  }

  componentWillUnmount() {
    window.removeEventListener('beforeunload', this.removeStatisticsCookie)
  }


FE committed
126 127
  // 获取分享信息
  fetchShareInfo = () => {
FE committed
128
    const share_code = getParam('share_code');
FE committed
129
    http.get(`${API.home}/sys/redPacket/shareUrl/${getParam('id')}`).then(res => {
zhanghaozhe committed
130 131
      const {code, data} = res.data;
      if (code === 200) {
FE committed
132
        this.setState({
zhanghaozhe committed
133
          shareInfo: share_code ? Object.assign({}, data, {share_code}) : data
FE committed
134 135 136
        });
      }
    });
137 138 139 140 141
  }

  // 分享后进入,链接中带 share_code 字段
  judgePopupType = () => {
    const share_code = getParam('share_code');
zhanghaozhe committed
142 143
    const {shareInfo} = this.state;
    if (share_code && share_code !== 'null') {
FE committed
144
      this.setState({
145
        shareInfo: Object.assign({}, shareInfo, {
FE committed
146
          share_code
147
        })
FE committed
148
      });
149 150 151 152 153 154 155 156 157 158

      // 检查收否领取过
      http.post(
        `${API.home}/sys/redPacket/split`,
        {
          action: 'check',
          share_code
        }
      ).then(res => {
        const {code, data} = res.data;
zhanghaozhe committed
159
        if (code === 200) {
160 161

          // is_overdue	红包是否过期 0-否 1-是
zhanghaozhe committed
162
          if (data.is_overdue) {
163 164 165
            this.setState({
              type: 7
            });
zhanghaozhe committed
166
          } else {
167

xuzhenghua committed
168
            // is_receive	是否领取过 0-否 1-是
zhanghaozhe committed
169
            if (data.is_receive) {
170
              this.judgeReceiveStatus(data);
zhanghaozhe committed
171
            } else {
172
              this.setState({
zhanghaozhe committed
173
                type: browser.isWeixin ? 2 : 3
174 175 176 177
              });
            }
          }
        }
zhanghaozhe committed
178
        if (code === 4030 || code === 4040) {
179
          this.setState({
zhanghaozhe committed
180
            type: browser.isWeixin ? 2 : 3
181 182 183 184 185 186
          });
        }
      });
    }
  }

187 188
  // 选择区号后进入
  judgePopupTypeFromCountry = () => {
zhanghaozhe committed
189 190 191
    const {country} = this.props;
    const {shareInfo} = this.state;
    if (country.code) {
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      this.setState({
        country,
        shareInfo: Object.assign({}, shareInfo, {
          share_code: country.code
        })
      });

      // 检查收否领取过
      http.post(
        `${API.home}/sys/redPacket/split`,
        {
          action: 'check',
          share_code: country.code
        }
      ).then(res => {
        console.log(res);
zhanghaozhe committed
208 209
        const {code, data} = res.data;
        if (code === 200) {
210

xuzhenghua committed
211
          // is_receive	是否领取过 0-否 1-是
zhanghaozhe committed
212
          if (data.is_receive) {
213
            this.judgeReceiveStatus(data, 8);
zhanghaozhe committed
214
          } else {
215 216 217 218 219 220 221 222 223 224 225 226 227
            this.setState({
              type: 3
            });
          }

          // 清除
          this.props.delCountryNum();
        }
      })
    }
  }


228
  // 判断领取状态--领取后
zhanghaozhe committed
229
  judgeReceiveStatus = (data, bindType = 4) => {
230 231 232 233
    let txt = '';
    let desc = '';

    // receive_type 领取类型 1自己 2别人
zhanghaozhe committed
234
    if (data.receive_type === 1) {
235 236
      desc = '越多好友领取,你所得越多!';
    }
zhanghaozhe committed
237
    if (data.receive_type === 2) {
238 239
      desc = '每天只能帮好友领取一次哦~';
    }
240

FE committed
241
    // is_overdue	红包是否过期 0-否 1-是
zhanghaozhe committed
242 243 244 245
    if (data.is_overdue) {
      txt = data.red_packet_type === 1 ? `您已经领过该红包 ${data.amount}元现金!` : `您已经领过该红包 ${data.amount}元代金券碎片!`;
    } else {
      txt = data.red_packet_type === 1 ? `今日已领取${data.amount}元现金!` : `今日已领取${data.amount}元代金券碎片!`;
FE committed
246 247
    }

248
    // receive_status 领取状态 1-已领取 2-已领取未绑定 3-已失效
zhanghaozhe committed
249
    if (data.receive_status === 1) {
250 251 252 253 254 255 256 257
      this.setState({
        type: 9,
        doneInfo: {
          status: 1,
          txt,
          desc
        }
      });
zhanghaozhe committed
258
    } else if (data.receive_status === 2) {
259
      this.setState({
260
        type: bindType,
261 262 263 264
        money: data.amount,
        endTime: data.end_time
      });
      this.startCountDown();
zhanghaozhe committed
265
    } else if (data.receive_status === 3) {
266 267 268 269 270 271 272 273
      this.setState({
        type: 9,
        doneInfo: {
          status: 2,
          txt: '手机号绑定超时,红包已失效!',
          desc
        }
      });
FE committed
274 275 276 277 278
    }
  }

  // 绑定时间
  startCountDown = () => {
zhanghaozhe committed
279
    if (this.countdownTimer) {
FE committed
280 281
      window.clearInterval(this.countdownTimer);
    }
zhanghaozhe committed
282
    const {endTime} = this.state;
FE committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    let time = endTime;
    let minutes = 0;
    let seconds = 0;
    this.countdownTimer = window.setInterval(() => {
      if (time <= 0) {
        window.clearInterval(this.countdownTimer);
        this.setState({
          endTime: 0
        });
      }
      time -= 1;
      minutes = `${Math.floor(time / 60)}`.padStart(2, 0);
      seconds = `${(time % 60)}`.padStart(2, 0);
      this.setState({
        countdown: `${minutes}${seconds}秒`
      });
    }, 1000)
  }

FE committed
302
  // 获取分享信息
FE committed
303
  handleToShare = () => {
zhanghaozhe committed
304 305
    const {history, userInfo} = this.props;
    if (browser.isWeixin) {
FE committed
306 307 308 309
      this.setState({
        type: 2
      });
      history.push(`/detail?id=${getParam('id')}&wechat=1`);
zhanghaozhe committed
310 311
    } else {
      if (userInfo && userInfo.uid) {
312 313
        // 领取好友的后,再领取自己的 share_code 未更新
        http.get(`${API.home}/sys/redPacket/shareUrl/${getParam('id')}`).then(res => {
zhanghaozhe committed
314 315
          const {code, data} = res.data;
          if (code === 200) {
316 317 318 319 320
            this.setState({
              shareInfo: data,
              type: 1
            });
          }
FE committed
321
        });
zhanghaozhe committed
322
      } else {
FE committed
323
        history.push('/passport/login');
FE committed
324
      }
FE committed
325
    }
FE committed
326 327
  }

FE committed
328 329
  // 提示关注公众号
  openTip = () => {
zhanghaozhe committed
330
    const {shareInfo} = this.state;
FE committed
331 332

    // is_follow 是否关注公众号,0否,1是
zhanghaozhe committed
333
    if (!shareInfo.is_follow) {
FE committed
334 335 336 337 338 339
      this.setState({
        type: 11
      });
    }
  }

FE committed
340
  // 拆红包
FE committed
341
  handleToOpen = () => {
zhanghaozhe committed
342 343
    const {history} = this.props;
    const {shareInfo: {share_code = ''}} = this.state;
FE committed
344 345 346 347 348 349 350
    http.post(
      `${API.home}/sys/redPacket/split`,
      {
        action: 'receive',
        share_code
      }
    ).then(res => {
zhanghaozhe committed
351 352
      const {code, data} = res.data;
      if (code === 200) {
FE committed
353

354
        // is_receive	是否领取过 0-否 1-是
zhanghaozhe committed
355
        if (data.is_receive) {
356
          this.judgeReceiveStatus(data);
zhanghaozhe committed
357
        } else {
FE committed
358

359
          // red_packet_type 红包类型 1-现金 2-代金券
zhanghaozhe committed
360
          if (data.red_packet_type === 2) {
361 362 363 364
            this.setState({
              type: 6,
              money: data.amount
            });
zhanghaozhe committed
365
          } else if (data.red_packet_type === 1) {
FE committed
366 367

            // receive_status 领取状态 1-已领取 2-已领取未绑定 3-已失效
zhanghaozhe committed
368 369
            if (data.receive_status === 1) {
              if (data.red_packet_type === 1) {
370 371 372 373
                this.setState({
                  type: 5,
                  money: data.amount
                });
zhanghaozhe committed
374
              } else if (data.red_packet_type === 2) {
375 376 377 378
                this.setState({
                  type: 6,
                  money: data.amount
                });
FE committed
379
              }
zhanghaozhe committed
380
            } else if (data.receive_status === 2) {
FE committed
381 382 383 384 385 386
              this.setState({
                type: 4,
                money: data.amount,
                endTime: data.end_time
              });
              this.startCountDown();
zhanghaozhe committed
387
            } else if (data.receive_status === 3) {
FE committed
388 389 390 391
              this.setState({
                type: 9,
                doneInfo: {
                  status: 2,
392
                  txt: '手机号绑定超时,红包已失效!',
zhanghaozhe committed
393
                  desc: data.receive_type === 1 ? '越多好友领取,你所得越多!' : data.receive_type === 2 ? '每天只能帮好友领取一次哦~' : ''
FE committed
394 395 396 397
                }
              });
            }
          }
398
        }
zhanghaozhe committed
399
      } else if (code === 4030 || code === 4040) {
FE committed
400 401 402 403 404 405 406
        history.push('/passport/login');
      }
    })
  }

  // 关闭弹出
  handleToClose = (isOpen, isShare = false) => {
zhanghaozhe committed
407 408
    if (isOpen) {
      const {shareInfo: {share_code = ''}} = this.state;
FE committed
409 410 411 412 413 414 415 416 417

      // 检查收否领取过
      http.post(
        `${API.home}/sys/redPacket/split`,
        {
          action: 'check',
          share_code
        }
      ).then(res => {
zhanghaozhe committed
418 419
        const {code, data} = res.data;
        if (code === 200) {
FE committed
420 421

          // is_receive	是否领取过 0-否 1-是
zhanghaozhe committed
422
          if (data.is_receive) {
423
            this.judgeReceiveStatus(data);
zhanghaozhe committed
424
          } else {
425 426 427
            this.setState({
              type: 3
            });
FE committed
428 429 430
          }
        }
      });
zhanghaozhe committed
431 432 433
    } else {
      if (isShare) {
        const {history} = this.props;
FE committed
434 435 436 437 438 439 440 441
        history.push(`/detail?id=${getParam('id')}`);
      }
      this.setState({
        type: 0
      });
    }
  }

442 443
  // 微信内点击蒙层
  clickMask = () => {
zhanghaozhe committed
444 445
    const {type} = this.state;
    if (type === 2) {
446 447 448 449 450 451
      this.setState({
        type: 0
      });
    }
  }

FE committed
452 453 454 455 456 457
  getCaptchaInstance = instance => {
    this.setState({
      captchaInstance: instance
    });
  }

zhanghaozhe committed
458 459 460 461 462
  onVerify = (data) => {
    this.setState({
      validate: true,
      validationData: data
    })
FE committed
463 464 465
  }

  handleToSend = ({tel, code}) => {
zhanghaozhe committed
466 467
    let {validate, seconds, validationData, isFirst, isTimer, captchaInstance, country: {num = '86'}} = this.state;
    if (validate) {
FE committed
468 469
      if (!isFirst) {
        Toast.info('请重新进行滑块验证', 2, null, false);
zhanghaozhe committed
470
        captchaInstance.reset();
FE committed
471
        this.setState({
zhanghaozhe committed
472
          isFirst: true
FE committed
473 474 475
        });
        return
      }
zhanghaozhe committed
476
      if (!isTimer) {
FE committed
477 478
        if (!tel) {
          Toast.info('手机号码不能为空', 2, null, false);
zhanghaozhe committed
479 480
        } else if (!/^\d+$/.test(tel)) {
          // }else if (!validateTel(tel)) {
FE committed
481
          Toast.info('请输入正确格式的手机号码', 2, null, false);
zhanghaozhe committed
482
        } else {
FE committed
483 484 485 486 487

          // 获取验证码
          http.post(
            `${API['passport-api']}/m/personal/bindPhoneSendCode`,
            {
FE committed
488
              area_code: `00${num}`,
zhanghaozhe committed
489 490
              phone_num: tel,
              ...validationData
FE committed
491 492
            }
          ).then(res => {
zhanghaozhe committed
493 494
            const {errno, msg} = res.data;
            if (errno === 200) {
FE committed
495 496 497 498 499 500 501 502 503 504
              Toast.info('验证码发送成功', 2, null, false);

              // 倒计时
              this.timer = window.setInterval(() => {
                if (seconds <= 0) {
                  window.clearInterval(this.timer);
                  this.setState({
                    isTimer: false,
                    seconds: 60
                  });
zhanghaozhe committed
505
                } else {
FE committed
506 507 508 509 510 511 512 513 514 515 516
                  this.setState({
                    isTimer: true,
                    seconds: --seconds
                  });
                }
              }, 1000);

              // 滑块
              this.setState({
                isFirst: false
              })
zhanghaozhe committed
517
            } else {
FE committed
518 519 520 521 522 523 524 525 526 527 528 529
              Toast.info(msg, 2, null, false);
            }
          })
        }
      }
    }
    return false;
  }

  // 绑定后领取
  receviceAfterBind = () => {
    http.get(`${API.home}/sys/red_packet/receive`).then(res => {
zhanghaozhe committed
530 531
      const {code, data, msg} = res.data;
      if (code === 200) {
FE committed
532
        // receive_status 领取状态 1-已领取 2-已领取未绑定 3-已失效
zhanghaozhe committed
533 534
        if (data.receive_status === 1) {
          if (data.red_packet_type === 1) {
FE committed
535 536 537 538
            this.setState({
              type: 5,
              money: data.amount
            });
zhanghaozhe committed
539
          } else if (data.red_packet_type === 2) {
FE committed
540 541 542 543 544
            this.setState({
              type: 6,
              money: data.amount
            });
          }
zhanghaozhe committed
545
        } else if (data.receive_status === 2) {
FE committed
546 547 548 549 550 551
          this.setState({
            type: 4,
            money: data.amount,
            endTime: data.end_time
          });
          this.startCountDown();
zhanghaozhe committed
552
        } else if (data.receive_status === 3) {
FE committed
553 554 555 556
          this.setState({
            type: 9,
            doneInfo: {
              status: 2,
557
              txt: '手机号绑定超时,红包已失效!',
zhanghaozhe committed
558
              desc: data.receive_type === 1 ? '越多好友领取,你所得越多!' : data.receive_type === 2 ? '每天只能帮好友领取一次哦~' : ''
FE committed
559 560 561
            }
          });
        }
zhanghaozhe committed
562
      } else {
FE committed
563 564 565 566 567 568 569
        Toast.info(msg, 2, null, false);
      }
    })
  }

  // 绑定手机
  toContinueBind = (isValid = 1) => {
zhanghaozhe committed
570
    const {accountInfo: {tel, code}, country: {num = '86'}} = this.state;
FE committed
571 572
    // is_valid	是否验证 1:验证(默认),0不验证
    http.post(
xuzhenghua committed
573
      `${API['passport-api']}/m/personal/bindPhone`,
FE committed
574
      {
FE committed
575
        area_code: `00${num}`,
FE committed
576 577 578 579 580 581
        phone_num: tel,
        code: code,
        type: 1,
        is_valid: isValid
      }
    ).then(res => {
zhanghaozhe committed
582 583 584 585
      const {errno, data, msg} = res.data;
      if (errno === 200) {
        if (isValid) {
          if (data.tip_info) {
FE committed
586 587 588 589
            this.setState({
              type: 10,
              bindInfo: data.tip_info
            })
zhanghaozhe committed
590
          } else {
FE committed
591 592
            this.receviceAfterBind();
          }
zhanghaozhe committed
593
        } else {
FE committed
594 595
          this.receviceAfterBind();
        }
zhanghaozhe committed
596
      } else {
FE committed
597 598 599 600 601
        Toast.info(msg, 2, null, false);
      }
    });
  }

zhanghaozhe committed
602 603 604 605

  //设置统计信息
  setRelativeCookie = () => {
    const config = {path: '/', domain: '.julyedu.com'}
zhanghaozhe committed
606
    cookie.set('share_code', getParam('share_code') ? getParam('share_code') : 'share_code', config)
zhanghaozhe committed
607 608
  }

FE committed
609 610
  render() {
    // console.log(this.props);
zhanghaozhe committed
611
    const {history, userInfo} = this.props;
xuzhenghua committed
612 613 614 615 616 617
    const {
      type,
      isCopy,
      validate,
      isTimer,
      seconds,
FE committed
618
      shareInfo: {
619 620
        command = '',
        share_code = ''
xuzhenghua committed
621 622
      },
      money,
FE committed
623 624 625
      doneInfo,
      countdown,
      endTime,
626 627
      bindInfo,
      country
FE committed
628
    } = this.state;
zhanghaozhe committed
629
    const cls = classnames('popup-mask', {
630
      'popup-mask--no': type !== 2
FE committed
631 632 633 634 635 636 637 638 639 640 641
    });
    return (
      <>
        <div className="red-packet">
          <p className="red-packet__title">分享课程给好友,你和好友都可以领红包哦〜</p>
          <button className="red-packet__button" onClick={this.handleToShare}>分享领红包</button>
        </div>

        {/* popup */}
        {
          type !== 0 &&
642
          <div className={cls} onClick={this.clickMask}>
FE committed
643 644 645

            {/* wechat */}
            {
xuzhenghua committed
646
              type == 2 &&
FE committed
647 648
              <div className="pupup-wechat">
                <h4 className="popup-wechat__title">当前环境不支持领红包活动</h4>
FE committed
649
                <p className="popup-wechat__desc">请点击右上角“ ··· ”,选择在浏览器中打开 然后参与活动!</p>
FE committed
650 651 652 653 654 655 656 657 658 659 660 661
                <i className="popup-wechat__icon"></i>
              </div>
            }

            {/* wechat--no */}
            {
              type === 1 &&
              <div className="popup-password">
                <div className="popup-password__content">
                  <h4 className="popup-password__header">复制口令发送给好友</h4>
                  <div className="popup-password__body">
                    <p id="password" className="popup-passowrd__info">
zhanghaozhe committed
662
                      {command.length > 16 ? `${command.substr(0, 16)}...` : command}
FE committed
663 664
                    </p>
                    {!isCopy
FE committed
665
                      ? userInfo && userInfo.uid
zhanghaozhe committed
666 667 668 669 670 671 672 673 674 675 676
                        ? (
                          <CopyToClipboard
                            text={command}
                            onCopy={() => this.setState({
                              isCopy: true
                            })}
                          >
                            <button className="popup-password__button--copy">复制口令</button>
                          </CopyToClipboard>
                        )
                        : (
xuzhenghua committed
677 678
                          <button
                            className="popup-password__button--copy"
FE committed
679
                            onClick={() => {
680
                              window.localStorage.setItem('redpacket-click', 'copy');
FE committed
681 682 683 684
                              history.push('/passport/login');
                            }}
                          >复制口令</button>
                        )
FE committed
685 686 687 688
                      : <p className="popup-password__success">复制成功,快发送给好友吧~</p>
                    }
                  </div>
                  <p className="popup-password__footer">
zhanghaozhe committed
689
                    好友领取红包后,你将获得同样奖励。<br/>
FE committed
690 691 692
                    自己也可以领取哦~
                  </p>
                </div>
xuzhenghua committed
693 694
                <i
                  className="popup-password__button--close iconfont iconiconfront-2"
695
                  onClick={() => {
zhanghaozhe committed
696 697
                    if (getParam('wechat') === '1') {
                      if (userInfo && userInfo.uid) {
698 699
                        this.handleToClose(true);
                        history.push(`/detail?id=${getParam('id')}`);
zhanghaozhe committed
700
                      } else {
701 702 703
                        this.setState({
                          type: 3
                        });
704
                        window.localStorage.setItem('redpacket-click', 'split');
705
                      }
zhanghaozhe committed
706
                    } else {
707 708
                      this.handleToClose(true);
                    }
FE committed
709 710 711
                    this.setState({
                      isCopy: false
                    });
712
                  }}
FE committed
713 714 715 716
                />
              </div>
            }

xuzhenghua committed
717
            {/* red-packet--close */}
FE committed
718 719
            {
              type === 3 &&
xuzhenghua committed
720
              <Packet
FE committed
721 722 723
                type={3}
                packetInfo={{
                  title: '七月在线给你发了个红包~'
xuzhenghua committed
724

FE committed
725 726 727 728 729 730 731 732 733
                }}
                handleToClose={() => this.handleToClose(false)}
                handleToOpen={this.handleToOpen}
              />
            }

            {/* red-packet--open */}
            {
              type === 4 &&
xuzhenghua committed
734
              <Packet
FE committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
                type={4}
                packetInfo={{
                  money,
                  tip: ['为了您的资金安全,请于5分钟之内绑定手机号,超时红包将失效'],
                  btn: {
                    txt: `立即绑定(${countdown})`,
                    onClick: () => {
                      this.setState({
                        type: 8
                      });
                    }
                  }
                }}
                handleToClose={() => this.handleToClose(false)}
              />
            }

            {/* red-packet--money success */}
            {
              type === 5 &&
xuzhenghua committed
755
              <Packet
FE committed
756 757 758
                type={5}
                packetInfo={{
                  money,
zhanghaozhe committed
759
                  tip: ['可前往【七月在线】APP', '- 账户资金中提现'],
FE committed
760 761 762 763 764 765 766 767
                  btn: {
                    txt: '获取更多奖励',
                    onClick: () => {
                      history.push('/ShareCourse');
                    }
                  }
                }}
                handleToClose={() => {
zhanghaozhe committed
768
                  const isShare = getParam('share_code') ? true : false;
FE committed
769
                  this.handleToClose(false, isShare);
FE committed
770
                  this.openTip();
FE committed
771 772 773 774 775 776 777
                }}
              />
            }

            {/* red-packet--fragment success */}
            {
              type === 6 &&
xuzhenghua committed
778
              <Packet
FE committed
779 780 781
                type={6}
                packetInfo={{
                  money,
zhanghaozhe committed
782
                  tip: ['可前往【七月在线】APP', '- 我的优惠券中合成代金券'],
FE committed
783 784 785 786 787 788 789 790
                  btn: {
                    txt: '获取更多奖励',
                    onClick: () => {
                      history.push('/ShareCourse');
                    }
                  }
                }}
                handleToClose={() => {
zhanghaozhe committed
791
                  const isShare = getParam('share_code') ? true : false;
FE committed
792
                  this.handleToClose(false, isShare);
FE committed
793
                  this.openTip();
FE committed
794 795 796 797 798 799 800
                }}
              />
            }

            {/* red-packet--late 来完了 */}
            {
              type === 7 &&
xuzhenghua committed
801
              <Packet
FE committed
802 803 804 805 806 807 808 809 810 811
                type={7}
                packetInfo={{
                  btn: {
                    txt: '获取更多奖励',
                    onClick: () => {
                      history.push('/ShareCourse')
                    }
                  }
                }}
                handleToClose={() => {
zhanghaozhe committed
812
                  const isShare = getParam('share_code') ? true : false;
FE committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
                  this.handleToClose(false, isShare);
                }}
              />
            }

            {/* form */}
            {
              type === 8 &&
              <div className="popup-form">
                <Formik
                  initialValues={{
                    tel: '',
                    code: ''
                  }}
                  validate={({tel, code}) => {
                    const errors = {};
xuzhenghua committed
829

830
                    // if (!validateTel(tel)) {
zhanghaozhe committed
831
                    if (!/^\d+$/.test(tel)) {
FE committed
832 833 834 835 836
                      errors.tel = '请填写正确格式的手机号';
                    }
                    if (!/[0-9]{6}/.test(code)) {
                      errors.code = '请输入验证码';
                    }
xuzhenghua committed
837

FE committed
838 839
                    return errors;
                  }}
zhanghaozhe committed
840
                  onSubmit={(values, {setStatus, setSubmitting}) => {
FE committed
841 842 843 844 845 846 847 848 849 850 851
                    this.setState({
                      accountInfo: {
                        ...values
                      }
                    });
                    this.toContinueBind();
                  }}
                  render={({values: {tel, code}, errors}) => (
                    <Form className="popup-form__content">
                      <h4 className="popup-form__title">绑定手机号</h4>
                      <div className="popup-form__item">
xuzhenghua committed
852
                        <Link
xuzhenghua committed
853
                          className="popup-form__button--num"
xuzhenghua committed
854
                          to={`/country?id=${getParam('id')}&share_code=${share_code}`}>
855
                          +{country.num}
xuzhenghua committed
856 857
                          <i className="iconfont iconiconfront-69"/>
                        </Link>
FE committed
858 859 860 861
                        <Field
                          name="tel"
                          render={({field}) => {
                            return (
xuzhenghua committed
862
                              <input
FE committed
863
                                {...field}
xuzhenghua committed
864
                                className="popup-form__ipt"
FE committed
865 866 867 868 869 870 871 872 873
                                data-bdrs="0 6px 6px 0"
                                data-type="tel"
                                type="text"
                                placeholder="请填写手机号"
                              />
                            );
                          }}
                        />
                      </div>
zhanghaozhe committed
874
                      <CaptchaAli getInstance={this.getCaptchaInstance} onVerify={this.onVerify}/>
FE committed
875
                      {
zhanghaozhe committed
876
                        endTime === 0 ? (
FE committed
877 878 879 880 881 882 883
                          <>
                            <div className="popup-form__item">
                              <p className="popup-form__tip">
                                <i className="iconfont icondanseshixintubiao-8"></i>
                                绑定超时,红包已失效!
                              </p>
                            </div>
xuzhenghua committed
884 885
                            <button
                              className="popup-packet__button--bundle"
FE committed
886 887 888 889 890
                              data-status="do"
                              type="button"
                              onClick={() => {
                                history.push('/ShareCourse');
                              }}
zhanghaozhe committed
891 892
                            >获取更多奖励
                            </button>
FE committed
893 894 895 896 897 898 899 900
                          </>
                        ) : (
                          <>
                            <div className="popup-form__item">
                              <Field
                                name="code"
                                render={({field}) => {
                                  return (
xuzhenghua committed
901
                                    <input
FE committed
902
                                      {...field}
903
                                      className="popup-form__ipt popup-form__ipt--left"
FE committed
904 905 906 907 908 909
                                      type="text"
                                      placeholder="输入验证码"
                                    />
                                  );
                                }}
                              />
xuzhenghua committed
910
                              <button
FE committed
911
                                className="popup-form__button--code"
zhanghaozhe committed
912
                                data-status={(validate && !isTimer) ? 'do' : ''}
FE committed
913 914 915 916
                                type="button"
                                onClick={() => this.handleToSend({tel, code})}
                              >
                                {
zhanghaozhe committed
917
                                  isTimer ? `重新发送${seconds}s` : '发送验证码'
FE committed
918 919 920
                                }
                              </button>
                            </div>
xuzhenghua committed
921 922
                            <button
                              className="popup-packet__button--bundle"
zhanghaozhe committed
923
                              data-status={(tel && code && isEmpty(errors)) ? 'do' : 'done'}
FE committed
924 925 926 927 928 929 930 931 932 933
                              type="submit"
                            >
                              {`完成绑定(${countdown})`}
                            </button>
                          </>
                        )
                      }
                    </Form>
                  )}
                />
xuzhenghua committed
934 935
                <i
                  className="popup-password__button--close iconfont iconiconfront-2"
FE committed
936 937 938 939 940 941 942 943 944 945
                  onClick={() => this.handleToClose(false)}
                />
              </div>
            }

            {/* 领取后的状态 */}
            {
              type === 9 &&
              <div className="popup-done">
                <div className="popup-done__content">
zhanghaozhe committed
946 947
                  <h4
                    className={classnames("popup-done__title", {'popup-done__title--overtime': doneInfo.status === 2})}>
FE committed
948 949 950 951 952 953
                    {
                      doneInfo.status === 2 &&
                      <i className="iconfont icondanseshixintubiao-8"></i>
                    }
                    {doneInfo.txt}
                  </h4>
954
                  <p className="popup-done__desc">{doneInfo.desc}</p>
xuzhenghua committed
955 956
                  <button
                    className="popup-packet__button--bundle"
FE committed
957 958 959 960 961
                    data-status="do"
                    type="submit"
                    onClick={() => {
                      history.push('/ShareCourse');
                    }}
zhanghaozhe committed
962 963
                  >获取更多奖励
                  </button>
FE committed
964
                </div>
xuzhenghua committed
965 966
                <i
                  className="popup-password__button--close iconfont iconiconfront-2"
FE committed
967
                  onClick={() => {
zhanghaozhe committed
968
                    const isShare = getParam('share_code') ? true : false;
FE committed
969 970 971 972 973 974 975 976
                    this.handleToClose(false, isShare);
                  }}
                />
              </div>
            }

            {/* 手机号绑定提示  */}
            {
xuzhenghua committed
977
              type === 10 &&
FE committed
978 979 980 981 982 983 984 985
              <div className="popup-bind">
                <div className="popup-bind__content">
                  <h4 className="popup-bind__title">绑定确认</h4>
                  <p className="popup-bind__desc">该手机号已绑定到以下账号,继续绑定将解除以下绑定状态</p>
                  <ul className="popup-bind__list">
                    {
                      bindInfo['email'] &&
                      <li className="popup-bind__account">
xuzhenghua committed
986

FE committed
987 988 989 990 991 992 993 994 995 996
                        {/* 邮箱 */}
                        <i className="iconfont iconduanxin"></i>
                        <p className="popup-bind__account--name">{bindInfo['email']}</p>
                      </li>
                    }
                    {
                      bindInfo['wechat_nickname'] &&
                      <li className="popup-bind__account">

                        {/* wechat */}
FE committed
997
                        <i className="icon-wachat"></i>
FE committed
998 999 1000 1001 1002 1003 1004
                        <p className="popup-bind__account--name">{bindInfo['wechat_nickname']}</p>
                      </li>
                    }
                    {
                      bindInfo['qq_nickname'] &&
                      <li className="popup-bind__account">

zhanghaozhe committed
1005 1006 1007 1008
                        {/* qq */}
                        <i className="icon-qq"></i>
                        <p className="popup-bind__account--name">{bindInfo['qq_nickname']}</p>
                      </li>
FE committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
                    }
                    {
                      bindInfo['sina_nickname'] &&
                      <li className="popup-bind__account">

                        {/* 微博 */}
                        <i className="icon-sina"></i>
                        <p className="popup-bind__account--name">{bindInfo['sina_nickname']}</p>
                      </li>
                    }
                  </ul>
                  <div className="popup-bind__button">
xuzhenghua committed
1021
                    <button
zhanghaozhe committed
1022 1023 1024
                      className="popup-bind__button--cancle"
                      onClick={() => this.handleToClose(false)}>取消
                    </button>
xuzhenghua committed
1025
                    <button
zhanghaozhe committed
1026 1027 1028
                      className="popup-bind__button--confirm"
                      onClick={() => this.toContinueBind(0)}>继续绑定
                    </button>
FE committed
1029 1030
                  </div>
                </div>
xuzhenghua committed
1031 1032
                <i
                  className="popup-password__button--close iconfont iconiconfront-2"
FE committed
1033 1034 1035 1036
                  onClick={() => this.handleToClose(false)}
                />
              </div>
            }
FE committed
1037 1038 1039 1040

            {/* 关注公众号 */}
            {
              type === 11 &&
zhanghaozhe committed
1041
              <FollowQRcode toClose={() => this.handleToClose(false)}/>
FE committed
1042
            }
FE committed
1043 1044 1045 1046 1047 1048 1049
          </div>
        }
      </>
    )
  }
}

zhanghaozhe committed
1050
const Packet = ({type, packetInfo, handleToClose, handleToOpen}) => {
FE committed
1051 1052 1053 1054 1055 1056 1057 1058 1059
  const cls_content = classnames(
    'popup-packet__content',
    {
      'popup-packet__content--open': type === 4,
      'popup-packet__content--money': type === 5,
      'popup-packet__content--fragment': type === 6,
      'popup-packet__content--late': type === 7,
    }
  );
zhanghaozhe committed
1060
  const {money, tip = [], btn} = packetInfo;
FE committed
1061 1062 1063 1064 1065 1066 1067
  return (
    <div className={classnames('popup-packet')}>
      <div className={cls_content}>
        {
          type === 3 &&
          <>
            <p className="popup-packet__title">七月在线给你发了个红包~</p>
zhanghaozhe committed
1068
            <button className="popup-packet__button--split" onClick={handleToOpen}/>
FE committed
1069 1070 1071
          </>
        }
        {
zhanghaozhe committed
1072
          (type === 4 || type === 5 || type === 6) &&
FE committed
1073 1074 1075 1076 1077 1078 1079 1080 1081
          <>
            <h4 className="popup-packet__label">恭喜您获得</h4>
            <p className="popup-packet__value">
              {money}
              <span className="popup-packet__value--unit"></span>
            </p>
            <p className="popup-packet__tip">
              {
                tip.map((item, index) => {
zhanghaozhe committed
1082
                  if (index !== (tip.lenght - 1)) {
FE committed
1083 1084 1085
                    return (
                      <>
                        {item}
zhanghaozhe committed
1086
                        <br/>
FE committed
1087 1088 1089 1090 1091 1092 1093
                      </>
                    )
                  }
                  return item;
                })
              }
            </p>
xuzhenghua committed
1094
            <button
FE committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
              className="popup-packet__button--bundle"
              onClick={btn.onClick}
            >
              {btn.txt}
            </button>
          </>
        }
        {
          type === 7 &&
          <>
            <h4 className="popup-packet__label">您来晚了!</h4>
            <p className="popup-packet__value">
zhanghaozhe committed
1107
              红包仅能当日领取,下次<br/>
FE committed
1108 1109
              早点来哦~
            </p>
xuzhenghua committed
1110
            <button
FE committed
1111 1112 1113 1114 1115 1116 1117 1118
              className="popup-packet__button--bundle"
              onClick={btn.onClick}
            >
              {btn.txt}
            </button>
          </>
        }
      </div>
xuzhenghua committed
1119 1120
      <i
        className="popup-password__button--close iconfont iconiconfront-2"
FE committed
1121 1122 1123 1124 1125 1126 1127
        onClick={handleToClose}
      />
    </div>
  );
}

export default RedPacket;