index.js 3.13 KB
Newer Older
FE committed
1 2
import React, {Component} from 'react'
import {http, getParam, SendMessageToApp} from '@/utils'
xuzhenghua committed
3 4
import PythonDes from './pythomDes'
import PythonStudy from './pythonStudy'
FE 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"
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 22 23
    constructor(props) {
        super(props)
        this.state = {
            isPay: '',
            userInfoList: [],
            isAppUpdate: false
        }
xuzhenghua committed
24 25
    }

FE committed
26 27 28 29 30 31 32 33
    componentDidMount() {
        const _this = this
        this.fetchCourseInfo()
        // 获取App登录信息
        window['loginInfo'] = result => {
            _this.loginInfo(result)
        }
    }
xuzhenghua committed
34

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

    }

    // 保存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
            })
xuzhenghua committed
63
        }
FE committed
64 65 66 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

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

    render() {
        const {isPay, isAppUpdate} = this.state
        return (
            <div>
                {
                    isPay === 0 && <PythonDes history={this.props.history} isAppUpdate={isAppUpdate}></PythonDes>
                }
                {
                    (isPay === 1 && !getParam('version')) && <PythonStudy isAppUpdate={isAppUpdate}/>
                }
            </div>
        )
    }
xuzhenghua committed
112 113 114
}

export default Python