index.js 9.23 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7 8
import React, {Component} from 'react'
import {http, getParam, browser, SendMessageToApp} from '@/utils'
import PythonDes from './pythomDes'
import PythonStudy from './pythonStudy'
import {connect} from "react-redux"
import {addDays} from "date-fns"
import cookie from "js-cookie"
import {setCurrentUser, startFetchUser} from "@/store/userAction"
xuzhenghua committed
9
import {Toast} from "antd-mobile"
xuzhenghua committed
10
import {WxLogin} from "@common/index"
zhanghaozhe committed
11 12 13 14 15 16 17


@connect(state => ({
        user: state.user
    }),
    {setCurrentUser, startFetchUser}
)
zhanghaozhe committed
18
class ML extends Component {
zhanghaozhe committed
19 20 21 22
    constructor(props) {
        super(props)
        this.state = {
            isPay: '',
xuzhenghua committed
23
            buyTry: '',
zhanghaozhe committed
24 25 26
            userInfoList: [],
            isAppUpdate: false,
            backwardVersion: false, // 默认是新版本
xuzhenghua committed
27
            isWxlogin: false
zhanghaozhe committed
28 29 30 31
        }
    }

    componentDidMount() {
zhanghaozhe committed
32
        document.title = '机器学习小课 [精讲8大经典算法,在线编程,无需安装环境,会 python 即可学习] - 七月在线'
zhanghaozhe committed
33 34 35 36 37 38
        const _this = this
        this.fetchCourseInfo()
        // 获取App登录信息
        window['loginInfo'] = result => {
            _this.loginInfo(result)
        }
xuzhenghua committed
39 40


xuzhenghua committed
41
        if (browser.isWeixin && getParam('oid')) {
xuzhenghua committed
42 43 44 45 46 47 48 49 50
            this.isweixinPay()
        }
        if (getParam('weixinpay')) {
            this.payCallback()
        }
    }

    // 微信内部支付回调
    isweixinPay = () => {
xuzhenghua committed
51 52
        let _this = this
        let weixin_code = getParam('code')
xuzhenghua committed
53 54 55 56 57 58
        if (weixin_code) {
            if (getParam('oid') === undefined) {
                return
            } else {
                http.get(`${API['base-api']}/pay/wxpay/pub_charge/oid/${getParam('oid')}/code/${weixin_code}`).then((res) => {
                    if (res.data.errno === 0) {
xuzhenghua committed
59
                        let data = res.data.data
xuzhenghua committed
60 61 62 63 64 65 66 67 68 69 70

                        function onBridgeReady() {
                            WeixinJSBridge.invoke(
                                'getBrandWCPayRequest', {
                                    appId: data.appId,                //公众号名称,由商户传入
                                    timeStamp: data.timeStamp,        //时间戳,自1970年以来的秒数
                                    nonceStr: data.nonceStr,          //随机串
                                    package: data.package,
                                    signType: data.signType,          //微信签名方式:
                                    paySign: data.paySign             //微信签名
                                },
xuzhenghua committed
71

xuzhenghua committed
72
                                function (res) {
xuzhenghua committed
73
                                    if (res.err_msg == "get_brand_wcpay_request:ok") {
xuzhenghua committed
74
                                        Toast.info('支付成功', 2)
xuzhenghua committed
75 76 77
                                        _this.intervalPayStatus = setInterval(function () {
                                            http.get(`${API['base-api']}/m/orderState/oid/${getParam('oid')}`).then(res => {
                                                if (res.data.errno === 401) {
xuzhenghua committed
78 79
                                                    clearInterval(_this.intervalPayStatus)
                                                    _this.intervalPayStatus = null
xuzhenghua committed
80
                                                    location.href = '/ml?id=' + getParam('id')
xuzhenghua committed
81 82 83 84 85 86 87 88 89 90 91 92
                                                }
                                            })
                                        }, 1000)
                                    }
                                }
                            )
                        }

                        if (typeof WeixinJSBridge == "undefined") {
                            if (document.addEventListener) {
                                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false)
                            } else if (document.attachEvent) {
xuzhenghua committed
93
                                document.attachEvent('WeixinJSBridgeReady', onBridgeReady)
xuzhenghua committed
94 95 96
                                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady)
                            }
                        } else {
xuzhenghua committed
97
                            onBridgeReady()
xuzhenghua committed
98 99 100 101 102 103 104 105
                        }
                    }
                })
            }
        }
    }
    // 支付完成之后获取状态
    payCallback = () => {
xuzhenghua committed
106
        const _this = this
xuzhenghua committed
107 108 109 110 111
        // 支付回调
        // 定时器轮训获取订单状态
        _this.intervalPayStatus = setInterval(function () {
            http.get(`${API['base-api']}/m/orderState/oid/${getParam('oid')}`).then(res => {
                if (res.data.errno === 401) {
xuzhenghua committed
112 113
                    clearInterval(_this.intervalPayStatus)
                    _this.intervalPayStatus = null
xuzhenghua committed
114
                    location.href = '/ml?id=' + getParam('id')
xuzhenghua committed
115 116 117
                }
            })
        }, 1000)
zhanghaozhe committed
118 119
    }

xuzhenghua committed
120 121 122
    isWxloginFun = (val) => {
        this.setState({isWxlogin: val})
    }
zhanghaozhe committed
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
    // 获取app登录数据
    loginInfo = (result) => {
        this.setState({
            userInfoList: result
        }, () => {
            if (this.state.userInfoList.length) {
                this.props.startFetchUser()
                this.appLogin()
            }
        })

    }

    // 保存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
151
            this.fetchCourseInfo()
zhanghaozhe committed
152 153
        }

xuzhenghua committed
154
        this.props.setCurrentUser(this.transformUser(this.state.userInfoList))
zhanghaozhe committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    }

    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')
xuzhenghua committed
183
                if (version) {
zhanghaozhe committed
184
                    version = version.replace(/\./g, '').slice(0, 3)
xuzhenghua committed
185
                    if (browser.isAndroidApp && version < 453) { // 安卓的低版本
zhanghaozhe committed
186 187 188 189
                        this.setState({
                            backwardVersion: true,
                            isPay: 0,
                        })
xuzhenghua committed
190
                    } else if (browser.isIOSApp && version < 380) { // ISO的低版本
zhanghaozhe committed
191 192 193 194
                        this.setState({
                            backwardVersion: true,
                            isPay: 0,
                        })
xuzhenghua committed
195 196 197 198
                    } else { // 安卓/IOS 的高版本
                        if (data.course_info.is_pay === 1) { // 在APP内未登录-去登陆-登录后还显示此页;如果是已购买的用户 就需要跳转到 APP已购买的原生页面
                            SendMessageToApp('toSyllabusChapter', id) // 跳转到APP的已购买详情页 id 是课程ID
                            return
zhanghaozhe committed
199 200 201 202 203 204 205 206 207
                        }
                        this.setState({
                            backwardVersion: false,
                            isPay: data.course_info.is_pay
                        })
                    }
                } else {
                    this.setState({
                        backwardVersion: false,
xuzhenghua committed
208 209
                        isPay: data.course_info.is_pay,
                        buyTry: data.course_info.buy_try
zhanghaozhe committed
210 211 212 213 214 215 216
                    })
                }
            }
        })
    }

    render() {
xuzhenghua committed
217
        const {isPay, buyTry, isAppUpdate, backwardVersion, isWxlogin} = this.state
zhanghaozhe committed
218 219 220
        // 旧版本 无论购买未购买 都跳转到 未购买的详情页; 如果是已购买就提示更新APP
        return (
            <div>
xuzhenghua committed
221

zhanghaozhe committed
222
                {
xuzhenghua committed
223
                    isPay === 0 && buyTry === 0 && (
xuzhenghua committed
224
                        <PythonDes
zhanghaozhe committed
225
                            backwardVersion={backwardVersion}
xuzhenghua committed
226
                            isWxloginFun={this.isWxloginFun.bind(this)}
xuzhenghua committed
227
                            history={this.props.history}
zhanghaozhe committed
228 229
                            isAppUpdate={isAppUpdate}
                            isPay={isPay}
xuzhenghua committed
230
                            buyTry={buyTry}
zhanghaozhe committed
231 232 233 234
                        />
                    )
                }
                {
xuzhenghua committed
235
                    ((isPay === 1 || buyTry === 1) && !getParam('version')) && <PythonStudy isAppUpdate={isAppUpdate} buyTry={buyTry}/>
xuzhenghua committed
236 237 238 239 240
                }

                {
                    isWxlogin &&
                    <WxLogin history={this.props.history}/>
zhanghaozhe committed
241
                }
xuzhenghua committed
242

zhanghaozhe committed
243 244 245 246 247
            </div>
        )
    }
}

zhanghaozhe committed
248
export default ML