App.js 8.48 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
import { initialState } from "@/store/userReducer"
xuzhenghua committed
7
import { withRouter, Link } from 'react-router-dom'
xuzhenghua committed
8
import { compose } from 'redux'
zhanghaozhe committed
9 10
import { getParam, http, browser } from "@/utils"
import { Toast } from "antd-mobile"
xuzhenghua committed
11
import { addDays } from 'date-fns'
xuzhenghua committed
12
import stringify from 'json-stringify-safe'
13

zhanghaozhe committed
14 15

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


// 默认样式
zhanghaozhe committed
21
import './assets/css/index.scss'
zhanghaozhe committed
22 23

// iconfont
zhanghaozhe committed
24
import './assets/font/iconfont.css'
zhanghaozhe committed
25

26 27
class App extends Component {

28
  static displayName = 'App'
zhanghaozhe committed
29

30
  previousLocation = {pathname: '/', search: '', hash: ''}
31

32
  records = []
FE committed
33

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

36
  firstLoad = true
xuzhenghua committed
37 38

  componentWillMount() {
xuzhenghua committed
39
    if (!getParam('version')) {
xuzhenghua committed
40
      if (!cookie.get('qimoClientIdedu')) {
xuzhenghua committed
41 42
        this.getqimoClientId()
      } else {
xuzhenghua committed
43 44
        window.qimoClientId = cookie.get('qimoClientIdedu')
      }
xuzhenghua committed
45 46
    }
  }
xuzhenghua committed
47

48 49 50
  componentDidMount() {
    this.umengStatistic()
    //平台信息
xuzhenghua committed
51 52
    !getParam('version') && cookie.set('plat', '5', {domain: '.julyedu.com'})

zhanghaozhe committed
53

54 55 56
    if (browser.isWeixin && browser.isIOS) {
      sessionStorage.setItem('enter_url', window.location.href)
    }
zhanghaozhe committed
57

58 59
    this.getUser()
    this.utm()
zhanghaozhe committed
60

61
    const {history} = this.props
zhanghaozhe committed
62 63


64 65
    this.setNavigationRecord(this.props.location, this.props.history.action)
    this.setPreviousLocation()
zhanghaozhe committed
66 67


68 69 70 71
    history.listen((location, action) => {
      this.firstLoad = false
      this.setNavigationRecord(location, action)
      this.utm()
zhanghaozhe committed
72

73 74 75 76
      if (cookie.get('uid') && this.props.user.hasError) {
        this.getUser()
      }
      if (location.pathname === '/passport') {
xuzhenghua committed
77
        window.localStorage.setItem('binding_redirect', stringify(this.previousLocation))
78 79 80 81 82 83 84 85 86 87
      }
      const {pathname, state} = location
      if (pathname.startsWith('/passport')) {
        location.state = {
          ...state,
          ...{
            from: {
              pathname: this.previousLocation.pathname,
              search: this.previousLocation.search,
              hash: this.previousLocation.hash
zhanghaozhe committed
88
            }
89
          }
90
        }
91 92 93 94 95
      } else {
        this.removeShareCodeCookie()
      }
    })

xuzhenghua committed
96 97 98
    if (!getParam('version')) {
      this.QimoClient()
    }
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
  }

  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)
  }

  removeShareCodeCookie = () => {
    cookie.remove('share_code', {path: '/', domain: '.julyedu.com'})
  }

  setNavigationRecord = (location, action) => {
    const {pathname, search, hash} = location
    let isLastRecord = location.pathname === (this.records.length && this.records[this.records.length - 1].pathname)
    let needHistoryMutation = location.pathname !== this.previousLocation.pathname

    switch (action) {
      case 'POP':
        if (needHistoryMutation) {
          this.firstLoad ? this.records.push({pathname, search, hash}) : this.records.pop()
        } else {
          this.records[this.records.length - 1] = location
xuzhenghua committed
124
        }
125 126 127 128 129 130
        break
      case 'REPLACE':
        this.records.length > 1 && (this.records[this.records.length - 1] = {pathname, search, hash})
        break
      default:
        !isLastRecord && this.records.push({pathname, search, hash})
xuzhenghua committed
131 132
    }

133 134 135 136 137 138 139 140 141
    location.state && location.state.records
      ? (location.state.records = this.records)
      : location.state ? location.state = {...location.state, records: this.records}
      : (location.state = {records: this.records})
  }


  utm = () => {
    // utm统计  m站全站统计广告投放、以及统计详情页浏览
xuzhenghua committed
142 143 144
    let zhihu_cb = getParam('cb')

    if (zhihu_cb) {
145
      let data = {
xuzhenghua committed
146
        'zhihu_cb': zhihu_cb
147 148
      }

xuzhenghua committed
149
      http.post(`${API['home']}/sys/zhihu/firstRecord`, data)
150
        .then(res => {
zhanghaozhe committed
151
        })
zhanghaozhe committed
152

153 154 155 156 157 158
    }
  }

  getUser = () => {
    //获取用户信息
    this.props.startFetchUser()
159

160 161 162 163 164 165 166 167 168 169 170
    //微信
    let code = getParam('code')
    let oid = getParam('oid')
    if (code && !oid) {

      //针对2019双旦活动特殊处理,活动结束后删掉
      //treasure_code=3538333139312c31&origin=2&aa=bb&code=0211aiYB1VVOL60rmWWB1367YB11aiY7&state=STATE
      if (getParam('treasure_code') == '3538333139312c31' && code == '0211aiYB1VVOL60rmWWB1367YB11aiY7') {
        return
      }

xuzhenghua committed
171
      http.get(`${API["passport-api"]}/m/wx_loginInfo/code/${code}?redirect=${encodeURIComponent(window.location.href)}`)
172 173 174 175 176 177 178 179 180 181 182 183 184
        .then(res => {
          let data = res.data
          if (data.errno == 200) {
            if (data.data['is_bind_mobile']) {
              window.location.assign(data.data.url)
            } else {
              let user = this.transformWxUser(res)
              let {role, uid, token} = data.data
              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: '/'})
              this.props.setCurrentUser(user)
FE committed
185
            }
186 187
          } else {
            Toast.info(data.msg)
188
            this.props.setCurrentUser(initialState)
189 190
          }
        })
zhanghaozhe committed
191 192


193 194 195 196
    } else {
      http.get(`${API.home}/m/user_info`).then(res => {
        this.props.setCurrentUser(this.transformUser(res))
      })
197
    }
198

199
  }
200

201
  componentDidUpdate() {
zhanghaozhe committed
202

203
    this.setPreviousLocation()
zhanghaozhe committed
204

205 206
    if (!this.props.user.hasError && getParam('redirect')) {
      window.location.href = getParam('redirect')
FE committed
207
    }
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  }

  setPreviousLocation = () => {
    const {location} = this.props
    let isInBlacklist = this.pathnameBlacklist.some(item => location.pathname.startsWith(item))
    !isInBlacklist && (this.previousLocation = location)
  }

  transformUser = res => {
    let payload
    if (res.data.code === 200) {
      //移除红包统计cookie
      this.removeShareCodeCookie()
      const {
        msg, data: {
          avatar_file: avatar,
          user_name: username,
          is_vip: isVIP,
          uid,
          code
228
        }
229 230 231 232 233 234 235 236 237 238 239
      } = res.data

      payload = {
        hasError: false,
        msg,
        data: {
          username,
          isVIP,
          avatar,
          uid,
          code
zhanghaozhe committed
240
        }
241 242 243 244 245 246 247 248
      }
    } else {
      payload = {
        hasError: true,
        msg: res.data.msg,
        code: res.data.code,
        data: {}
      }
zhanghaozhe committed
249
    }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    return payload
  }

  transformWxUser = res => {
    let data = res.data
    if (data.errno == 200) {
      //移除红包统计cookie
      this.removeShareCodeCookie()
      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: {}
      }
zhanghaozhe committed
278
    }
279 280
  }

xuzhenghua committed
281 282

  // 七陌客服代码 start
xuzhenghua committed
283
  getqimoClientId = () => {
xuzhenghua committed
284
    http.post(`${API['home']}/qiMoUserId`)
xuzhenghua committed
285 286 287 288 289 290 291 292
      .then(res => {
        if (res.data.code == 200) {
          window.qimoClientId = res.data.data.userId
          cookie.set('qimoClientIdedu', res.data.data.userId, {domain: '.julyedu.com'})
        }
      })
      .catch(function () {
      })
xuzhenghua committed
293 294
  }

xuzhenghua committed
295
  QimoClient = () => {
xuzhenghua committed
296
    let zhihu_cb = getParam('cb')
xuzhenghua committed
297 298
    var src = ''
    // 知乎投放
xuzhenghua committed
299
    if (zhihu_cb) {
xuzhenghua committed
300
      src = 'https://webchat.7moor.com/javascripts/7moorInit.js?accessId=e2ca4f90-2d04-11ea-84c3-43908ae47640&autoShow=false&language=ZHCN'
xuzhenghua committed
301 302
    } else {
      // m端
xuzhenghua committed
303
      src = 'https://webchat.7moor.com/javascripts/7moorInit.js?accessId=fbcf9aa0-2d03-11ea-84c3-43908ae47640&autoShow=false&language=ZHCN'
xuzhenghua committed
304 305 306 307 308 309 310 311 312 313
    }

    var script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = src
    script.async = 'async'
    document.body.appendChild(script)

  }

xuzhenghua committed
314
  // 七陌客服代码 end
xuzhenghua committed
315 316


317
  render() {
xuzhenghua committed
318 319
    return <>
      <Routes/>
zhanghaozhe committed
320
      {/*<Link className={'year19-index'} to="/box/boxActive">
zhanghaozhe committed
321
        <img src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/treasure-active/m/global-entry.png" alt=""/>
zhanghaozhe committed
322
      </Link>*/}
xuzhenghua committed
323
    </>
324
  }
325 326
}

zhanghaozhe committed
327
export default compose(
328 329 330 331 332
  connect(
    state => ({user: state.user}),
    {setCurrentUser, startFetchUser}
  ),
  withRouter
333
)(App)