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

FE committed
10 11 12 13 14 15

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

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

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

    }

    // 保存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
            })
FE committed
65
            this.fetchCourseInfo();
xuzhenghua committed
66
        }
FE committed
67

FE committed
68
        this.props.setCurrentUser(this.transformUser(this.state.userInfoList));
FE committed
69
    }
FE committed
70

FE committed
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
    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) {
wangshuo committed
96 97 98
                let version = getParam('version')
                if(version) {
                    version = version.replace(/\./g, '').slice(0, 3)
wangshuo committed
99
                    if(browser.isAndroidApp && version < 453) { // 安卓的低版本
wangshuo committed
100 101 102 103
                        this.setState({
                            backwardVersion: true,
                            isPay: 0,
                        })
FE committed
104
                    }else if(browser.isIOSApp && version < 380) { // ISO的低版本
wangshuo committed
105 106 107 108
                        this.setState({
                            backwardVersion: true,
                            isPay: 0,
                        })
109 110 111 112 113
                    }else{ // 安卓/IOS 的高版本
                        if(data.course_info.is_pay === 1) { // 在APP内未登录-去登陆-登录后还显示此页;如果是已购买的用户 就需要跳转到 APP已购买的原生页面
                            SendMessageToApp('toSyllabusChapter', id); // 跳转到APP的已购买详情页 id 是课程ID
                            return;
                        }
wangshuo committed
114
                        this.setState({
wangshuo committed
115
                            backwardVersion: false,
wangshuo committed
116 117 118
                            isPay: data.course_info.is_pay
                        })
                    }
wangshuo committed
119 120 121 122 123
                } else {
                    this.setState({
                        backwardVersion: false,
                        isPay: data.course_info.is_pay
                    })
wangshuo committed
124
                }
FE committed
125 126 127 128 129
            }
        })
    }

    render() {
wangshuo committed
130 131
        const {isPay, isAppUpdate, backwardVersion} = this.state;
        // 旧版本 无论购买未购买 都跳转到 未购买的详情页; 如果是已购买就提示更新APP
FE committed
132 133 134
        return (
            <div>
                {
FE committed
135
                    isPay === 0 && (
FE committed
136 137 138 139 140 141
                        <PythonDes 
                            backwardVersion={backwardVersion}
                            history={this.props.history} 
                            isAppUpdate={isAppUpdate}
                            isPay={isPay}
                        />
FE committed
142
                    )
FE committed
143 144 145 146 147 148 149
                }
                {
                    (isPay === 1 && !getParam('version')) && <PythonStudy isAppUpdate={isAppUpdate}/>
                }
            </div>
        )
    }
xuzhenghua committed
150 151 152
}

export default Python