main.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. let importType = 'originalData';
  75. // 点击导入按钮
  76. function handleImportClick(libID, type) {
  77. setCurLib(libID);
  78. importType = type;
  79. $('#import').modal('show');
  80. }
  81. // 导入确认
  82. function handleImportConfirm() {
  83. $.bootstrapLoading.start();
  84. const self = $(this);
  85. try {
  86. const formData = new FormData();
  87. const file = $("input[name='import_data']")[0];
  88. if (file.files.length <= 0) {
  89. throw '请选择文件!';
  90. }
  91. formData.append('file', file.files[0]);
  92. formData.append('libID', curLib.id);
  93. formData.append('importType', importType);
  94. $.ajax({
  95. url: '/priceInfo/importExcel',
  96. type: 'POST',
  97. data: formData,
  98. cache: false,
  99. contentType: false,
  100. processData: false,
  101. beforeSend: function () {
  102. self.attr('disabled', 'disabled');
  103. self.text('上传中...');
  104. },
  105. success: function (response) {
  106. self.removeAttr('disabled');
  107. self.text('确定导入');
  108. if (response.err === 0) {
  109. $.bootstrapLoading.end();
  110. const message = response.msg !== undefined ? response.msg : '';
  111. if (message !== '') {
  112. alert(message);
  113. }
  114. // 成功则关闭窗体
  115. $('#import').modal("hide");
  116. } else {
  117. $.bootstrapLoading.end();
  118. const message = response.msg !== undefined ? response.msg : '上传失败!';
  119. alert(message);
  120. }
  121. },
  122. error: function () {
  123. $.bootstrapLoading.end();
  124. alert("与服务器通信发生错误");
  125. self.removeAttr('disabled');
  126. self.text('确定导入');
  127. }
  128. });
  129. } catch (error) {
  130. alert(error);
  131. $.bootstrapLoading.end();
  132. }
  133. }
  134. // 导出信息价库数据
  135. function handleExportInfoPriceByLib(libID) {
  136. setCurLib(libID);
  137. window.location.href = `/priceInfo/exportInfoPriceByLib?libID=${libID}`;
  138. }
  139. // 导出编办信息价数据
  140. function handleExportInfoPriceByCompilation() {
  141. if (!compilationID) {
  142. alert('请选择编办!');
  143. return;
  144. }
  145. window.location.href = `/priceInfo/exportInfoPriceByCompilation?compilationID=${compilationID}`;
  146. }
  147. const { ProcessStatus, CRAWL_LOG_KEY } = window.PRICE_INFO_CONST;
  148. const CHECKING_TIME = 5000;
  149. // 检测爬取、导入是否完成
  150. function processChecking(key, cb) {
  151. checking();
  152. function checking() {
  153. ajaxPost('/priceInfo/processChecking', { key })
  154. .then(handleResolve)
  155. .catch(handleReject)
  156. }
  157. let timer;
  158. function handleResolve({ key, status, errorMsg }) {
  159. if (status === ProcessStatus.START) {
  160. if (!$('#progressModal').is(':visible')) {
  161. const title = key === CRAWL_LOG_KEY ? '爬取数据' : '导入数据';
  162. const text = key === CRAWL_LOG_KEY ? '正在爬取数据,请稍候……' : '正在导入数据,请稍候……';
  163. $.bootstrapLoading.progressStart(title, true);
  164. $("#progress_modal_body").text(text);
  165. }
  166. timer = setTimeout(checking, CHECKING_TIME);
  167. } else if (status === ProcessStatus.FINISH) {
  168. if (timer) {
  169. clearTimeout(timer);
  170. }
  171. $.bootstrapLoading.progressEnd();
  172. if (cb) {
  173. cb();
  174. }
  175. } else {
  176. if (timer) {
  177. clearTimeout(timer);
  178. }
  179. if (errorMsg) {
  180. alert(errorMsg);
  181. }
  182. if (cb) {
  183. cb();
  184. }
  185. $.bootstrapLoading.progressEnd();
  186. }
  187. }
  188. function handleReject(err) {
  189. if (timer) {
  190. clearInterval(timer);
  191. }
  192. alert(err);
  193. $.bootstrapLoading.progressEnd();
  194. }
  195. }
  196. const matched = window.location.search.match(/filter=(.+)/);
  197. const compilationID = matched && matched[1] || '';
  198. // 爬取数据确认
  199. function handleCrawlConfirm() {
  200. const from = $('#period-start').val();
  201. const to = $('#period-end').val();
  202. if (!periodReg.test(from) || !periodReg.test(to)) {
  203. $('#crawl-error').show();
  204. return false;
  205. }
  206. $('#crawl').modal('hide');
  207. ajaxPost('/priceInfo/crawlData', { from, to, compilationID }, 0) // 没有timeout
  208. .then(() => {
  209. processChecking(CRAWL_LOG_KEY, () => window.location.reload());
  210. })
  211. }
  212. /* function handleCrawlConfirm() {
  213. const from = $('#period-start').val();
  214. const to = $('#period-end').val();
  215. if (!periodReg.test(from) || !periodReg.test(to)) {
  216. $('#crawl-error').show();
  217. return false;
  218. }
  219. $('#crawl').modal('hide');
  220. $.bootstrapLoading.progressStart('爬取数据', true);
  221. $("#progress_modal_body").text('正在爬取数据,请稍候……');
  222. // 不用定时器的话,可能finally处理完后,进度条界面才显示,导致进度条界面没有被隐藏
  223. const time = setInterval(() => {
  224. if ($('#progressModal').is(':visible')) {
  225. clearInterval(time);
  226. ajaxPost('/priceInfo/crawlData', { from, to, compilationID }, 0) // 没有timeout
  227. .then(() => {
  228. window.location.reload();
  229. })
  230. .finally(() => $.bootstrapLoading.progressEnd());
  231. }
  232. }, 500);
  233. } */
  234. const throttleTime = 1000;
  235. $(document).ready(function () {
  236. processChecking();
  237. // 锁定、解锁
  238. $('.lock').click(function () {
  239. lockUtil.handleLockClick($(this));
  240. });
  241. // 新增
  242. $('#add-lib').click(throttle(createLib, throttleTime));
  243. // 重命名
  244. $('#rename').click(throttle(handleEditConfirm, throttleTime));
  245. // 删除
  246. $('#delete').click(handleDeleteConfirm);
  247. // 爬取数据
  248. $('#crawl-confirm').click(throttle(handleCrawlConfirm, throttleTime));
  249. // 导入excel
  250. $('#import-confirm').click(throttle(handleImportConfirm, throttleTime));
  251. $('#add').on('hidden.bs.modal', () => {
  252. $('#name-error').hide();
  253. $('#period-error').hide();
  254. });
  255. $('#edit').on('hidden.bs.modal', () => $('#rename-error').hide());
  256. $('#crawl').on('hidden.bs.modal', () => $('#crawl-error').hide());
  257. $('#export-compilation-price').click(() => {
  258. handleExportInfoPriceByCompilation();
  259. })
  260. });
  261. $.ajax({
  262. url: 'http://api.zjtcn.com/user/dyn_code',
  263. type: 'post',
  264. data: { service_id: '2020090003' },
  265. contentType: 'application/x-www-form-urlencoded',
  266. })