sync.js 4.7 KB

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