sync.js 4.6 KB

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