<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;">您当前的套餐已到期，如需继续使用，请<span @click="bundleOfServices" style="color:#409eff;cursor:pointer;">联系客服</span>或扫描下方二维码进行续费。</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="fontColorC" 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
		};
	},
	created() {
		this.getTypes();
	},
	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: {
		getTypes() {
			var that = this;
			this.$axios('get', '/api/integral/types').then(res => {
				that.$setTyps(res.data.data.list);
			});
		},
		bundleOfServices(){
			this.$dd.biz.util.openSlidePanel({
				url:"https://page.dingtalk.com/wow/dingtalk/act/serviceconversation?wh_biz=tm&showmenu=false&goodsCode=DT_GOODS_881607043109331&corpId="+this.$getCache('corpId')+"&token=5784a3e6b5e025ee891517ea814180f4",
				title: '客服群',
				onSuccess : function(result) {},
				onFail : function(err) {
					console.log('onFail')
				}
			})
		},
		//重新登录
		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.$setCache('corpId',corpId)
			this.$dd.runtime.permission.requestAuthCode({
				corpId: corpId, // 企业id
				onSuccess: function(info) {
					that.$axios('post', '/api/ding/login', { authCode: info.code, corpId: corpId }).then(res => {
						if (res.data.code == 1) {
							var { token, user } = res.data.data;
							if (user.is_official==1) {
								that.$setUserData(user);
								that.$router.replace({ path: '/index' });
							}else{
								that.$router.replace({ path: '/noAccess' });
							}
						}else if(res.data.code==2001){
							that.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>
