<template>
    <div id="app">
        <router-view style="height:100%;"></router-view>
    </div>
</template>

<script>
    import Vue from 'vue'
    import md5 from 'blueimp-md5'
    import * as Const from './const'

    export default {
        name: 'app',
        beforeCreate(){
            Vue.http.options.root = 'http://api.yizhiwang.wang/web';
            Vue.http.interceptors.push(function(request, next) {
                var timestamp = Math.round(new Date().getTime()/1000);
                request.headers.set('AppKey', Const.appkey);
                request.headers.set('Rest-Auth', md5(timestamp+Const.appkey+Const.secret+request.url));
                request.headers.set('Rest-Time', timestamp+'');
                let token = this.$localStorage.get('yzw_token');
                console.log(token);
                if(request.url == 'apiv1/auths'){
                    next();
                    return;
                }
                if(!token){
                    getToken.call(this,request,next);
                }else{
                    request.params.access_token = token;
                    next(function(res) {
                        console.log(this);
                        if(res.body.code && res.body.code != '200'){
                            this.$notify({
                                title: '错误',
                                message: res.body.message,
                                type:'error'
                            });
                        }
                        if(res.body.code == '1001'){
                            getToken.call(this,request,next);
                        }
                    });
                }
            });
            let token = this.$localStorage.get('yzw_token');
            if(!token){
                getToken.call(this,null,null);
            }
        }
    }

    function getToken(request,next){
        //接口授权
        this.$http.post('apiv1/auths',{}).then(res => {
            if(res.body.code == 200){
                console.log('获取token');
                this.$localStorage.set('yzw_token',res.body.info.access_token);
                if(this.$route.path !== '/login' && this.$route.path !== '/reg'){
                    this.$router.push('/login');
                    return;
                }else{
                    this.$notify({
                        title: '错误',
                        message: '接口授权超时，请重新操作',
                        type:'error'
                    });
                    //重发请求
//                    this.$http[request.method.toLowerCase()](request.url,request.body).then(res => {
//                        next();
//                    },res => {
//                        next();
//                    });
                }
            }else{
                alert(res.body.message);
            }
        },res => {
            console.log('错误');
        });
    }
</script>

<style>

    html,body{
        height:100%;
    }

    body{
        background-color:#fff;
    }

    #app{
        height:100%;
    }

    .content-box{
        min-height:100%;
        padding-top:100px;
        padding-bottom:120px;
        box-sizing:border-box;
    }

    .btn-transparent{
        background-color:transparent;
        color:#000;
        border:2px solid #000;
        /*font-weight:bold;*/
        transition:all .3s;
    }

    .btn-transparent:hover{
        background-color:#000;
        color:#fff;
    }

    .btn:focus,.btn:active:focus{
        outline:none;
    }

    input.is-danger{
        border-color:#ff3860;
    }

    span.help.is-danger{
        display:block;
        color:#ff3860;
        font-size:12px;
        margin-top:5px;
    }

</style>
