<template>
    <div>
        <img src="../assets/login_background.png"
             style="max-width: 100%"
        />
        <h1>正在自动登录，请稍后</h1>
    </div>
</template>

<script>
    import 'whatwg-fetch';
    import { BasicFunction } from '../connector/basic-service';
    import {Toast} from 'vant';

    export default {
        created: function () {
            let otp = this.$route.query.o;
            // let otp = BasicFunction.utils_get_param("o");
            console.log('otp: ');
            console.log(otp);
            let userid = '';
            if (localStorage.getItem('frontend-userid')) {
                userid = localStorage.getItem('frontend-userid');
                this.$router.push('index');
            }
            BasicFunction.get_data("ajaxotplogin?otp=" + otp.toString(), function (response) {
                console.log("------ User Info --------");
                console.log(response);
                if (response.ret === "10000") {
                    let user = response.model.list[0];
                    userid = user.uid;
                    localStorage.setItem('frontend-userid', userid);
                } else {
                    // 失败的处理
                    console.warn("获取用户信息失败");
                    Toast.fail('链接失效，请回到公众号重新获取链接');
                }
            }, {});
            // if (userid.length === 0) this.$router.push('warning');
            // else this.$router.push('index');
            this.$router.push('index');
        }
    }

    /** Post JSON **/
    /*
    // 怎么把一段放到另一个文件？

    fetch('/users', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            name: 'Hubot',
            login: 'hubot',
        })
    })

    */
</script>

<style scoped>
    .card {
        margin: 20px 20px 130px;
        border: solid 1px #e4e4e4;
    }
    .login {
        margin-bottom: 10px;
        width: 85%;
        background-color: #fd6740;
        border-color: #fd6740;
    }
    .register {
        margin-bottom: 5px;
        width: 85%;
    }
</style>