App.js 8.1 KB
Newer Older
xuzhenghua committed
1
import React, { Component } from 'react'
zhanghaozhe committed
2 3
import Routes from './router'
import cookie from 'js-cookie'
zhanghaozhe committed
4 5
import { connect } from "react-redux"
import { setCurrentUser, startFetchUser } from "@/store/userAction"
xuzhenghua committed
6 7
import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
zhanghaozhe committed
8 9
import { getParam, http, browser } from "@/utils"
import { Toast } from "antd-mobile"
xuzhenghua committed
10
import { addDays } from 'date-fns'
11

zhanghaozhe committed
12 13

//拦截ajax请求,返回mock数据
zhanghaozhe committed
14 15
/*import mock from '@/utils/mock'
mock()*/
zhanghaozhe committed
16 17 18


// 默认样式
zhanghaozhe committed
19
import './assets/css/index.scss'
zhanghaozhe committed
20 21

// iconfont
zhanghaozhe committed
22
import './assets/font/iconfont.css'
zhanghaozhe committed
23

24 25
class App extends Component {

zhanghaozhe committed
26 27
    static displayName = 'App'

zhanghaozhe committed
28
    previousLocation = {pathname: '/', search: '', hash: ''}
29

FE committed
30 31
    records = []

zhanghaozhe committed
32 33 34
    pathnameBlacklist = ['/country', '/passport']


FE committed
35 36
    firstLoad = true

37
    componentDidMount() {
zhanghaozhe committed
38
        this.umengStatistic()
39

FE committed
40

zhanghaozhe committed
41
        //平台信息
zhanghaozhe committed
42 43
        cookie.set('plat', '5', {domain: '.julyedu.com'})

zhanghaozhe committed
44
        if (browser.isWeixin && browser.isIOS) {
zhanghaozhe committed
45 46 47
            sessionStorage.setItem('enter_url', window.location.href)
        }

zhanghaozhe committed
48
        this.getUser()
xuzhenghua committed
49
        this.utm()
zhanghaozhe committed
50

zhanghaozhe committed
51
        const {history} = this.props
zhanghaozhe committed
52 53 54 55 56 57


        this.setNavigationRecord(this.props.location, this.props.history.action)
        this.setPreviousLocation()


FE committed
58 59 60
        history.listen((location, action) => {
            this.firstLoad = false
            this.setNavigationRecord(location, action)
xuzhenghua committed
61
            this.utm()
zhanghaozhe committed
62

63
            if (cookie.get('uid') && this.props.user.hasError) {
zhanghaozhe committed
64 65
                this.getUser()
            }
xuzhenghua committed
66 67 68
            if(location.pathname==='/passport'){
                window.localStorage.setItem('binding_redirect', JSON.stringify(this.previousLocation))
            }
zhanghaozhe committed
69
            const {pathname, state} = location
zhanghaozhe committed
70
            if (pathname.startsWith('/passport')) {
zhanghaozhe committed
71 72 73 74 75 76 77 78 79
                location.state = {
                    ...state,
                    ...{
                        from: {
                            pathname: this.previousLocation.pathname,
                            search: this.previousLocation.search,
                            hash: this.previousLocation.hash
                        }
                    }
zhanghaozhe committed
80
                }
zhanghaozhe committed
81 82
            } else {
                this.removeShareCodeCookie()
zhanghaozhe committed
83 84 85
            }
        })

86

87
    }
zhanghaozhe committed
88

zhanghaozhe committed
89 90 91 92 93 94 95 96
    umengStatistic = () => {
        // 友盟统计
        const script = document.createElement('script')
        script.src = 'https://s22.cnzz.com/z_stat.php?id=1265696973&web_id=1265696973'
        script.language = 'JavaScript'
        document.body.appendChild(script)
    }

zhanghaozhe committed
97 98 99 100
    removeShareCodeCookie = () => {
        cookie.remove('share_code', {path: '/', domain: '.julyedu.com'})
    }

FE committed
101 102
    setNavigationRecord = (location, action) => {
        const {pathname, search, hash} = location
zhanghaozhe committed
103
        let isLastRecord = location.pathname === (this.records.length && this.records[this.records.length - 1].pathname)
wangshuo committed
104

FE committed
105 106 107 108 109
        switch (action) {
            case 'POP':
                this.firstLoad ? this.records.push({pathname, search, hash}) : this.records.pop()
                break
            case 'REPLACE':
zhanghaozhe committed
110
                this.records.length > 1 && (this.records[this.records.length - 1] = {pathname, search, hash})
FE committed
111 112 113
                break
            default:
                !isLastRecord && this.records.push({pathname, search, hash})
114
        }
FE committed
115 116 117

        location.state && location.state.records
            ? (location.state.records = this.records)
zhanghaozhe committed
118
            : location.state ? location.state = {...location.state, records: this.records}
FE committed
119
            : (location.state = {records: this.records})
zhanghaozhe committed
120 121
    }

xuzhenghua committed
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

    utm = () => {
        // utm统计  m站全站统计广告投放、以及统计详情页浏览
        let utm_source = getParam('utm_source')
        let utm_medium = getParam('utm_medium')
        let utm_campaign = getParam('utm_campaign')
        let utm_content = getParam('utm_content')
        let utm_term = getParam('utm_term')
        let courseId = location.pathname.startsWith('/getDetail') || location.pathname.startsWith('/detail') ? getParam('id') : 0

        if (courseId || (utm_term && utm_source && utm_medium && utm_campaign && utm_content)) {
            let data = {
                'utm_source': utm_source,
                'utm_medium': utm_medium,
                'utm_campaign': utm_campaign,
                'utm_content': utm_content,
                'utm_term': utm_term,
            }

            http.post(`${API['www']}/base/first_utm_record/${courseId}`, data)
                .then(res => {
                })

        }
    }

zhanghaozhe committed
148
    getUser = () => {
zhanghaozhe committed
149
        //获取用户信息
zhanghaozhe committed
150 151 152 153
        this.props.startFetchUser()
        http.get(`${API.home}/m/user_info`).then(res => {
            this.props.setCurrentUser(this.transformUser(res))
        })
zhanghaozhe committed
154
        //微信
zhanghaozhe committed
155
        let code = getParam('code')
xuzhenghua committed
156
        let oid = getParam('oid')
zhanghaozhe committed
157

xuzhenghua committed
158
        if (code && !oid) {
zhanghaozhe committed
159

160
            http.get(`${API['home']}/m/wx_loginInfo/code/${code}?redirect=${encodeURIComponent(window.location.href)}`)
zhanghaozhe committed
161 162 163 164 165 166
                .then(res => {
                    let data = res.data
                    if (data.errno == 200) {
                        if (data.data['is_bind_mobile']) {
                            window.location.assign(data.data.url)
                        } else {
zhanghaozhe committed
167 168
                            let user = this.transformWxUser(res)
                            let {role, uid, token} = data.data
xuzhenghua committed
169 170 171 172
                            let expires = addDays(new Date(), 90)
                            cookie.set('role', role, {expires, domain: '.julyedu.com', path: '/'})
                            cookie.set('uid', uid, {expires, domain: '.julyedu.com', path: '/'})
                            cookie.set('token', token, {expires, domain: '.julyedu.com', path: '/'})
zhanghaozhe committed
173
                            this.props.setCurrentUser(user)
zhanghaozhe committed
174 175 176 177 178 179 180 181
                        }
                    } else {
                        Toast.info(data.msg)
                    }
                })


        }
182 183
    }

zhanghaozhe committed
184
    componentDidUpdate() {
FE committed
185

zhanghaozhe committed
186
        this.setPreviousLocation()
zhanghaozhe committed
187 188 189 190

        if (!this.props.user.hasError && getParam('redirect')) {
            window.location.href = getParam('redirect')
        }
zhanghaozhe committed
191 192
    }

FE committed
193
    setPreviousLocation = () => {
zhanghaozhe committed
194 195 196
        const {location} = this.props
        let isInBlacklist = this.pathnameBlacklist.some(item => location.pathname.startsWith(item))
        !isInBlacklist && (this.previousLocation = location)
FE committed
197
    }
zhanghaozhe committed
198

199
    transformUser = res => {
200 201
        let payload
        if (res.data.code === 200) {
zhanghaozhe committed
202
            //移除红包统计cookie
zhanghaozhe committed
203
            this.removeShareCodeCookie()
204 205 206 207 208
            const {
                msg, data: {
                    avatar_file: avatar,
                    user_name: username,
                    is_vip: isVIP,
zhanghaozhe committed
209 210
                    uid,
                    code
211 212
                }
            } = res.data
zhanghaozhe committed
213

214 215 216 217 218 219 220
            payload = {
                hasError: false,
                msg,
                data: {
                    username,
                    isVIP,
                    avatar,
zhanghaozhe committed
221 222
                    uid,
                    code
223 224 225 226 227
                }
            }
        } else {
            payload = {
                hasError: true,
zhanghaozhe committed
228
                msg: res.data.msg,
zhanghaozhe committed
229 230
                code: res.data.code,
                data: {}
231 232 233
            }
        }
        return payload
zhanghaozhe committed
234
    }
235

zhanghaozhe committed
236 237 238
    transformWxUser = res => {
        let data = res.data
        if (data.errno == 200) {
zhanghaozhe committed
239 240
            //移除红包统计cookie
            this.removeShareCodeCookie()
zhanghaozhe committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
            let {uid, token, avatar_file: avatar, uname: username,} = data.data

            return {
                hasError: false,
                data: {
                    uid,
                    token,
                    avatar,
                    username
                },
                msg: data.msg
            }
        } else {
            let {code, msg} = data.data
            return {
                code,
                msg,
                hasError: true,
                data: {}
            }
        }
    }

264
    render() {
zhanghaozhe committed
265
        return <Routes/>
zhanghaozhe committed
266
    }
267 268
}

zhanghaozhe committed
269 270
export default compose(
    connect(
zhanghaozhe committed
271
        state => ({user: state.user}),
zhanghaozhe committed
272
        {setCurrentUser, startFetchUser}
zhanghaozhe committed
273 274
    ),
    withRouter
275
)(App)