main.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // 节流
  2. function throttle(fn, time) {
  3. let canRun = true;
  4. return function () {
  5. if (!canRun) {
  6. return;
  7. }
  8. canRun = false;
  9. const rst = fn.apply(this, arguments);
  10. // 事件返回错误,说明没有发起请求,允许直接继续触发事件,不进行节流处理
  11. if (rst === false) {
  12. canRun = true;
  13. return;
  14. }
  15. setTimeout(() => canRun = true, time);
  16. }
  17. }
  18. const periodReg = /\d{4}-(0[1-9])|(1[0-2])$/;
  19. function createLib() {
  20. const name = $('#name').val();
  21. if (!name) {
  22. $('#name-error').show();
  23. return false;
  24. }
  25. const period = $('#period').val();
  26. if (!period || !periodReg.test(period)) {
  27. $('#period-error').show();
  28. return false;
  29. }
  30. $('#add-lib-form').submit();
  31. }
  32. let curLib = {};
  33. //设置当前库
  34. function setCurLib(libID) {
  35. curLib.id = libID;
  36. curLib.name = $(`#${libID}`).text();
  37. }
  38. // 点击编辑按钮
  39. function handleEditClick(libID) {
  40. setCurLib(libID);
  41. $('#edit').modal('show');
  42. }
  43. // 点击确认编辑
  44. function handleEditConfirm() {
  45. const rename = $('#rename-text').val();
  46. if (!rename) {
  47. $('#rename-error').show();
  48. return false;
  49. }
  50. $('#edit').modal('hide');
  51. ajaxPost('/priceInfo/renameLib', { libID: curLib.id, name: rename })
  52. .then(() => $(`#${curLib.id} a`).text(rename));
  53. }
  54. // 删除需要连需点击三次才可删除
  55. let curDelCount = 0;
  56. // 点击删除按钮
  57. function handleDeleteClick(libID) {
  58. setCurLib(libID);
  59. curDelCount = 0;
  60. $('#del').modal('show');
  61. }
  62. // 删除确认
  63. function handleDeleteConfirm() {
  64. curDelCount++;
  65. if (curDelCount === 3) {
  66. $.bootstrapLoading.start();
  67. curDelCount = -10; // 由于需要连续点击,因此没有对此事件进行节流,为防止多次请求,一旦连续点击到三次,马上清空次数。
  68. $('#del').modal('hide');
  69. ajaxPost('/priceInfo/deleteLib', { libID: curLib.id })
  70. .then(() => $(`#${curLib.id}`).parent().remove())
  71. .finally(() => $.bootstrapLoading.end());
  72. }
  73. }
  74. // 点击导入按钮
  75. function handleImportClick(libID) {
  76. setCurLib(libID);
  77. $('#import').modal('show');
  78. }
  79. // 导入确认
  80. function handleImportConfirm() {
  81. $.bootstrapLoading.start();
  82. const self = $(this);
  83. try {
  84. const formData = new FormData();
  85. const file = $("input[name='import_data']")[0];
  86. if (file.files.length <= 0) {
  87. throw '请选择文件!';
  88. }
  89. formData.append('file', file.files[0]);
  90. formData.append('libID', curLib.id);
  91. $.ajax({
  92. url: '/priceInfo/importExcel',
  93. type: 'POST',
  94. data: formData,
  95. cache: false,
  96. contentType: false,
  97. processData: false,
  98. beforeSend: function () {
  99. self.attr('disabled', 'disabled');
  100. self.text('上传中...');
  101. },
  102. success: function (response) {
  103. self.removeAttr('disabled');
  104. self.text('确定导入');
  105. if (response.err === 0) {
  106. $.bootstrapLoading.end();
  107. const message = response.msg !== undefined ? response.msg : '';
  108. if (message !== '') {
  109. alert(message);
  110. }
  111. // 成功则关闭窗体
  112. $('#import').modal("hide");
  113. } else {
  114. $.bootstrapLoading.end();
  115. const message = response.msg !== undefined ? response.msg : '上传失败!';
  116. alert(message);
  117. }
  118. },
  119. error: function () {
  120. $.bootstrapLoading.end();
  121. alert("与服务器通信发生错误");
  122. self.removeAttr('disabled');
  123. self.text('确定导入');
  124. }
  125. });
  126. } catch (error) {
  127. alert(error);
  128. $.bootstrapLoading.end();
  129. }
  130. }
  131. const matched = window.location.search.match(/filter=(.+)/);
  132. const compilationID = matched && matched[1] || '';
  133. // 爬取数据确认
  134. function handleCrawlConfirm() {
  135. const from = $('#period-start').val();
  136. const to = $('#period-end').val();
  137. if (!periodReg.test(from) || !periodReg.test(to)) {
  138. $('#crawl-error').show();
  139. return false;
  140. }
  141. $('#crawl').modal('hide');
  142. $.bootstrapLoading.progressStart('爬取数据', true);
  143. $("#progress_modal_body").text('正在爬取数据,请稍候……');
  144. // 不用定时器的话,可能finally处理完后,进度条界面才显示,导致进度条界面没有被隐藏
  145. const time = setInterval(() => {
  146. if ($('#progressModal').is(':visible')) {
  147. clearInterval(time);
  148. ajaxPost('/priceInfo/crawlData', { from, to, compilationID }, 0) // 没有timeout
  149. .then(() => {
  150. window.location.reload();
  151. })
  152. .finally(() => $.bootstrapLoading.progressEnd());
  153. }
  154. }, 500);
  155. }
  156. const throttleTime = 1000;
  157. $(document).ready(function () {
  158. // 锁定、解锁
  159. $('.lock').click(function () {
  160. lockUtil.handleLockClick($(this));
  161. });
  162. // 新增
  163. $('#add-lib').click(throttle(createLib, throttleTime));
  164. // 重命名
  165. $('#rename').click(throttle(handleEditConfirm, throttleTime));
  166. // 删除
  167. $('#delete').click(handleDeleteConfirm);
  168. // 爬取数据
  169. $('#crawl-confirm').click(throttle(handleCrawlConfirm, throttleTime));
  170. // 导入excel
  171. $('#import-confirm').click(throttle(handleImportConfirm, throttleTime));
  172. $('#add').on('hidden.bs.modal', () => {
  173. $('#name-error').hide();
  174. $('#period-error').hide();
  175. });
  176. $('#edit').on('hidden.bs.modal', () => $('#rename-error').hide());
  177. $('#crawl').on('hidden.bs.modal', () => $('#crawl-error').hide());
  178. });