<?php
exit('asdasd');
	include_once("./define.php");
	include_once("./config/config.php");
	include_once('./includes/upload_cls.php');
	include_once('./includes/weixin_class_sdk.php');
	$qrcode = _R('q');//二维码
	
	$qarr = $ass->one('village_qrcode','','qrcode_code="'.$qrcode.'"');

	$msg = '二维码正常';
	$code = 0;
	$sn = '';
	//判断小程序是否合法
	if($qarr){

		$shop = $ass->one('shop','','qrcode_code="'.$qrcode.'"');

		$appid = $shop['shop_dyappid'];
		$secret = $shop['shop_dyappsecret'];

		if(!$appid){
			exit("抖音小程序没有配置！");
		}
		$sn = $qrcode;
		if($qarr['raffle_state']==3){
			$code = 401;
			$msg = '二维码已结束';
		}else{
			//$if($qarr['qrcode_money']){
			//}
		}
	}else{
		$appid = "tte04f96f27f4765ed01";
		$secret = "8409a8248b4d822516b8d0c8a8e9b7cdb6841b2d";

		$code = 401;
		$msg = '二维码不存在';
	}

	$query = array(
		'code'	=> $code,
		'msg'	=> $msg,
		'code'	=> $code,
		'data'	=> array(
			'SN' => $sn
		)
	);

	
	//抖音跳转开始
	$url = "https://open.douyin.com/oauth/client_token/";
	$data11 = array(
		'grant_type' => 'client_credential',
		'client_key' => $appid,
		'client_secret' => $secret,
	);
	$method = "POST";
	$type = "json";
	$tokenArr = curl_data($url,json_encode($data11),$method,$type);

	$tokenArr = json_decode($tokenArr, true);

	$token = $tokenArr['data']['access_token'];

	$url = "https://open.douyin.com/api/apps/v1/url_link/generate/";
	$data1 = array(
		'app_name' => 'douyin',
		'app_id' => $appid,
		'path'=>'pages/index/index',
		'query'=>json_encode($query),
		'expire_time'=>1748534400
	);

	$method = "POST";
	$type = "json";
	$urlArr = curl_data($url,json_encode($data1),$method,$type,$token);
	//var_dump($urlArr);
	//echo "<br>";
	//echo "<br>";

	$urlArr = json_decode($urlArr, true);

	$authUrl = $urlArr['data']['url_link'];

	//echo "<br>".$authUrl;


	header("Location: $authUrl");



	//抖音跳转结束

	

	function curl_data($url, $data = [], $method = 'POST', $type = 'form-data',$access_token='')
    {
        //初始化
        $ch = curl_init();
        $headers = [
            'form-data' => ['Content-Type: multipart/form-data'], 'json' => ['Content-Type: application/json'],
        ];

        //统一转化为大写
        $method = strtoupper($method);
        if ($method == 'GET') {
            if ($data) {
                $querystring = http_build_query($data);
                $url = $url . '?' . $querystring;
            }
        }
        // 请求头，可以传数组
        curl_setopt($ch, CURLOPT_URL, $url);
		if($access_token){
			curl_setopt($ch, CURLOPT_HTTPHEADER, [
				'content-type: application/json',
				'access-token: '.$access_token,
			]);
		}else{
			curl_setopt($ch, CURLOPT_HTTPHEADER, [
				'content-type: application/json'
			]);
		}

        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);           //最大相应超时时间
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
		curl_setopt ($ch,CURLOPT_FOLLOWLOCATION, false);

        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
?>  