sync.php 3.6 KB

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