sync.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var sync_db_list = [
  2. { script: "sync/table_article.php", count: -1, finished: 0, enable: false },
  3. { script: "sync/table_term.php", count: -1, finished: 0, enable: false },
  4. { script: "sync/table_article_collect.php", count: -1, finished: 0, enable: false },
  5. { script: "sync/table_sentence.php", count: -1, finished: 0, enable: true },
  6. ];
  7. var isStop = false;
  8. var sync_curr_do_db = 0;
  9. function sync_index_init() {
  10. render_progress();
  11. }
  12. function sync_pull() {
  13. sync_curr_do_db = 0;
  14. isStop = false;
  15. $("#sync_log").html("working"); //
  16. sync_do_db($("#sync_server_address").val(), $("#sync_local_address").val(), 1);
  17. }
  18. function sync_push() {
  19. isStop = false;
  20. sync_curr_do_db = 0;
  21. $("#sync_log").html("working"); //
  22. sync_do_db($("#sync_local_address").val(), $("#sync_server_address").val(), 1);
  23. }
  24. function sync_stop() {
  25. isStop = true;
  26. }
  27. function sync_do_db(src, dest, time = 1) {
  28. let size = 500;
  29. while (sync_db_list[sync_curr_do_db].enable == false) {
  30. sync_curr_do_db++;
  31. if (sync_curr_do_db >= sync_db_list.length) {
  32. $("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
  33. return;
  34. }
  35. }
  36. if (sync_db_list[sync_curr_do_db].count < 0) {
  37. $.get(
  38. "sync.php",
  39. {
  40. server: src,
  41. localhost: dest,
  42. path: sync_db_list[sync_curr_do_db].script,
  43. time: time,
  44. size: -1,
  45. },
  46. function (data) {
  47. let result;
  48. try {
  49. result = JSON.parse(data);
  50. sync_db_list[sync_curr_do_db].count = parseInt(result.data);
  51. sync_do_db(src, dest, time);
  52. } catch (error) {
  53. console.error(error + " data:" + data);
  54. return;
  55. }
  56. }
  57. );
  58. } else {
  59. $.get(
  60. "sync.php",
  61. {
  62. server: src,
  63. localhost: dest,
  64. path: sync_db_list[sync_curr_do_db].script,
  65. time: time,
  66. size: size,
  67. },
  68. function (data) {
  69. let result;
  70. try {
  71. result = JSON.parse(data);
  72. } catch (error) {
  73. console.error(error + " data:" + data);
  74. return;
  75. }
  76. $("#sync_log").html(
  77. $("#sync_log").html() +
  78. "<div><h2>" +
  79. sync_db_list[sync_curr_do_db].script +
  80. "</h2>" +
  81. result.message +
  82. "</div>"
  83. ); //
  84. render_progress();
  85. if (isStop) {
  86. return;
  87. }
  88. sync_db_list[sync_curr_do_db].finished += parseInt(result.src_row);
  89. if (result.src_row >= size) {
  90. //没弄完,接着弄
  91. sync_do_db(src, dest, result.time);
  92. } else {
  93. sync_curr_do_db++;
  94. if (sync_curr_do_db < sync_db_list.length) {
  95. while (sync_db_list[sync_curr_do_db].enable == false) {
  96. sync_curr_do_db++;
  97. if (sync_curr_do_db >= sync_db_list.length) {
  98. $("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
  99. return;
  100. }
  101. }
  102. sync_do_db(src, dest, 1);
  103. } else {
  104. $("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
  105. }
  106. }
  107. }
  108. );
  109. }
  110. }
  111. function render_progress() {
  112. let html = "";
  113. for (const iterator of sync_db_list) {
  114. let spanWidth = parseInt((500 * iterator.finished) / iterator.count);
  115. html +=
  116. "<div style='width:500px;background-color:white;color:black;'><span style='background-color:green;display:inline-block;width:" +
  117. spanWidth +
  118. "px;'>" +
  119. iterator.script +
  120. "|" +
  121. iterator.finished +
  122. "/" +
  123. iterator.count +
  124. "<span></div>";
  125. }
  126. $("#sync_result").html(html);
  127. }
  128. function login() {
  129. $("#server_msg").html("正在登录<br>");
  130. $.post(
  131. "login.php",
  132. {
  133. userid: $("#userid").val(),
  134. password: $("#password").val(),
  135. server: $("#sync_server_address").val(),
  136. },
  137. function (data) {
  138. let result = JSON.parse(data);
  139. $("#server_msg").html(result.message);
  140. }
  141. );
  142. }