<template>
	<div>
		<div class="noData flex-box-v flex-center-center">
			<div class="data-all">
				<img src="./assets/image/init.gif" class="appImg" />
				<div>管理执行难，就用功道云</div>
				<el-button class="refresh" type="primary" :loading="disabled" :disabled="disabled" v-if="isRefresh" @click="openLogin()">{{ text }}</el-button>
			</div>
		</div>
		<el-dialog title="通知" :visible.sync="dialogFormVisible" width="480px" :show-close="false" :close-on-click-modal="false">
			<div>
				<div style="font-size: 18px;">尊敬的用户:</div>
				<div style="margin: 10px 0;">您当前的套餐已到期，如需继续使用，请点击续费详情进行续费。</div>
			</div>
			<div style="border-radius: 15px;border: 1px solid #f1f1f1;padding: 10px; width: 276px;box-sizing: border-box;margin: 0 auto;">
				<img src="./assets/image/code.png"/>
			</div>
			<div class="fontColorF" style="text-align: center;margin-top: 15px;font-size: 18px;">手机钉钉扫码，付费升级</div>
		</el-dialog>
	</div>
</template>

<script>
export default {
	data() {
		return {
			text: '重试',
			corpId: '',
			isRefresh: false,
			disabled: false,
			showBtnCunt: 0,
			dialogFormVisible:false
		};
	},
	watch: {},
	mounted() {
		var url = window.location.href;
		var str = this.GetRequest(url).corpId || '123';
		var corpId = str.split('#')[0];
		this.corpId = corpId;
		if (corpId) {
			this.login(corpId);
		}
	},
	methods: {
		//重新登录
		openLogin() {
			var num = 10;
			var that = this;
			this.disabled = true;
			this.showBtnCunt = this.showBtnCunt + 1;
			var intterval = setInterval(function() {
				num--;
				if (num == 0) {
					that.text = '重试';
					that.disabled = false;
					if (that.showBtnCunt >= 2) {
						that.$alert('数据加载过慢，请联系客服', '温馨提示', {
							confirmButtonText: '确定',
							callback: action => {}
						});
					}
					clearInterval(intterval);
					return;
				}
				that.text = num;
			}, 1000);
			this.login(this.corpId);
		},
		login(corpId) {
			var that = this;
			this.loading = true;
			this.$dd.runtime.permission.requestAuthCode({
				corpId: corpId, // 企业id
				onSuccess: function(info) {
					that.$axios('post', '/api/ding/login', { authCode: info.code, corpId: corpId }).then(res => {
					// that.$axios('post', '/api/ding/login', { authCode: '962c50cef81a3da4a9a83487cd3a391d', corpId: 'ding011f57ab048cf202ffe93478753d9884' }).then(res => {
						var is = false;
						if (res.data.code == 1) {
							var { token, user } = res.data.data;
							user.employee_detail.role_list.forEach(item => {
								// if (user.is_official == 1) {
									if (item.name == 'admin' || item.name == 'creator' || item.name == 'point_manager') {is = true;}
								// }
							});
							if (is) {
								that.$setUserData(user);
								that.$router.replace({ path: '/index' });
							} else {
								that.$router.replace({ path: '/noAccess' });
							}
						}else if(res.data.code==2001){
							this.dialogFormVisible=true;
						}
					}).finally(() => {
						that.loading = false;
					});
				}
			});
		},
		GetRequest(urlStr) {
			if (typeof urlStr == 'undefined') {
				var url = decodeURI(location.search); //获取url中"?"符后的字符串
			} else {
				var url = '?' + urlStr.split('?')[1];
			}
			var theRequest = new Object();
			var strs;
			if (url.indexOf('?') != -1) {
				var str = url.substr(1);
				strs = str.split('&');
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
				}
			}
			return theRequest;
		}
	}
};
</script>

<style scoped="scoped">
.refresh {
	margin-top: 100px;
	width: 100px;
}
.appImg {
	width: 200px;
	height: 200px;
}
.noData {
	text-align: center;
	position: fixed;
	left: 0;
	top: 0;
	right: 0;
	bottom: 0;
}
.data-all {
	margin-bottom: 10%;
	color: #595959;
}
</style>
