//--- 获取cookie
function getCookie(sName) {
    var aCookie = document.cookie.split("; ");
    for (var j = 0; j < aCookie.length; j++) {
        var aCrumb = aCookie[j].split("=");
        if (escape(sName) == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    return null;
}
//判断是否为网址
function IsURL(strUrl) {
    var regular = /^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))\b(\/[-a-z0-9_:\@&?=+,.!\/~%\$]*)?)$/i
    if (regular.test(strUrl)) {
        return true;
    }
    else {
        return false;
    }
}
function yuming_shezhi() {
	var domain = getCookie("QinYinCookie");
	xtip.open({
		type: 'html',
		width: '350px',
		height: '170px',
		content: '<div class="layui-form-pane" style="padding:15px 15px;"><input type="text" id="domain_ck" style="width:100%;padding: 10px 10px;border: 1px solid #ccc;border-radius:5px;margin-bottom:10px;" placeholder="请输入您的域名" value="'+domain+'"><button type="button" style="width:100%;padding: 10px 10px;border: 1px solid #ccc;border-radius:5px;margin-bottom:10px;" onclick="app11_pay()">确认使用</button></div>',
		title: '域名信息设置',
		lock: true,
	});
}
function app11_pay() {
	$.ajax({
		url:'',
		type: "post",
		data: {domain:$("#domain_ck").val()},
		dataType: "json",
	});
	xtip.msg('保存成功');
	setTimeout(function(){
		location.reload();
	},1000)
}
function app_pay(id) {
	var domain = getCookie("QinYinCookie");
	if(IsURL(domain)){
		$.ajax({
			url:"https://su1018.cn/AppPay.php?app=purchase",
			type: "post",
			data: {id:id},
			dataType: "json",
			success: function(result) {
				xtip.open({
					type: 'html',
					width: '300px',
					height: '350px',
					content: '<div class="layui-form-pane" style="padding:15px 15px;"><img id="pay_cone" style="margin:-13px;width:295px;height:290px;" src="'+result['code']+'"></div>',
					title: '支付宝扫码付款',
					lock: true,
				});
				setInterval(function() {
					var domain = getCookie("QinYinCookie");
					var url = "https://su1018.cn/AppPay.php?app=chaxun&domain="+domain+"&outTradeNo="+result.outTradeNo;
					$.getJSON(url, function(result) {
						xtip.msg(result.msg,{pos:'b'})
						if (result.code == 200) {
							location.reload();
						}
					});
				}, 7000);
			}
		});
	}else{
		yuming_shezhi();
	}
}
function yuming_shouquan() {
	$.ajax({
		url:"https://su1018.cn/AppPay.php?app=shouquan",
		type: "post",
		dataType: "json",
		success: function(result) {
			var arr = result;
			for(var i=0;i<arr.length;i++){
				//console.log('<option value="'+arr[i].code+'">'+arr[i].name+'</option>');
				var content = content+'<option value="'+arr[i].code+'">'+arr[i].name+'</option>';
			}
			xtip.open({
				type: 'html',
				width: '300px',
				height: '220px',
				content: '<div class="layui-form-pane" style="padding:15px 15px;"><select id="ChengXuCoDe" style="width:100%;padding: 10px 10px;border: 1px solid #ccc;border-radius:5px;margin-bottom:10px;">'+content+'</select><input type="text" placeholder="例：http://www.qinyin.com/" id="ChengXuUrl" style="width:100%;padding: 10px 10px;border: 1px solid #ccc;border-radius:5px;margin-bottom:10px;"><button type="button" class="search-btn" onclick="BenDiShengCheng()" style="width:100%;padding: 10px 10px;border: 1px solid #ccc;border-radius:5px;margin-bottom:10px;background:red;color:#fff;">生成</button></div>',
				title: '本地授权生成',
				lock: true,
			});
		}
	});
}
function saveShareContent (content, fileName) {
    let downLink = document.createElement('a')
    downLink.download = fileName
    //字符内容转换为blod地址
    let blob = new Blob([content])
    downLink.href = URL.createObjectURL(blob)
    // 链接插入到页面
    document.body.appendChild(downLink)
    downLink.click()
    // 移除下载链接
    document.body.removeChild(downLink)
}
function BenDiShengCheng() {
    $.ajax({
        url: 'https://su1018.cn/token.php?api=shouquan',
        type: "post",
        data: {code:$("#ChengXuCoDe").val(),url:$("#ChengXuUrl").val()},
        dataType: "json",
        success: function(result) {
			if(result.code == 200){
				saveShareContent (result.msg,'auth.ini');
			}else{
				xtip.msg(result.msg);
			}
        }
    });
}