function.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. require_once "../public/function.php";
  3. require_once '../redis/function.php';
  4. function do_sync($param)
  5. {
  6. $redis=redis_connect();
  7. if($redis){
  8. $key = $redis->hget("sync://key",$_POST["userid"]);
  9. if($key===FALSE){
  10. return false;
  11. }
  12. else{
  13. if($key!=$_POST["sync_key"]){
  14. return false;
  15. }
  16. }
  17. }
  18. else{
  19. return false;
  20. }
  21. if (isset($_GET["op"])) {
  22. $op = $_GET["op"];
  23. } else if (isset($_POST["op"])) {
  24. $op = $_POST["op"];
  25. } else {
  26. echo "error: no op";
  27. return (false);
  28. }
  29. $PDO = new PDO("" . $param->database, "", "", array(PDO::ATTR_PERSISTENT => true));
  30. $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  31. switch ($op) {
  32. case "sync":
  33. {
  34. if(isset($_POST["size"])){
  35. $size=intval($_POST["size"]);
  36. }
  37. else{
  38. $size=0;
  39. }
  40. if($size>2000){
  41. $size=2000;
  42. }
  43. if(isset($_POST["time"])){
  44. $time = $_POST["time"];
  45. $query = "SELECT {$param->uuid} as guid, {$param->modify_time} as modify_time from {$param->table} where {$param->modify_time} > ? order by {$param->modify_time} ASC limit 0,".$size;
  46. $stmt = $PDO->prepare($query);
  47. $stmt->execute(array($time));
  48. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  49. if(count($Fetch)>0){
  50. $newTime = $Fetch[count($Fetch)-1]["modify_time"];
  51. $query = "SELECT {$param->uuid} as guid, {$param->modify_time} as modify_time from {$param->table} where {$param->modify_time} > ? and {$param->modify_time} <= ? order by {$param->modify_time} ASC ";
  52. $stmt = $PDO->prepare($query);
  53. $stmt->execute(array($time,$newTime));
  54. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  55. }
  56. echo (json_encode($Fetch, JSON_UNESCAPED_UNICODE));
  57. }
  58. else if(isset($_POST["id"])){
  59. $params = json_decode($_POST["id"],true);
  60. $count =count($params);
  61. /* 创建一个填充了和params相同数量占位符的字符串 */
  62. $place_holders = implode(',', array_fill(0, count($params), '?'));
  63. $query = "SELECT {$param->uuid} as guid, {$param->modify_time} from {$param->table} where {$param->uuid} in ($place_holders) limit 0,".$size;
  64. $stmt = $PDO->prepare($query);
  65. $stmt->execute($params);
  66. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  67. $iFetch = count($Fetch);
  68. echo (json_encode($Fetch, JSON_UNESCAPED_UNICODE));
  69. }
  70. break;
  71. }
  72. case "get":
  73. {
  74. if (isset($_GET["id"])) {
  75. $id = $_GET["id"];
  76. } else if (isset($_POST["id"])) {
  77. $id = $_POST["id"];
  78. } else {
  79. return (false);
  80. }
  81. $arrId = json_decode($id);
  82. /* 创建一个填充了和params相同数量占位符的字符串 */
  83. $place_holders = implode(',', array_fill(0, count($arrId), '?'));
  84. $query = "SELECT * FROM {$param->table} WHERE {$param->uuid} in ($place_holders)";
  85. $stmt = $PDO->prepare($query);
  86. $stmt->execute($arrId);
  87. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  88. echo (json_encode($Fetch, JSON_UNESCAPED_UNICODE));
  89. return (true);
  90. break;
  91. }
  92. case "insert":
  93. {
  94. echo "正在准备插入记录<br>";
  95. if (isset($_POST["data"])) {
  96. $data = $_POST["data"];
  97. } else {
  98. echo "没有数据<br>";
  99. return (false);
  100. }
  101. // 开始一个事务,关闭自动提交
  102. $PDO->beginTransaction();
  103. $query = "INSERT INTO {$param->table} (";
  104. foreach ($param->insert as $row) {
  105. $query .= "'" . $row . "',";
  106. }
  107. $query .= "'receive_time') VALUES ( ";
  108. for ($i = 0; $i < count($param->insert); $i++) {
  109. $query .= " ?, ";
  110. }
  111. $query .= " ? )";
  112. $arrData = json_decode($data, true);
  113. $stmt = $PDO->prepare($query);
  114. foreach ($arrData as $oneParam) {
  115. $newRow = array();
  116. foreach ($param->insert as $row) {
  117. $newRow[] = $oneParam["{$row}"];
  118. }
  119. $newRow[] = mTime();
  120. $stmt->execute($newRow);
  121. }
  122. // 提交更改
  123. $PDO->commit();
  124. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  125. $error = $PDO->errorInfo();
  126. echo "error - $error[2] <br>";
  127. return (false);
  128. } else {
  129. $count = count($arrData);
  130. echo "INSERT $count recorders." . "<br>";
  131. return (true);
  132. }
  133. break;
  134. }
  135. case "update":
  136. {
  137. echo "更在准备更新数据<br>";
  138. if (isset($_POST["data"])) {
  139. $data = $_POST["data"];
  140. } else {
  141. echo "没有输入数据<br>";
  142. return (false);
  143. }
  144. $arrData = json_decode($data, true);
  145. $query = "UPDATE {$param->table} SET ";
  146. foreach ($param->update as $row) {
  147. $query .= "{$row} = ? ,";
  148. }
  149. $query .= "{$param->receive_time} = ? where {$param->uuid} = ? ";
  150. $stmt = $PDO->prepare($query);
  151. // 开始一个事务,关闭自动提交
  152. try {
  153. $PDO->beginTransaction();
  154. foreach ($arrData as $one) {
  155. $newRow = array();
  156. foreach ($param->update as $row) {
  157. $newRow[] = $one["{$row}"];
  158. }
  159. $newRow[] = mTime();
  160. $newRow[] = $one["{$param->uuid}"];
  161. $stmt->execute($newRow);
  162. }
  163. // 提交更改
  164. $PDO->commit();
  165. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  166. $error = $PDO->errorInfo();
  167. echo "error - $error[2] <br>";
  168. return (false);
  169. } else {
  170. $count = count($arrData);
  171. echo "INSERT $count recorders." . "<br>";
  172. return (true);
  173. }
  174. } catch (Exception $e) {
  175. $PDO->rollback();
  176. echo "Failed:" . $e->getMessage() . "<br>";
  177. return (false);
  178. }
  179. break;
  180. }
  181. default:
  182. break;
  183. }
  184. }