<?php
class Oth007
{
	public $account = null;
	public $YpwBase = null;
	public function __construct($tel = null)
	{
		$this->YpwBase = new YpwBase();
		$where = empty($tel)? " order by rand() limit 1": sprintf(" and acc_user='%s'", $tel);
		$res = $this->YpwBase->get('catch_007_account', ['*'], " and acc_status=1 ".$where, true);
		$this->account = $res['data'];
		if(empty($this->account)) return LibFc::ReturnData(false, '提供的第三方数据的账号不存在或者异常');
	}


	/**
	  * @method 获取零零汽的接口数据
	  * @param  $apiurl string [必填] 接口  如/api/brands
	  * @param  $params array [选填] 除了公共参数的参数
	  * @param  $account string [选填] 是否指定账号
	  * @author soul
	  * @copyright 2020/9/7
	  * @return 
	  */
	function getApi($apiurl, $params = [], $from = '')
	{
		$apiurl = '/'.trim($apiurl, '/');
		$res = $this->YpwBase->get('catch_007_api', ['api_id, api_method, api_cachetime'], sprintf(" and api_url='%s' and api_status = 1", LibFc::Escape($apiurl)), true);
		if(empty($res['data'])) return LibFc::ReturnData(false, '数据接口不存在或者尚未开放');
		$apiInfo = $res['data'];
		if(!is_array($params)) $params = [];

		//是否启用缓存
		$apiParams = $params;
		unset($apiParams['open_id']);unset($apiParams['apiurl']);unset($apiParams['token']);unset($apiParams['lang']);
		ksort($apiParams);
		$cachekey = md5($apiurl.'@'.json_encode($apiParams, JSON_UNESCAPED_UNICODE));

		$res = $this->YpwBase->get('catch_007_lists', ['cli_response'], sprintf(" and cli_cachekey='%s' and cli_atime > %d order by cli_id desc limit 1", $cachekey, time()-$apiInfo['api_cachetime']), true);
		if(!empty($res['data'])){
			$response = json_decode($res['data']['cli_response'], true);
			return LibFc::ReturnData(true, $response);
		}

		$url = 'https://sapi.007vin.com'.$apiurl;
		$params['open_id'] = $this->account['acc_open_id'];
		$params['token'] = md5($this->account['acc_open_id'].$this->account['acc_secret_key']);
		if($apiInfo['api_method'] == 'post'){
			$post = http_build_query($params);
		}else{
			$url = $url.'?'.http_build_query($params);
		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		if($post) curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		// 启用返回结果而不是直接输出
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// 检查证书中是否设置域名
		// 设置代理 IP 和端口
		//$proxy = '123.45.67.89:8080'; // 替换为你的代理 IP 和端口
		//curl_setopt($ch, CURLOPT_PROXY, $proxy);
		$response = curl_exec($ch);
		// 检查是否发生错误
		if (curl_errno($ch)) {
			$LibLog = new LibLog();
			$LibLog->write('007采集异常'.$url.'=>'.curl_error($ch));
			curl_close($ch);
			return LibFc::ReturnData(false, '获取第三方数据异常，请稍后重试'.$response);
		} else {
			curl_close($ch);
			$response = json_decode($response, true);
			$add = [
				'cli_acc_user'=>$this->account['acc_user'],
				'cli_api_url'=>$apiurl,	
				'cli_cachekey'=>$cachekey,	
				'cli_params'=>json_encode($apiParams, JSON_UNESCAPED_UNICODE),
				'cli_response'=>json_encode($response, JSON_UNESCAPED_UNICODE),
				'cli_atime'=>time(),
				'cli_from'=>$from
			];
			$this->YpwBase->add('catch_007_lists', $add);
			return LibFc::ReturnData(true, $response);
		}
	}
}