App.js 3.49 KB
Newer Older
1
import React, { Component } from 'react'
zhanghaozhe committed
2 3
import Routes from './router'
import cookie from 'js-cookie'
4
import { connect } from "react-redux";
zhanghaozhe committed
5
import { setCurrentUser, startFetchUser } from "@/store/userAction";
zhanghaozhe committed
6 7
import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
zhanghaozhe committed
8
import { getParam, http } from "@/utils";
zhanghaozhe committed
9 10
import { Toast } from "antd-mobile";
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 19 20 21 22 23


// 默认样式
import './assets/css/index.scss';

// iconfont
import './assets/font/iconfont.css';

24 25 26 27
class App extends Component {


    componentDidMount() {
zhanghaozhe committed
28
        //平台信息
zhanghaozhe committed
29 30
        cookie.set('plat', '5', {domain: '.julyedu.com'})

zhanghaozhe committed
31

zhanghaozhe committed
32
        this.props.startFetchUser()
zhanghaozhe committed
33
        http.get(`${API.home}/m/user_info`).then(res => {
34
            this.props.setCurrentUser(this.transformUser(res))
35
        })
36 37


zhanghaozhe committed
38 39 40


        //微信
zhanghaozhe committed
41 42 43 44
        let code = getParam('code')

        if (code) {

zhanghaozhe committed
45
            http.get(`${api['home']}/m/wx_loginInfo/code/${code}`)
zhanghaozhe committed
46 47 48 49 50 51 52
                .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
53 54 55
                            let user = this.transformWxUser(res)
                            let {role, uid, token} = data.data
                            let expires = {expires: addDays(new Date(), 90)}
zhanghaozhe committed
56 57 58
                            cookie.set('role', role, expires)
                            cookie.set('uid', uid, expires)
                            cookie.set('token', token, expires)
zhanghaozhe committed
59
                            this.props.setCurrentUser(user)
zhanghaozhe committed
60 61 62 63 64 65 66 67 68 69
                        }
                    } else {
                        Toast.info(data.msg)
                    }
                })


        }


70 71
    }

72
    transformUser = res => {
73 74 75 76 77 78 79
        let payload
        if (res.data.code === 200) {
            const {
                msg, data: {
                    avatar_file: avatar,
                    user_name: username,
                    is_vip: isVIP,
zhanghaozhe committed
80 81
                    uid,
                    code
82 83
                }
            } = res.data
zhanghaozhe committed
84

85 86 87 88 89 90 91
            payload = {
                hasError: false,
                msg,
                data: {
                    username,
                    isVIP,
                    avatar,
zhanghaozhe committed
92 93
                    uid,
                    code
94 95 96 97 98
                }
            }
        } else {
            payload = {
                hasError: true,
zhanghaozhe committed
99
                msg: res.data.msg,
zhanghaozhe committed
100 101
                code: res.data.code,
                data: {}
102 103 104
            }
        }
        return payload
zhanghaozhe committed
105
    }
106

zhanghaozhe committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    transformWxUser = res => {
        let data = res.data
        if (data.errno == 200) {
            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: {}
            }
        }
    }

133
    render() {
zhanghaozhe committed
134 135
        return <Routes/>
    }
136 137
}

zhanghaozhe committed
138 139 140
export default compose(
    connect(
        null,
zhanghaozhe committed
141
        {setCurrentUser, startFetchUser}
zhanghaozhe committed
142 143
    ),
    withRouter
144
)(App)