import React, { Component } from 'react'
import Routes from './router'
import cookie from 'js-cookie'
import { connect } from "react-redux";
import { setCurrentUser, startFetchUser } from "@/store/userAction";
import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
import { getParam, http } from "@/utils";
import { Toast } from "antd-mobile";
import { addDays } from 'date-fns'


//拦截ajax请求，返回mock数据
/*import mock from '@/utils/mock'
mock()*/


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

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

class App extends Component {


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


        this.props.startFetchUser()
        http.get(`${API.home}/m/user_info`).then(res => {
            this.props.setCurrentUser(this.transformUser(res))
        })




        //微信
        let code = getParam('code')

        if (code) {

            http.get(`${API['home']}/m/wx_loginInfo/code/${code}`)
                .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 = {expires: addDays(new Date(), 90)}
                            cookie.set('role', role, expires)
                            cookie.set('uid', uid, expires)
                            cookie.set('token', token, expires)
                            this.props.setCurrentUser(user)
                        }
                    } else {
                        Toast.info(data.msg)
                    }
                })


        }


    }

    transformUser = res => {
        let payload
        if (res.data.code === 200) {
            const {
                msg, data: {
                    avatar_file: avatar,
                    user_name: username,
                    is_vip: isVIP,
                    uid,
                    code
                }
            } = res.data

            payload = {
                hasError: false,
                msg,
                data: {
                    username,
                    isVIP,
                    avatar,
                    uid,
                    code
                }
            }
        } else {
            payload = {
                hasError: true,
                msg: res.data.msg,
                code: res.data.code,
                data: {}
            }
        }
        return payload
    }

    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: {}
            }
        }
    }

    render() {
        return <Routes/>
    }
}

export default compose(
    connect(
        null,
        {setCurrentUser, startFetchUser}
    ),
    withRouter
)(App)