<?php /*a:2:{s:53:"D:\phpEnv\www\SCM-API\app\admin\view\index\login.html";i:1750736984;s:52:"D:\phpEnv\www\SCM-API\app\admin\view\layout\vue.html";i:1750736984;}*/ ?>

    export default{
        data(){
            return {
                thirdLogin:false,
                qrcode:'',
                captchaUrl:'',
                loginForm:{
                    __token__:'',
                    username: '',
                    password: '',
                    captcha: '',
                    savepassword:[1],
                },
                rules:{
                    username:[{required:true,message:'用户名不能为空！',}],
                    password:[{required:true,message:'密码不能为空！',}],
                },
                dialogVisible:false,
                adminlist:[],
                checked:''
            }
        },
        onLoad:function (){
            this.refreshCaptcha();
            let width=document.body.clientWidth;
            this.thirdLogin=Yunqi.data.thirdLogin && width>800;
            window.addEventListener('resize',()=>{
                let width=document.body.clientWidth;
                this.thirdLogin=Yunqi.data.thirdLogin && width>800;
            });
            this.qrcode=Yunqi.data.qrcode;
        },
        onShow:function (){
            this.loginForm.__token__=document.getElementsByTagName('input')[0].value;
            this.loginForm.username= localStorage.getItem('username') || '';
            this.loginForm.password= localStorage.getItem('password') || '';
            this.loginForm.savepassword= localStorage.getItem('savepassword')? [1] : [];
            this.checklogin();
        },
        methods:{
            refreshCaptcha:function (){
                this.captchaUrl=Yunqi.config.baseUrl+"captcha?"+Math.random();
            },
            checklogin:function (){
                if(!this.thirdLogin){
                    return;
                }
                let token=document.querySelector('input[name="__token__"]').value;
                Yunqi.ajax.get('qrcodeLogin', {token:token},false,false).then(res=>{
                    Yunqi.message.success('登录成功');
                    setTimeout(()=>{
                        this.redirect();
                    },1000);
                }).catch(err=>{
                    if(err.data.length>0){
                        this.adminlist=err.data;
                        this.dialogVisible=true;
                        return;
                    }
                    setTimeout(()=>{
                        this.checklogin();
                    },2000);
                });
            },
            cancelDialog:function (){
                location.reload();
            },
            confirmDialog:function (){
                let token=document.querySelector('input[name="__token__"]').value;
                Yunqi.ajax.get('qrcodeLogin',{token:token,admin_id:this.checked},true,false).then(res=>{
                    this.dialogVisible=false;
                    Yunqi.message.success('登录成功');
                    setTimeout(()=>{
                        this.redirect();
                    },1000);
                });
            },
            redirect:function (){
                location.href=Yunqi.config.baseUrl+'index';
            },
            login:function (){
                this.$refs.loginForm.validate((valid)=>{
                    if(valid){
                        Yunqi.ajax.post('login',this.loginForm,true).then(res=>{
                            let savepassword=this.loginForm.savepassword.length>0?1:0;
                            if(savepassword){
                                localStorage.setItem('username',this.loginForm.username);
                                localStorage.setItem('password',this.loginForm.password);
                                localStorage.setItem('savepassword',savepassword);
                            }else{
                                localStorage.removeItem('username');
                                localStorage.removeItem('password');
                                localStorage.removeItem('savepassword');
                            }
                            this.redirect();
                        }).catch(err=>{
                            if(err.data){
                                this.refreshCaptcha();
                            }
                        });
                    }
                });
            }
        }
    }
