main.js 8.8 KB

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