<template>
    <div>
        <img src="http://pjczv6ygf.bkt.clouddn.com/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 () {
            Toast.loading({
                mask: true,
                message: '加载中...',
                duration: 1500
            });
            if (localStorage.getItem('frontend-userid')) {
                Toast.success('登录成功');
                this.$router.push('index');
                return;
            }
            let otp = this.$route.query.o;
            // console.log('otp: ');
            // console.log(otp);
            if (otp === undefined) {
                otp = BasicFunction.utils_get_param("o");
            }
            // console.log('otp: ');
            // console.log(otp);
            let userid = '';

            if (otp === null) {
                // no-otp
                Toast.fail("链接失效！请回到公众号重新获取链接");
                return;
            }
            let that = this;
            BasicFunction.get_data("ajaxotplogin?otp=" + otp.toString(), function (response) {
                // console.log("------ User Info login --------");
                // console.log(response);
                if (response.ret === "10000") {
                    let user = response.model;
                    userid = user.userId;
                    localStorage.setItem('frontend-userid', userid);
                    Toast.success('登录成功');
                    that.$router.push('index');
                } else {
                    // 失败的处理
                    // console.warn("获取用户信息失败");
                    Toast.fail('链接失效！请回到公众号重新获取链接');
                    that.$router.push('warning');
                }
            }, {});
        }
    }

    /** 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>