sync.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. header('Content-type: application/json; charset=utf8');
  3. //查询term字典
  4. require_once "../config.php";
  5. $PDO = new PDO("" . _FILE_DB_TERM_, "", "", array(PDO::ATTR_PERSISTENT => true));
  6. $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  7. if (isset($_GET["op"])) {
  8. $op = $_GET["op"];
  9. } else if (isset($_POST["op"])) {
  10. $op = $_POST["op"];
  11. }
  12. switch ($op) {
  13. case "sync":
  14. {
  15. $time = $_POST["time"];
  16. $query = "select guid,modify_time from term where receive_time>'{$time}' limit 0,1000";
  17. $stmt = $PDO->query($query);
  18. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  19. $iFetch = count($Fetch);
  20. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  21. break;
  22. }
  23. case "get":
  24. {
  25. if (isset($_GET["id"])) {
  26. $id = $_GET["id"];
  27. } else if (isset($_POST["id"])) {
  28. $id = $_POST["id"];
  29. } else {
  30. return;
  31. }
  32. $arrId = json_decode($id);
  33. $queryId = "('";
  34. foreach ($arrId as $one) {
  35. $queryId .= $one . "','";
  36. }
  37. $queryId = substr($queryId, 0, -2) . ")";
  38. $query = "select guid, word, word_en, meaning, other_meaning, note, tag, create_time, owner, hit,language,receive_time,modify_time from term where guid in {$queryId}";
  39. $stmt = $PDO->query($query);
  40. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  41. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  42. break;
  43. }
  44. case "insert":
  45. {
  46. echo "正在准备插入记录<br>";
  47. if (isset($_POST["data"])) {
  48. $data = $_POST["data"];
  49. } else {
  50. echo "没有数据<br>";
  51. return;
  52. }
  53. $arrData = json_decode($data);
  54. // 开始一个事务,关闭自动提交
  55. $PDO->beginTransaction();
  56. $query = "INSERT INTO term ('id',
  57. 'guid',
  58. 'word',
  59. 'word_en',
  60. 'meaning',
  61. 'other_meaning',
  62. 'note',
  63. 'tag',
  64. 'create_time',
  65. 'owner',
  66. 'hit',
  67. 'language',
  68. 'receive_time',
  69. 'modify_time'
  70. ) VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  71. $stmt = $PDO->prepare($query);
  72. foreach ($arrData as $oneParam) {
  73. $param = array();
  74. $param[] = $oneParam->guid;
  75. $param[] = $oneParam->word;
  76. $param[] = $oneParam->word_en;
  77. $param[] = $oneParam->meaning;
  78. $param[] = $oneParam->other_meaning;
  79. $param[] = $oneParam->note;
  80. $param[] = $oneParam->tag;
  81. $param[] = $oneParam->create_time;
  82. $param[] = $oneParam->owner;
  83. $param[] = $oneParam->hit;
  84. $param[] = $oneParam->language;
  85. $param[] = time();
  86. $param[] = $oneParam->modify_time;
  87. $stmt->execute($param);
  88. }
  89. // 提交更改
  90. $PDO->commit();
  91. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  92. $error = $PDO->errorInfo();
  93. echo "error - $error[2] <br>";
  94. } else {
  95. $count = count($arrData);
  96. echo "INSERT $count recorders." . "<br>";
  97. }
  98. break;
  99. }
  100. case "update":
  101. {
  102. echo "更在准备更新数据<br>";
  103. if (isset($_POST["data"])) {
  104. $data = $_POST["data"];
  105. } else {
  106. echo "没有输入数据<br>";
  107. return;
  108. }
  109. $arrData = json_decode($data);
  110. // 开始一个事务,关闭自动提交
  111. try {
  112. $PDO->beginTransaction();
  113. foreach ($arrData as $one) {
  114. $query = "UPDATE term SET word='{$one->word}' ,
  115. word_en='{$one->word_en}' ,
  116. meaning='{$one->meaning}' ,
  117. other_meaning='{$one->other_meaning}' ,
  118. note='{$one->note}' ,
  119. tag='{$one->tag}' ,
  120. create_time='{$one->create_time}' ,
  121. owner='{$one->owner}' ,
  122. hit='{$one->hit}' ,
  123. language='{$one->language}' ,
  124. receive_time='" . time() . "' ,
  125. modify_time='{$one->modify_time}'
  126. where guid='{$one->guid}'";
  127. $PDO->exec($query);
  128. }
  129. // 提交更改
  130. $PDO->commit();
  131. echo "update " . count($arrData) . "<br>";
  132. } catch (Exception $e) {
  133. $PDO->rollback();
  134. echo "Failed:" . $e->getMessage() . "<br>";
  135. }
  136. break;
  137. }
  138. }