<?php
    include_once("jssdk.php");
    include_once("../config.php");

    $jssdk = new JSSDK($appId, $appSecret);
    $access_token = $jssdk->getAccessToken();

	$data=$_POST['data'];
	$d = json_decode($data,true);
	
	$mediaid=$d['img'];
	$img=downlodimg($access_token,$mediaid);
	$d['img']=$img;
	
	$data = json_encode($d,JSON_UNESCAPED_UNICODE);
	$ll='[{"user":"'.$_SESSION['admin'].'","time":"'.date('Y-m-d H:i:s').'","act":"wx新增","new":'.$data.'}]';
	$sjk = 'xinxizichan';
	$zcbh=$d['zcbh'];
	$xlh=$d['xlh'];
	if(!empty($zcbh)){
		$sql = "select zcbh from $sjk where zcbh='$zcbh'";
		$requ = mysqli_query($con,$sql);
		if(mysqli_num_rows($requ)){
			die('{"status":"0","msg":"资产编号重复"}');
		}
	}
	$sql = "select xlh from $sjk where xlh='$xlh'";
	$requ = mysqli_query($con,$sql);
	if(mysqli_num_rows($requ)){
		die('{"status":"0","msg":"序列号重复"}');
	}
	$zclx=$d['zclx'];
	$zczt=$d['zczt'];
	$bm=$d['bm'];
	$bgr=$d['bgr'];
	$dz=$d['dz'];
	$cgsj=strtotime($d['cgsj']);
	$rzsj=strtotime($d['rzsj']);
	$zbsc=$d['zbsc'];
	$sysc=$d['sysc'];
	$pp=$d['pp'];
	$xh=$d['xh'];
	$zcly=$d['zcly'];
	$zcjz=$d['zcjz'];
	$gg=$d['gg'];
	$bz=$d['bz'];
	//$mediaid=$d->img;
	//$img=downlodimg($access_token,$mediaid);
	$wlbs=$d['wlbs'];
	$ip=$d['ip'];
	$xsq=$d['xsq'];
	$yp=$d['yp'];
	$nc=$d['nc'];
	$cw=$d['cw'];
	if(empty($wlbs)){$wlbs=0;}
	if(empty($yp)){$yp=0;}
	if(empty($nc)){$nc=0;}
	$sql = "insert into $sjk (zcbh,xlh,zclx,cw,zczt,bm,bgr,dz,cgsj,rzsj,zbsc,sysc,pp,xh,zcly,zcjz,bz,img,gg,ll,wlbs,ip,xsq,yp,nc) values 
							 ('$zcbh','$xlh',$zclx,'$cw',$zczt,$bm,'$bgr','$dz',$cgsj,$rzsj,$zbsc,$sysc,'$pp','$xh','$zcly',$zcjz,'$bz','$img','$gg','$ll',$wlbs,'$ip','$xsq',$yp,$nc)";

	mysqli_query($con,$sql);
	if(mysqli_insert_id($con)){
		$result='{"status":"1"}';
	}else{
		$result='{"status":"0","msg":"添加失败，请重试"}';
	}
	//$_SESSION['addlishi']=$data;

    echo $result;

    function downlodimg($access_token,$serverId){
         $targetName="../uploads/$serverId.jpg";
         if(!file_exists($targetName)){
             $url="https://api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$serverId";
             $ch=curl_init($url);
             $fp=fopen($targetName, 'wb');
             curl_setopt($ch, CURLOPT_FILE, $fp);
             curl_setopt($ch, CURLOPT_HEADER, 0);
             curl_exec($ch);
             curl_close($ch);
             fclose($fp);
         }
        $targetName = substr($targetName, 2);
        return  $targetName;
    }

?>