index.js 4.43 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6
import React, { Component } from "react"
import { http, getParam, browser, SendMessageToApp } from "src/utils"
import PythonDes from "./pythomDes"
import PythonStudy from "./pythonStudy"
import { connect } from "react-redux"
import { addDays } from "date-fns"
FE committed
7
import cookie from "js-cookie"
zhanghaozhe committed
8
import { setCurrentUser, startFetchUser } from "src/store/userAction"
xuzhenghua committed
9

zhanghaozhe committed
10 11 12 13 14
@connect(
  (state) => ({
    user: state.user,
  }),
  { setCurrentUser, startFetchUser }
FE committed
15
)
xuzhenghua committed
16
class Python extends Component {
zhanghaozhe committed
17 18 19 20 21 22 23
  constructor(props) {
    super(props)
    this.state = {
      isPay: "",
      userInfoList: [],
      isAppUpdate: false,
      backwardVersion: false, // 默认是新版本
xuzhenghua committed
24
    }
zhanghaozhe committed
25
  }
xuzhenghua committed
26

zhanghaozhe committed
27 28 29 30 31 32 33 34
  componentDidMount() {
    document.title =
      "Python人工智能 [P1级,驰援武汉 本图文小课注册即送] - 七月在线"
    const _this = this
    this.fetchCourseInfo()
    // 获取App登录信息
    window["loginInfo"] = (result) => {
      _this.loginInfo(result)
FE committed
35
    }
zhanghaozhe committed
36
  }
xuzhenghua committed
37

zhanghaozhe committed
38 39 40 41 42 43 44 45 46 47
  // 获取app登录数据
  loginInfo = (result) => {
    this.setState(
      {
        userInfoList: result,
      },
      () => {
        if (this.state.userInfoList.length) {
          this.props.startFetchUser()
          this.appLogin()
xuzhenghua committed
48
        }
zhanghaozhe committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      }
    )
  }

  // 保存cookie
  appLogin = () => {
    let expires = addDays(new Date(), 90)
    this.state.userInfoList.map((item, index) => {
      cookie.set("token", item.token, {
        expires,
        path: "/",
        domain: ".julyedu.com",
      })
      cookie.set("plat", item.plat, {
        expires,
        path: "/",
        domain: ".julyedu.com",
      })
      cookie.set("uid", item.uid, {
        expires,
        path: "/",
        domain: ".julyedu.com",
      })
      cookie.set("uname", item.uname, {
        expires,
        path: "/",
        domain: ".julyedu.com",
      })
      cookie.set("avatar_file", item.avatar_file, {
        expires,
        path: "/",
        domain: ".julyedu.com",
      })
    })
FE committed
83

zhanghaozhe committed
84 85 86 87 88
    if (cookie.get("token") && cookie.get("uid")) {
      this.setState({
        isAppUpdate: true,
      })
      this.fetchCourseInfo()
FE committed
89
    }
FE committed
90

zhanghaozhe committed
91 92
    this.props.setCurrentUser(this.transformUser(this.state.userInfoList))
  }
FE committed
93

zhanghaozhe committed
94 95
  transformUser = (res) => {
    let payload
FE committed
96

zhanghaozhe committed
97 98 99 100 101 102 103 104 105 106 107 108
    res.map((item, index) => {
      payload = {
        hasError: false,
        data: {
          username: item.uname,
          avatar: item.avatar_file,
          token: item.token,
          uid: item.uid,
        },
        isFetching: false,
      }
    })
FE committed
109

zhanghaozhe committed
110 111
    return payload
  }
FE committed
112

zhanghaozhe committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  fetchCourseInfo = () => {
    const id = getParam("id")
    http.get(`${API.home}/m/course/detail/${id}`).then((res) => {
      const { data, code } = res.data
      if (code === 200) {
        let version = getParam("version")
        if (version) {
          version = version.replace(/\./g, "").slice(0, 3)
          if (browser.isAndroidApp && version < 453) {
            // 安卓的低版本
            this.setState({
              backwardVersion: true,
              isPay: 0,
            })
          } else if (browser.isIOSApp && version < 380) {
            // ISO的低版本
            this.setState({
              backwardVersion: true,
              isPay: 0,
            })
          } else {
            // 安卓/IOS 的高版本
            if (data.course_info.is_pay === 1) {
              // 在APP内未登录-去登陆-登录后还显示此页;如果是已购买的用户 就需要跳转到 APP已购买的原生页面
              SendMessageToApp("toSyllabusChapter", id) // 跳转到APP的已购买详情页 id 是课程ID
              return
FE committed
139
            }
zhanghaozhe committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153
            this.setState({
              backwardVersion: false,
              isPay: data.course_info.is_pay,
            })
          }
        } else {
          this.setState({
            backwardVersion: false,
            isPay: data.course_info.is_pay,
          })
        }
      }
    })
  }
FE committed
154

zhanghaozhe committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  render() {
    const { isPay, isAppUpdate, backwardVersion } = this.state
    // 旧版本 无论购买未购买 都跳转到 未购买的详情页; 如果是已购买就提示更新APP
    return (
      <div>
        {isPay === 0 && (
          <PythonDes
            backwardVersion={backwardVersion}
            history={this.props.history}
            isAppUpdate={isAppUpdate}
            isPay={isPay}
          />
        )}
        {isPay === 1 && !getParam("version") && (
          <PythonStudy isAppUpdate={isAppUpdate} />
        )}
      </div>
    )
  }
xuzhenghua committed
174 175 176
}

export default Python