index.js 34.8 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
      this.state = {

        // 弹窗类型:0: 无弹出,1:非微信,2:微信
        type: 0,
        isCopy: false,
        validate: '',
        captchaInstance: null,
        seconds: 60,
        isFirst: true,
        timer: null,
        isTimer: false, // 是否开始倒计时
        shareInfo: {
          url: '',
          share_code: '',
zhanghaozhe committed
34
          command: '【七月在线送你一个红包】https://www.julyedu.com/',
zhanghaozhe committed
35 36 37 38
        },
        doneInfo: { //领取后的状态,1:领取成功,2:领取超时
          status: 2,
          txt: '手机号绑定超时,红包已失效!',
zhanghaozhe committed
39
          desc: '',
zhanghaozhe committed
40 41 42 43 44 45 46 47
        },
        money: '', // 红包金额,
        endTime: 10, // 手机绑定时限
        countdownTimer: null,
        countdown: '00分00秒', // 绑定时间
        accountInfo: {},
        bindInfo: {},
        country: {
zhanghaozhe committed
48
          num: '86',
zhanghaozhe committed
49 50
        },
        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
              // 检查收否领取过
              http.post(
                `${API.home}/sys/redPacket/split`,
                {
                  action: 'check',
zhanghaozhe committed
83 84
                  share_code: data.share_code,
                },
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
                    this.setState({
zhanghaozhe committed
94
                      type: 3,
95 96 97 98 99 100
                    });
                  }
                }
              })
            }
          });
zhanghaozhe committed
101
        } else {
102
          this.setState({
zhanghaozhe committed
103
            type: 1,
104 105 106
          });
          window.localStorage.removeItem('redpacket-click');
        }
zhanghaozhe committed
107
      } else {
zhanghaozhe committed
108
        this.setRelativeCookie()
109
        this.setState({
zhanghaozhe committed
110
          type: 1,
111 112
        });
      }
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, {
zhanghaozhe committed
146 147
          share_code,
        }),
FE committed
148
      });
149 150 151 152 153 154

      // 检查收否领取过
      http.post(
        `${API.home}/sys/redPacket/split`,
        {
          action: 'check',
zhanghaozhe committed
155 156
          share_code,
        },
157 158
      ).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
            this.setState({
zhanghaozhe committed
164
              type: 7,
165
            });
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
      this.setState({
        country,
        shareInfo: Object.assign({}, shareInfo, {
zhanghaozhe committed
195 196
          share_code: country.code,
        }),
197 198 199 200 201 202 203
      });

      // 检查收否领取过
      http.post(
        `${API.home}/sys/redPacket/split`,
        {
          action: 'check',
zhanghaozhe committed
204 205
          share_code: country.code,
        },
206 207
      ).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
            this.setState({
zhanghaozhe committed
216
              type: 3,
217 218 219 220 221 222 223 224 225 226 227
            });
          }

          // 清除
          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
      this.setState({
        type: 9,
        doneInfo: {
          status: 1,
          txt,
zhanghaozhe committed
255 256
          desc,
        },
257
      });
zhanghaozhe committed
258
    } else if (data.receive_status === 2) {
259
      this.setState({
260
        type: bindType,
261
        money: data.amount,
zhanghaozhe committed
262
        endTime: data.end_time,
263 264
      });
      this.startCountDown();
zhanghaozhe committed
265
    } else if (data.receive_status === 3) {
266 267 268 269 270
      this.setState({
        type: 9,
        doneInfo: {
          status: 2,
          txt: '手机号绑定超时,红包已失效!',
zhanghaozhe committed
271 272
          desc,
        },
273
      });
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
    let time = endTime;
    let minutes = 0;
    let seconds = 0;
    this.countdownTimer = window.setInterval(() => {
      if (time <= 0) {
        window.clearInterval(this.countdownTimer);
        this.setState({
zhanghaozhe committed
290
          endTime: 0,
FE committed
291 292 293 294 295 296
        });
      }
      time -= 1;
      minutes = `${Math.floor(time / 60)}`.padStart(2, 0);
      seconds = `${(time % 60)}`.padStart(2, 0);
      this.setState({
zhanghaozhe committed
297
        countdown: `${minutes}${seconds}秒`,
FE committed
298 299 300 301
      });
    }, 1000)
  }

FE committed
302
  // 获取分享信息
FE committed
303
  handleToShare = () => {
zhanghaozhe committed
304 305
    const {history, userInfo} = this.props;
    if (browser.isWeixin) {
FE committed
306
      this.setState({
zhanghaozhe committed
307
        type: 2,
FE committed
308 309
      });
      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
            this.setState({
              shareInfo: data,
zhanghaozhe committed
318
              type: 1,
319 320
            });
          }
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
      this.setState({
zhanghaozhe committed
335
        type: 11,
FE committed
336 337 338 339
      });
    }
  }

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
    http.post(
      `${API.home}/sys/redPacket/split`,
      {
        action: 'receive',
zhanghaozhe committed
348 349
        share_code,
      },
FE committed
350
    ).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
            this.setState({
              type: 6,
zhanghaozhe committed
363
              money: data.amount,
364
            });
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
                this.setState({
                  type: 5,
zhanghaozhe committed
372
                  money: data.amount,
373
                });
zhanghaozhe committed
374
              } else if (data.red_packet_type === 2) {
375 376
                this.setState({
                  type: 6,
zhanghaozhe committed
377
                  money: data.amount,
378
                });
FE committed
379
              }
zhanghaozhe committed
380
            } else if (data.receive_status === 2) {
FE committed
381 382 383
              this.setState({
                type: 4,
                money: data.amount,
zhanghaozhe committed
384
                endTime: data.end_time,
FE committed
385 386
              });
              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 394
                  desc: data.receive_type === 1 ? '越多好友领取,你所得越多!' : data.receive_type === 2 ? '每天只能帮好友领取一次哦~' : '',
                },
FE committed
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

      // 检查收否领取过
      http.post(
        `${API.home}/sys/redPacket/split`,
        {
          action: 'check',
zhanghaozhe committed
415 416
          share_code,
        },
FE committed
417
      ).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
            this.setState({
zhanghaozhe committed
426
              type: 3,
427
            });
FE committed
428 429 430
          }
        }
      });
zhanghaozhe committed
431 432 433
    } else {
      if (isShare) {
        const {history} = this.props;
FE committed
434 435 436
        history.push(`/detail?id=${getParam('id')}`);
      }
      this.setState({
zhanghaozhe committed
437
        type: 0,
FE committed
438 439 440 441
      });
    }
  }

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

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

zhanghaozhe committed
458 459 460
  onVerify = (data) => {
    this.setState({
      validate: true,
zhanghaozhe committed
461
      validationData: data,
zhanghaozhe committed
462
    })
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
              phone_num: tel,
zhanghaozhe committed
490 491
              ...validationData,
            },
FE committed
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
              Toast.info('验证码发送成功', 2, null, false);

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

              // 滑块
              this.setState({
zhanghaozhe committed
515
                isFirst: false,
FE committed
516
              })
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
            this.setState({
              type: 5,
zhanghaozhe committed
537
              money: data.amount,
FE committed
538
            });
zhanghaozhe committed
539
          } else if (data.red_packet_type === 2) {
FE committed
540 541
            this.setState({
              type: 6,
zhanghaozhe committed
542
              money: data.amount,
FE committed
543 544
            });
          }
zhanghaozhe committed
545
        } else if (data.receive_status === 2) {
FE committed
546 547 548
          this.setState({
            type: 4,
            money: data.amount,
zhanghaozhe committed
549
            endTime: data.end_time,
FE committed
550 551
          });
          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 559
              desc: data.receive_type === 1 ? '越多好友领取,你所得越多!' : data.receive_type === 2 ? '每天只能帮好友领取一次哦~' : '',
            },
FE committed
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
        phone_num: tel,
        code: code,
        type: 1,
zhanghaozhe committed
579 580
        is_valid: isValid,
      },
FE committed
581
    ).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
            this.setState({
              type: 10,
zhanghaozhe committed
588
              bindInfo: data.tip_info,
FE committed
589
            })
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
        command = '',
zhanghaozhe committed
620
        share_code = '',
xuzhenghua committed
621 622
      },
      money,
FE committed
623 624 625
      doneInfo,
      countdown,
      endTime,
626
      bindInfo,
zhanghaozhe committed
627
      country,
FE committed
628
    } = this.state;
zhanghaozhe committed
629
    const cls = classnames('popup-mask', {
zhanghaozhe committed
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
                        ? (
                          <CopyToClipboard
                            text={command}
                            onCopy={() => this.setState({
zhanghaozhe committed
670
                              isCopy: true,
zhanghaozhe committed
671 672 673 674 675 676
                            })}
                          >
                            <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
                        this.setState({
zhanghaozhe committed
702
                          type: 3,
703
                        });
704
                        window.localStorage.setItem('redpacket-click', 'split');
705
                      }
zhanghaozhe committed
706
                    } else {
707 708
                      this.handleToClose(true);
                    }
FE committed
709
                    this.setState({
zhanghaozhe committed
710
                      isCopy: false,
FE committed
711
                    });
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
                type={3}
                packetInfo={{
zhanghaozhe committed
723
                  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
                type={4}
                packetInfo={{
                  money,
                  tip: ['为了您的资金安全,请于5分钟之内绑定手机号,超时红包将失效'],
                  btn: {
                    txt: `立即绑定(${countdown})`,
                    onClick: () => {
                      this.setState({
zhanghaozhe committed
743
                        type: 8,
FE committed
744
                      });
zhanghaozhe committed
745 746
                    },
                  },
FE committed
747 748 749 750 751 752 753 754
                }}
                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
                  btn: {
                    txt: '获取更多奖励',
                    onClick: () => {
                      history.push('/ShareCourse');
zhanghaozhe committed
764 765
                    },
                  },
FE committed
766 767
                }}
                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
                  btn: {
                    txt: '获取更多奖励',
                    onClick: () => {
                      history.push('/ShareCourse');
zhanghaozhe committed
787 788
                    },
                  },
FE committed
789 790
                }}
                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
                type={7}
                packetInfo={{
                  btn: {
                    txt: '获取更多奖励',
                    onClick: () => {
                      history.push('/ShareCourse')
zhanghaozhe committed
808 809
                    },
                  },
FE committed
810 811
                }}
                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
                  this.handleToClose(false, isShare);
                }}
              />
            }

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

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

            {/* 手机号绑定提示  */}
            {
xuzhenghua committed
980
              type === 10 &&
FE committed
981 982 983 984 985 986 987 988
              <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
989

FE committed
990 991 992 993 994 995 996 997 998 999
                        {/* 邮箱 */}
                        <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
1000
                        <i className="icon-wachat"></i>
FE committed
1001 1002 1003 1004 1005 1006 1007
                        <p className="popup-bind__account--name">{bindInfo['wechat_nickname']}</p>
                      </li>
                    }
                    {
                      bindInfo['qq_nickname'] &&
                      <li className="popup-bind__account">

zhanghaozhe committed
1008 1009 1010 1011
                        {/* qq */}
                        <i className="icon-qq"></i>
                        <p className="popup-bind__account--name">{bindInfo['qq_nickname']}</p>
                      </li>
FE committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
                    }
                    {
                      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
1024
                    <button
zhanghaozhe committed
1025 1026 1027
                      className="popup-bind__button--cancle"
                      onClick={() => this.handleToClose(false)}>取消
                    </button>
xuzhenghua committed
1028
                    <button
zhanghaozhe committed
1029 1030 1031
                      className="popup-bind__button--confirm"
                      onClick={() => this.toContinueBind(0)}>继续绑定
                    </button>
FE committed
1032 1033
                  </div>
                </div>
xuzhenghua committed
1034 1035
                <i
                  className="popup-password__button--close iconfont iconiconfront-2"
FE committed
1036 1037 1038 1039
                  onClick={() => this.handleToClose(false)}
                />
              </div>
            }
FE committed
1040 1041 1042 1043

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

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

export default RedPacket;