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