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

xuzhenghua committed
10 11 12 13 14 15

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

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

xuzhenghua committed
37 38
    // 获取app登录数据
    loginInfo = (result) => {
FE committed
39
        this.setState({
xuzhenghua committed
40 41 42 43 44 45
            userInfoList: result
        }, () => {
            if (this.state.userInfoList.length) {
                this.props.startFetchUser()
                this.appLogin()
            }
FE committed
46
        })
xuzhenghua committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    }

    // 保存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'})
        })

        if (cookie.get("token") && cookie.get("uid")) {
            this.setState({
                isAppUpdate: true
            })
            this.fetchCourseInfo();
FE committed
66
        }
xuzhenghua committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 139 140 141 142 143 144 145 146 147 148 149 150

        this.props.setCurrentUser(this.transformUser(this.state.userInfoList));
    }

    transformUser = res => {
        let payload

        res.map((item, index) => {
            payload = {
                hasError: false,
                data: {
                    username: item.uname,
                    avatar: item.avatar_file,
                    token: item.token,
                    uid: item.uid
                },
                isFetching: false
            }
        })

        return payload
    }


    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;
                        }
                        this.setState({
                            backwardVersion: false,
                            isPay: data.course_info.is_pay
                        })
                    }
                } else {

                    this.setState({
                        backwardVersion: false,
                        isPay: data.course_info.is_pay
                    })
                }
            }
        })
    }

    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>
        )
    }
FE committed
151 152 153
}

export default Python