sync.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. require '../vendor/autoloader.php';
  3. require_once '../redis/function.php';
  4. $server = $_GET["server"];
  5. $localhost = $_GET["localhost"];
  6. $path=$_GET["path"];
  7. $time=$_GET["time"];
  8. $size=$_GET["size"];
  9. $message="";
  10. $output=["message"=>"","time"=>0,"src_row"=>0];
  11. $redis=redis_connect();
  12. if($redis){
  13. $sync_key = $redis->hget("sync://key",$_COOKIE["userid"]);
  14. if($sync_key===FALSE){
  15. $message.= "客户端没有钥匙"."<br>";
  16. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  17. exit;
  18. }
  19. }
  20. else{
  21. $message.= "redis连接失败"."<br>";
  22. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  23. exit;
  24. }
  25. $client = new \GuzzleHttp\Client();
  26. $response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'sync','time'=>$time,'size'=>$size,"key"=>$sync_key]]);
  27. $serverJson=$response->getBody();
  28. $serverData = json_decode($serverJson);
  29. $output["src_row"]=count($serverData);
  30. $message.= "输入时间:".$time."<br>";
  31. $message.= "src_row:".$output["src_row"]."<br>";
  32. if($output["src_row"]>0){
  33. $output["time"]=$serverData[$output["src_row"]-1]->modify_time;
  34. $message.= "最新时间:".$output["time"]."<br>";
  35. }
  36. else{
  37. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  38. exit;
  39. }
  40. $aIdList=array();
  41. foreach($serverData as $sd){
  42. $aIdList[]=$sd->guid;
  43. }
  44. $sIdlist = json_encode($aIdList, JSON_UNESCAPED_UNICODE);;
  45. // 拉 id 列表
  46. $response = $client->request('POST', $localhost.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'sync','id'=>$sIdlist,'size'=>$size,"key"=>$sync_key]]);
  47. $strLocalData = $response->getBody();
  48. $localData = json_decode($strLocalData);
  49. $localCount = count($localData);
  50. $message .= "local-row:".$localCount."<br>";
  51. $localindex=array();
  52. $insert_to_server=array();
  53. $update_to_server=array();
  54. $insert_to_local=array();
  55. $update_to_local=array();
  56. $message .= "<h3>{$path}</h3>";
  57. foreach($localData as $local){
  58. $localindex[$local->guid][0]=$local->modify_time;
  59. $localindex[$local->guid][1]=false;
  60. }
  61. foreach($serverData as $sd){
  62. if(isset($localindex[$sd->guid])){
  63. $localindex[$sd->guid][1]=true;
  64. if($sd->modify_time>$localindex[$sd->guid][0]){
  65. //服务器数据较新 server data is new than local
  66. $update_to_local[]=$sd->guid;
  67. }
  68. else if($sd->modify_time==$localindex[$sd->guid][0]){
  69. //"相同 same
  70. }
  71. else{
  72. //"服务器数据较旧 local data is new than server
  73. //$update_to_server[]=$sd->guid;
  74. }
  75. }
  76. else{
  77. //本地没有 新增 insert recorder in local
  78. $insert_to_local[]=$sd->guid;
  79. }
  80. }
  81. foreach($localindex as $x=>$x_value){
  82. if($x_value[1]==false){
  83. //服务器新增 new data in server;
  84. //$insert_to_server[]=$x;
  85. }
  86. }
  87. $syncCount = count($insert_to_server)+count($update_to_server)+count($insert_to_local)+count($update_to_local);
  88. if($syncCount==0){
  89. $message .= "与服务器数据完全一致,无需更新。<br>";
  90. }
  91. else{
  92. //start sync
  93. if(count($insert_to_server)>0){
  94. /*
  95. */
  96. $message .= "需要插入服务器".count($insert_to_server)."条记录<br>";
  97. /*
  98. $idInLocal = json_encode($insert_to_server, JSON_UNESCAPED_UNICODE);
  99. $response = $client->request('POST', $localhost.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'get','id'=>"{$idInLocal}"]]);
  100. $localData=$response->getBody();
  101. $response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'insert','data'=>"{$localData}"]]);
  102. $message .= $response->getBody()."<br>";
  103. */
  104. }
  105. if(count($update_to_server)>0){
  106. /*
  107. */
  108. /*
  109. $message .= "需要更新到服务器".count($update_to_server)."条记录<br>";
  110. $idInLocal = json_encode($update_to_server, JSON_UNESCAPED_UNICODE);
  111. $response = $client->request('POST', $localhost.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'get','id'=>"{$idInLocal}"]]);
  112. $localData=$response->getBody();
  113. $response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'update','data'=>"{$localData}"]]);
  114. $message .= $response->getBody()."<br>";
  115. */
  116. }
  117. if(count($insert_to_local)>0){
  118. $message .= "需要新增到本地".count($insert_to_local)."条记录<br>";
  119. /*
  120. $idInServer = json_encode($insert_to_local, JSON_UNESCAPED_UNICODE);
  121. $response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'get','id'=>"{$idInServer}","key"=>$sync_key]]);
  122. $serverData=$response->getBody();
  123. $response = $client->request('POST', $localhost.'/app/'.$path, ['verify' => false,'form_params'=>['op'=>'insert','data'=>"{$serverData}","key"=>$sync_key]]);
  124. $message .= $response->getBody()."<br>";
  125. */
  126. }
  127. if(count($update_to_local)>0){
  128. $message .= "需要更新到本地".count($update_to_local)."条记录<br>";
  129. /*
  130. $idInServer = json_encode($update_to_local, JSON_UNESCAPED_UNICODE);
  131. $response = $client->request('POST', $server.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'get','id'=>"{$idInServer}","key"=>$sync_key]]);
  132. $serverData=$response->getBody();
  133. $response = $client->request('POST', $localhost.'/app/'.$path,['verify' => false,'form_params'=>['op'=>'update','data'=>"{$serverData}","key"=>$sync_key]]);
  134. $message .= $response->getBody()."<br>";
  135. */
  136. }
  137. }
  138. $output["message"]=$message;
  139. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  140. ?>