function.php 7.9 KB

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