<?php
defined('BASEPATH') or exit('No direct script access allowed');
require_once('ApiBase.php');

class ConveySample extends ApiBase
{
    protected $isLogin = false; // 不需要登录信息

    public function getDeptSample(){
		$samples = $this->input->post('sample');
		if(!$samples){
			$this->wrong('参数错误');
		}
        $url = $this->config->item('api_url2')."api/v1/ConveySample/getDeptSample";
		$ret = $this->curl_post($url,['sample' => $samples]);
		if(empty($ret)){
			$this->wrong('请求失败');
		}
		header('Content-Type:application/json; charset=utf-8');
		exit($ret);
    }

	public function sampleReceive(){
		$samples = $this->input->post('sample');
		if(!$samples){
			$this->wrong('参数错误');
		}
		$url = $this->config->item('api_url2')."api/v1/ConveySample/sampleReceive";
		$ret = $this->curl_post($url,['sample' => $samples]);
		if(empty($ret)){
			$this->wrong('请求失败');
		}
		header('Content-Type:application/json; charset=utf-8');
		exit($ret);
	}

	public function cancelSample(){
		$samples = $this->input->post('sampleCode');
		if(!$samples){
			$this->wrong('参数错误');
		}
		$url = $this->config->item('api_url2')."api/v1/ConveySample/cancelSample";
		$ret = $this->curl_post($url,['sampleCode' => $samples]);
		if(empty($ret)){
			$this->wrong('请求失败');
		}
		header('Content-Type:application/json; charset=utf-8');
		exit($ret);
	}


	function curl_post($url, $data) {
		$ch = curl_init ();
		$header = array ("Accept-Charset: utf-8",'Expect:' );
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
		curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
		curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
		curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
		// 最好加上http_build_query 转换,防止有些服务器不兼容
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query ( $data ) );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
		$result = curl_exec ( $ch );
		curl_close ( $ch );
		return $result;
	}


	public function ok($data=array(),$msg='成功',$isNull=0,$isObject=0){
		$ret = array(
			'message' => $msg,
			'data' => null,
			'code' => 0
		);
		if($data && is_array($data)){
			if($isNull == 0){
				$ret['data'] = $this->change_null(array_change_line_to_hump($data));
			}else{
				$ret['data'] = array_change_line_to_hump($data);
			}
		}else{
			$ret['data'] = $data;
		}
		header('Content-Type:application/json; charset=utf-8');
		if($isObject == 1 && empty($data)){
			exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
		}else{
			exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
		}
	}

	public function wrong($msg = '错误',$code = 1,$data=array(),$isNull=0,$isObject=0){
		if($code == 0){
			$code = 1; // 一般错误
		}
		$ret = array(
			'message' => $msg,
			'data' => null,
			'code' => $code
		);
		if($data){
			if($isNull == 0){
				$ret['data'] = $this->change_null(array_change_line_to_hump($data));
			}else{
				$ret['data'] = array_change_line_to_hump($data);
			}
		}
		header('Content-Type:application/json; charset=utf-8');
		if($isObject == 1 && empty($data)){
			exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
		}else{
			exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
		}
	}

}