function.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. require_once "../public/function.php";
  3. require_once '../redis/function.php';
  4. function do_sync($param)
  5. {
  6. $output=array();
  7. $output["error"]=0;
  8. $output["message"]="";
  9. $output["data"]=array();
  10. $redis=redis_connect();
  11. if($redis){
  12. $key = $redis->hget("sync://key",$_POST["userid"]);
  13. if($key===FALSE){
  14. $output["error"]=1;
  15. $output["message"]="无法提取key userid:".$_POST["userid"];
  16. return $output;
  17. }
  18. else{
  19. if($key!=$_POST["key"]){
  20. $output["error"]=1;
  21. $output["message"]="key验证失败 {$key}- {$_POST["key"]} ";
  22. return $output;
  23. }
  24. }
  25. }
  26. else{
  27. $output["error"]=1;
  28. $output["message"]="redis初始化失败";
  29. return $output;
  30. }
  31. if (isset($_GET["op"])) {
  32. $op = $_GET["op"];
  33. } else if (isset($_POST["op"])) {
  34. $op = $_POST["op"];
  35. } else {
  36. $output["error"]=1;
  37. $output["message"]="无操作码";
  38. return $output;
  39. }
  40. $PDO = new PDO($param->database, "", "", array(PDO::ATTR_PERSISTENT => true));
  41. $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  42. switch ($op) {
  43. case "sync_count":
  44. if(isset($_POST["time"])){
  45. $time = $_POST["time"];
  46. $query = "SELECT count(*) as co from {$param->table} where {$param->receive_time} > ? {$param->where} ";
  47. $stmt = $PDO->prepare($query);
  48. $stmt->execute(array($time));
  49. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  50. if($Fetch){
  51. $output["data"]=(int)$Fetch["co"];
  52. }
  53. else{
  54. $output["data"]=0;
  55. }
  56. }
  57. else{
  58. $output["data"]=0;
  59. }
  60. return $output;
  61. break;
  62. case "sync":
  63. {
  64. if(isset($_POST["size"])){
  65. $size=intval($_POST["size"]);
  66. }
  67. else{
  68. $size=0;
  69. }
  70. if($size>2000){
  71. $size=2000;
  72. }
  73. if(isset($_POST["time"])){
  74. $time = $_POST["time"];
  75. $query = "SELECT {$param->receive_time} as receive_time from {$param->table} where {$param->receive_time} > ? {$param->where} order by {$param->receive_time} limit 0,".$size;
  76. $stmt = $PDO->prepare($query);
  77. $stmt->execute(array($time));
  78. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  79. #防止同一个时间有两条数据
  80. if(count($Fetch)>0){
  81. $newTime = $Fetch[count($Fetch)-1]["receive_time"];
  82. $syncId = implode(",",$param->sync_id);
  83. $query = "SELECT {$param->uuid} as guid, $syncId , {$param->modify_time} as modify_time , {$param->receive_time} as receive_time from {$param->table} where {$param->receive_time} > ? and {$param->receive_time} <= ? {$param->where} order by {$param->receive_time} ";
  84. $stmt = $PDO->prepare($query);
  85. $stmt->execute(array($time,$newTime));
  86. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  87. foreach ($Fetch as $key => $value) {
  88. # code...
  89. $arrSyncId = array();
  90. foreach ($param->sync_id as $field) {
  91. # code...
  92. $arrSyncId[]=$value[$field];
  93. }
  94. $Fetch[$key]["sync_id"] = implode("#",$arrSyncId);
  95. }
  96. }
  97. $output["data"]=$Fetch;
  98. return $output;
  99. }
  100. else if(isset($_POST["id"])){
  101. /*
  102. $params = json_decode($_POST["id"],true);
  103. $count =count($params);
  104. # 创建一个填充了和params相同数量占位符的字符串
  105. $place_holders = implode(',', array_fill(0, count($params), '?'));
  106. $query = "SELECT {$param->uuid} as guid, {$param->modify_time} as modify_time from {$param->table} where {$param->uuid} in ($place_holders) order by {$param->receive_time} ASC limit 0,".$size;
  107. $stmt = $PDO->prepare($query);
  108. $stmt->execute($params);
  109. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  110. $iFetch = count($Fetch);
  111. $output["data"]=$Fetch;
  112. return $output;
  113. */
  114. $syncId = implode(",",$param->sync_id);
  115. $arrId = json_decode($_POST["id"],true);
  116. $query = "SELECT {$param->uuid} as guid, {$syncId} , {$param->modify_time} as modify_time from {$param->table} where ";
  117. foreach ($param->sync_id as $field) {
  118. $query .= " $field = ? and";
  119. }
  120. $query .= " 1 limit 0,1";
  121. $stmt = $PDO->prepare($query);
  122. $data = array();
  123. foreach ($arrId as $id) {
  124. # code...
  125. $stmt->execute(explode("#",$id));
  126. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  127. if($Fetch){
  128. $Fetch["sync_id"] = $id;
  129. $data[]=$Fetch;
  130. }
  131. }
  132. $output["data"]=$data;
  133. return $output;
  134. }
  135. break;
  136. }
  137. case "get":
  138. {
  139. if (isset($_GET["id"])) {
  140. $id = $_GET["id"];
  141. } else if (isset($_POST["id"])) {
  142. $id = $_POST["id"];
  143. } else {
  144. return (false);
  145. }
  146. $arrId = json_decode($id,true);
  147. /* 创建一个填充了和params相同数量占位符的字符串 */
  148. $place_holders = implode(',', array_fill(0, count($arrId), '?'));
  149. $query = "SELECT * FROM {$param->table} WHERE {$param->uuid} in ($place_holders)";
  150. $stmt = $PDO->prepare($query);
  151. $stmt->execute($arrId);
  152. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  153. $output["message"]="提取数据".count($Fetch);
  154. $output["data"]=$Fetch;
  155. return $output;
  156. break;
  157. }
  158. case "insert":
  159. {
  160. if (isset($_POST["data"])) {
  161. $data = $_POST["data"];
  162. } else {
  163. $output["error"]=1;
  164. $output["message"]="没有提交数据";
  165. return $output;
  166. }
  167. // 开始一个事务,关闭自动提交
  168. $PDO->beginTransaction();
  169. $query = "INSERT INTO {$param->table} (";
  170. foreach ($param->insert as $row) {
  171. $query .= "'" . $row . "',";
  172. }
  173. $query = mb_substr($query,0,-1,"UTF-8");
  174. $query .= ") VALUES ( ";
  175. for ($i = 0; $i < count($param->insert); $i++) {
  176. $query .= " ?,";
  177. }
  178. $query = mb_substr($query,0,-1,"UTF-8");
  179. $query .= " )";
  180. $arrData = json_decode($data, true);
  181. $stmt = $PDO->prepare($query);
  182. foreach ($arrData as $oneParam) {
  183. $newRow = array();
  184. foreach ($param->insert as $row) {
  185. $newRow[] = $oneParam["{$row}"];
  186. }
  187. $stmt->execute($newRow);
  188. }
  189. // 提交更改
  190. $PDO->commit();
  191. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  192. $error = $PDO->errorInfo();
  193. $output["error"]=1;
  194. $output["message"]="error - $error[2]";
  195. return $output;
  196. } else {
  197. $count = count($arrData);
  198. $output["error"]=0;
  199. $output["message"]="INSERT $count recorders.";
  200. return $output;
  201. }
  202. break;
  203. }
  204. case "update":
  205. {
  206. if (isset($_POST["data"])) {
  207. $data = $_POST["data"];
  208. } else {
  209. $output["error"]=1;
  210. $output["message"]="没有输入数据";
  211. return $output;
  212. }
  213. $arrData = json_decode($data, true);
  214. $query = "UPDATE {$param->table} SET ";
  215. foreach ($param->update as $row) {
  216. $query .= "{$row} = ? ,";
  217. }
  218. $query = mb_substr($query,0,-1,"UTF-8");
  219. $query .= " where {$param->uuid} = ? ";
  220. $stmt = $PDO->prepare($query);
  221. // 开始一个事务,关闭自动提交
  222. try {
  223. $PDO->beginTransaction();
  224. foreach ($arrData as $one) {
  225. $newRow = array();
  226. foreach ($param->update as $row) {
  227. $newRow[] = $one["{$row}"];
  228. }
  229. $newRow[] = $one["{$param->uuid}"];
  230. $stmt->execute($newRow);
  231. }
  232. // 提交更改
  233. $PDO->commit();
  234. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  235. $error = $PDO->errorInfo();
  236. $output["error"]=1;
  237. $output["message"]="error - $error[2]";
  238. return $output;
  239. } else {
  240. $count = count($arrData);
  241. $output["error"]=0;
  242. $output["message"]="Update $count recorders.";
  243. return $output;
  244. }
  245. } catch (Exception $e) {
  246. $PDO->rollback();
  247. $output["error"]=1;
  248. $output["message"]="Failed:" . $e->getMessage();
  249. return $output;
  250. }
  251. break;
  252. }
  253. default:
  254. break;
  255. }
  256. }