index.js 3.72 KB
Newer Older
FE committed
1
import React, { Component } from 'react';
xuzhenghua committed
2
import classnames from 'classnames';
wangshuo committed
3
import { http, SendMessageToApp } from "@/utils";
FE committed
4
import './index.scss';
wangshuo committed
5
import { getParam } from '../../../utils';
FE committed
6 7 8 9 10

class CoursePopup extends Component {
  constructor(props) {
    super(props);
    this.state = {
xuzhenghua committed
11 12
      courseList: [],
      isshowAppTip:false
FE committed
13 14 15 16 17
    };
  }

  componentDidMount() {
    this.fetchCourseData();
xuzhenghua committed
18
    this.distinguishVersion();
FE committed
19
  }
xuzhenghua committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  distinguishVersion = () => {
      // plat_form 1ios 2android
      // version  1->3.704    2->4.5.1.20191105
    if(getParam('plat_form') == 1){
        let version = Number(getParam('version'))
        this.setState({
            isshowAppTip: version < 3.704 ? true : false
        });
    }
    if(getParam('plat_form') == 2){
        let version = getParam('version').replace(/\./g, '').slice(0, 3)
        this.setState({
         isshowAppTip: Number(version) < 451 ? true:false
        });
    }
  }

FE committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  fetchCourseData = () => {
    Promise.all([
      http.get(`${API.home}/sys/browse/blessing/courses`),
      http.get(`${API.home}/sys/user/blessing`)
    ]).then(res => {
      const { code, data } = res[0].data;
      const doneCourse = res[1].data.data.today_browsed_courses || [];
      if(code === 200) {
        this.setState({
          courseList: data.map(item => {
            if(doneCourse.some(id => id == item.course_id)) {
              return Object.assign({}, item, {
                blessing: 2
              });
            }
            return item;
          })
        });
      }
    });
  }

wangshuo committed
60 61 62
  toCourseDetail = (item) => {
    console.log(this.props);
    const {isLogin, history, toLogin} = this.props;
xuzhenghua committed
63
    // to={`/detail?id=${item.course_id}&ac=11`}
wangshuo committed
64 65 66 67
    if(isLogin) {
      if(!getParam('version')) {
        history.push(`/detail?id=${item.course_id}&ac=11`);
      }else{
xuzhenghua committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        if(this.state.isshowAppTip){
          SendMessageToApp("toCourse", item.course_id);
        } else {
          if(item.blessing) {
            let data = {
              courseId: item.course_id,
              type: 2
            }
            SendMessageToApp("toBlessingCourse", data);
          }else{
            let data = {
              courseId: item.course_id,
              type: 1 // 正常跳课程详情页type:0,积福气浏览课程详情页-没有浏览过type:1 已浏览过type:2
            }
            SendMessageToApp("toBlessingCourse", data);
xuzhenghua committed
83
          }
wangshuo committed
84 85 86 87 88 89 90
        }
      }
    }else{
      toLogin();
    }
  }

FE committed
91
  render() {
xuzhenghua committed
92
    const { courseList,isshowAppTip } = this.state;
FE committed
93 94 95 96 97 98 99 100
    const { handleToHide } = this.props;
    return (
      <div className="course-popup__container">
        <div className="course-popup">
          <h2 className="course-popup__title">指定课程</h2>
          <div className="course-popup__list">
            {
              courseList.map(item => (
wangshuo committed
101
                <span
xuzhenghua committed
102 103
                  className={classnames("course-popup__item", {
                    "course-popup__item--active": item.blessing
xuzhenghua committed
104
                  })}
FE committed
105
                  key={item.course_id}
wangshuo committed
106
                  onClick={()=>{this.toCourseDetail(item)}}
FE committed
107
                >
FE committed
108 109
                  <span className="course-popup__name">{item.course_title}</span>
                  {
xuzhenghua committed
110
                    item.blessing &&
FE committed
111 112
                    <span>+2</span>
                  }
wangshuo committed
113
                </span>
FE committed
114 115 116
              ))
            }
          </div>
xuzhenghua committed
117 118 119 120 121 122 123

          {
            isshowAppTip &&
            <div className="course-popup-apptip">
              当前版本浏览课程暂不能增加福气值,可以前往h5/pc端浏览课程增加福气值
            </div>
          }
FE committed
124 125 126 127 128 129 130 131
        </div>
        <i className="iconfont iconiconfront-2" onClick={handleToHide}></i>
      </div>
    );
  }
}

export default CoursePopup;