<template>
	<view class="login">
		<h3 style="margin-top: 100px;" @click="login">学生评价位置调换系统</h3>
		<div id="self_defined_element" class="self-defined-classname ui-mt40"></div>
		<view class="intro">
			请打开钉钉APP扫码登录
		</view>
        
        <uni-popup ref="popup" background-color="#fff" :mask-click="false">
        	<view style="width: 350rpx;" class="popup-content pop">
        		<view style="margin-bottom: 24rpx;" class="">
        			账号
        		</view>
        		<view style="margin-bottom: 24rpx;" class="">
        			<uni-easyinput type="number" v-model="user.mobile" placeholder="请输入账号"
        				:maxlength="100"></uni-easyinput>
        		</view>
        		<view style="margin-bottom: 24rpx;" class="">
        			密码
        		</view>
        		<view style="margin-bottom: 24rpx;" class="">
        			<uni-easyinput type="password" v-model="user.password" placeholder="请输入账号"
        				:maxlength="100"></uni-easyinput>
        		</view>
        		<u-button @click="submit" type="primary">登录</u-button>
        		<u-button class="ui-mt20" @click="$refs.popup.close()">关闭</u-button>
        		<!-- <u-button class="ui-mt20" @click="loadVconsole()">加载/关闭vconsole</u-button> -->
        	</view>
        </uni-popup>
	</view>
</template>

<script>
	import Config from '@/common/js/config.js'
	import ddLogin from "@/static/js/ddLogin.js"
	export default {
		name:'',
		data(){
			return {
				redirectUrl : '',
				// redirectUrl : 'https://oy-desk.wucits.com',
				school_id : '',
                num : 0,
                user: {
                	mobile: '15158580014',
                	password: 'xlg123==='
                }
			}
		},
		mounted(){
			this.school_id=this.getUrlParam("school_id")
			// this.school_id=1
			
			if(!this.school_id){
				this.school_id=uni.getStorageSync("school_id")
			}else{
				uni.setStorageSync("school_id",this.school_id)
			}
			
			this.redirectUrl=Config.schoolBaseData.redirectUrl+"?school_id="+this.school_id
			let token=uni.getStorageSync("token")
			
			if(!token){
				this.buildCode()
			}else{
				uni.navigateTo({
					url: "/pages/studentRanked/studentRanked"
				})
			}
		},
		methods:{
            login() {
            	this.num++
            	if (this.num >= 10) {
            		this.$refs.popup.open('center');
                    this.num = 0
            	}
            },
            submit() {
            	this.$api.sendRequest({
            		url: `/mobile/login/passLogin`,
            		data: this.user,
            		success: res => {
            			this.loginCallBack(res.data)
            			this.$refs.popup.close()
            
            		},
            	})
            },
            loginCallBack(teacher){
                console.log(teacher,"denglu");
                this.$store.commit("setTeacherInfo",teacher)
                uni.setStorageSync('token', teacher.token)
                uni.navigateTo({
                	url:'/pages/studentRanked/studentRanked'
                })
            },
			doLogin(code){
				console.log("zhixinglema",this.school_id);
				this.$api.sendRequest({
					url: `/mobile/getUserInfoByScanCode`,
					method : 'post',
					data : {
						code,
						school_id : this.school_id
					},
					success: res => {
                        this.loginCallBack(res.data)
                    },
					error: res => {
				
					}
				})
			},
			getUrlParam(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return unescape(r[2]);
				return null;
			},
			buildCode(){
				console.log(this.redirectUrl,"this.redirectUrl")
				window.DTFrameLogin(
					{
						id: 'self_defined_element',
						width: '300',
						height: '300',
					},
					{
						redirect_uri: encodeURIComponent(this.redirectUrl),
						client_id: Config.schoolBaseData.client_id,
						scope: 'openid',
						response_type: 'code',
						prompt: 'consent',
					},
					(loginResult) => {
						const {redirectUrl, authCode, state} = loginResult;
						console.log(authCode,"ininini?",redirectUrl);
						this.doLogin(authCode)
						// 这里可以直接进行重定向
						
						// 也可以在不跳转页面的情况下,使用code进行授权
						
					},
					(errorMsg) => {
						// 这里一般需要展示登录失败的具体原因
						console.log(`Login Error: ${errorMsg}`);
					},
				);
			}
		}
	}
</script>

<style scoped>
	.login{
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		
	}
	.self-defined-classname {
		width: 300px;
		height: 300px;

		background-color: rebeccapurple;
	}
	.intro{
		color: #333;
		margin-top: 20px;
	}
</style>