<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>
				<div style="margin-top: 60px;" v-if="num==3">
					<div style="text-align: center;margin-bottom: 10px;" class="fontColorB">当前企业信息未能成功同步，请联系人工客服为您开通应用</div>
					<el-button  type="primary" :loading="disabled" :disabled="disabled"  @click="activeNavigation()">联系客服</el-button>
				</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>
		<el-dialog title="通知" :visible.sync="dialogFormVisible2" width="480px" :show-close="false" :close-on-click-modal="false">
			<div>
				<div style="font-size: 18px;">尊敬的用户:</div>
				<div style="margin: 10px 0;">您未授权进入系统，请联系管理员将你纳入绩效应用</div>
			</div>
		</el-dialog>
	</div>
</template>

<script>
export default {
	data() {
		return {
			text: '重试',
			corpId: '',
			isRefresh: false,
			disabled: false,
			showBtnCunt: 0,
			dialogFormVisible:false,
			dialogFormVisible2:false,
			num:0,
		};
	},
	mounted() {
		this.$setCache('activeIndex',null);
		var url = window.location.href;
		var str = this.GetRequest(url).corpId || 'ding49b5c3bf0b205896bc961a6cb783455b';
		var corpId = str.split('#')[0];
		this.corpId = corpId;
		if (corpId) {
			this.login(corpId);
		}
		if(this.GetRequest(url).url){
			this.$setCache("url",this.GetRequest(url).url)
		}
	},
	methods: {
		bundleOfServices(){
			this.$dd.biz.util.openSlidePanel({
				url:"https://page.dingtalk.com/wow/dingtalk/act/serviceconversation?wh_biz=tm&showmenu=false&goodsCode=DT_GOODS_881649645924368&corpId="+this.$getCache('corpId')+"&token=b26816672b9a5f26022afcc5f1926d93",
				title: '客服群',
				onSuccess : function(result) {
				},
				onFail : function(err) {
					console.log('onFail')
				}
			})
		},
		
		activeNavigation(){
			let url=`https://page.dingtalk.com/wow/dingtalk/act/serviceconversation?wh_biz=tm&showmenu=false&goodsCode=DT_GOODS_881649645924368&corpId=${this.$getCache('corpId')}&token=b26816672b9a5f26022afcc5f1926d93`
			this.$dd.biz.util.openSlidePanel({
				url:url,//页面名称
				title: '客服群',
				onSuccess : function() {
					/**/
				},
				onFail : function(err) {}
			});
		},
		//重新登录
		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 => {
						// console.log(JSON.stringify(res.data))
						if (res.data.code == 1) {
							let user = res.data.data;
							let timestamp = Date.parse(new Date()); //当前时间戳
							if((user.site.expire_time*1000)<timestamp){ //判断是否过期
								that.dialogFormVisible=true;
								return false
							}
							if (user.is_official==1&&user.is_scope == 1) {
								that.$setUserData(user);
								that.$router.replace({ path: '/index' });
							}else{
								if(user.is_official==0){//未启用
								   that.$router.replace({ path: '/noAccess' });
								}
								if(user.is_scope==0){//未授权
								   that.$router.replace({ path: '/noAccess',query:{id:1} });
								}	
							}
						}else if(res.data.code==2001){
							that.dialogFormVisible=true;
						}else if(res.data.code==4000||res.data.code==4001){
							if (that.num == 3) { return false }
							setTimeout(()=> {
							  that.num++
							  that.login(that.corpId);
							}, 3000);
						}
					}).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>
