sync.js 4.1 KB

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