main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. function getPeriodData(from, to) {
  136. const monthMap = {
  137. '1': '01月',
  138. '2': '02月',
  139. '3': '03月',
  140. '4': '04月',
  141. '5': '05月',
  142. '6': '06月',
  143. '7': '07月',
  144. '8': '08月',
  145. '9': '09月',
  146. '10': '10月',
  147. '11': '11月',
  148. '12': '12月',
  149. };
  150. if (from > to) {
  151. return null;
  152. }
  153. const reg = /(\d+)-(\d+)/;
  154. const fromMatch = from.match(reg);
  155. const fromYear = +fromMatch[1];
  156. const fromMonth = +fromMatch[2];
  157. const toMatch = to.match(reg);
  158. const toYear = +toMatch[1];
  159. const toMonth = +toMatch[2];
  160. let curYear = fromYear;
  161. let curMonth = fromMonth;
  162. const periods = [];
  163. while ((curYear <= toYear && curMonth <= toMonth) || curYear < toYear) {
  164. periods.push(`${curYear}年-${monthMap[curMonth]}`);
  165. if (curMonth === 12) {
  166. curYear++;
  167. curMonth = 1;
  168. } else {
  169. curMonth++;
  170. }
  171. }
  172. return periods;
  173. }
  174. // 导出浙江信息价数据
  175. function handleExportZheJiangConfirm() {
  176. const from = $('#zj-period-start').val();
  177. const to = $('#zj-period-end').val();
  178. if (!(periodReg.test(from) && periodReg.test(to)) && !(quaterReg.test(from) && quaterReg.test(to))) {
  179. $('#zj-error').show();
  180. return false;
  181. }
  182. const periods = getPeriodData(from, to);
  183. if (!periods) {
  184. $('#zj-error').show();
  185. return false;
  186. }
  187. $('#export-zj').modal('hide');
  188. window.location.href = `/priceInfo/exportZheJiangExcel?from=${from}&to=${to}`;
  189. }
  190. // 导出信息价库数据
  191. function handleExportInfoPriceByLib(libID) {
  192. setCurLib(libID);
  193. window.location.href = `/priceInfo/exportInfoPriceByLib?libID=${libID}`;
  194. }
  195. // 导出编办信息价数据
  196. function handleExportInfoPriceByCompilation() {
  197. if (!compilationID) {
  198. alert('请选择编办!');
  199. return;
  200. }
  201. window.location.href = `/priceInfo/exportInfoPriceByCompilation?compilationID=${compilationID}`;
  202. }
  203. const { ProcessStatus, CRAWL_LOG_KEY } = window.PRICE_INFO_CONST;
  204. const CHECKING_TIME = 5000;
  205. // 检测爬取、导入是否完成
  206. function processChecking(key, cb) {
  207. checking();
  208. function checking() {
  209. ajaxPost('/priceInfo/processChecking', { key })
  210. .then(handleResolve)
  211. .catch(handleReject)
  212. }
  213. let timer;
  214. function handleResolve({ key, status, errorMsg }) {
  215. if (status === ProcessStatus.START) {
  216. if (!$('#progressModal').is(':visible')) {
  217. const title = key === CRAWL_LOG_KEY ? '爬取数据' : '导入数据';
  218. const text = key === CRAWL_LOG_KEY ? '正在爬取数据,请稍候……' : '正在导入数据,请稍候……';
  219. $.bootstrapLoading.progressStart(title, true);
  220. $("#progress_modal_body").text(text);
  221. }
  222. timer = setTimeout(checking, CHECKING_TIME);
  223. } else if (status === ProcessStatus.FINISH) {
  224. if (timer) {
  225. clearTimeout(timer);
  226. }
  227. $.bootstrapLoading.progressEnd();
  228. if (cb) {
  229. cb();
  230. }
  231. } else {
  232. if (timer) {
  233. clearTimeout(timer);
  234. }
  235. if (errorMsg) {
  236. alert(errorMsg);
  237. }
  238. if (cb) {
  239. cb();
  240. }
  241. $.bootstrapLoading.progressEnd();
  242. }
  243. }
  244. function handleReject(err) {
  245. if (timer) {
  246. clearInterval(timer);
  247. }
  248. alert(err);
  249. $.bootstrapLoading.progressEnd();
  250. }
  251. }
  252. const matched = window.location.search.match(/filter=(.+)/);
  253. const compilationID = matched && matched[1] || '';
  254. // 爬取数据确认
  255. function handleCrawlConfirm() {
  256. const from = $('#period-start').val();
  257. const to = $('#period-end').val();
  258. if (!periodReg.test(from) || !periodReg.test(to)) {
  259. $('#crawl-error').show();
  260. return false;
  261. }
  262. $('#crawl').modal('hide');
  263. ajaxPost('/priceInfo/crawlData', { from, to, compilationID }, 0) // 没有timeout
  264. .then(() => {
  265. processChecking(CRAWL_LOG_KEY, () => window.location.reload());
  266. })
  267. }
  268. /* function handleCrawlConfirm() {
  269. const from = $('#period-start').val();
  270. const to = $('#period-end').val();
  271. if (!periodReg.test(from) || !periodReg.test(to)) {
  272. $('#crawl-error').show();
  273. return false;
  274. }
  275. $('#crawl').modal('hide');
  276. $.bootstrapLoading.progressStart('爬取数据', true);
  277. $("#progress_modal_body").text('正在爬取数据,请稍候……');
  278. // 不用定时器的话,可能finally处理完后,进度条界面才显示,导致进度条界面没有被隐藏
  279. const time = setInterval(() => {
  280. if ($('#progressModal').is(':visible')) {
  281. clearInterval(time);
  282. ajaxPost('/priceInfo/crawlData', { from, to, compilationID }, 0) // 没有timeout
  283. .then(() => {
  284. window.location.reload();
  285. })
  286. .finally(() => $.bootstrapLoading.progressEnd());
  287. }
  288. }, 500);
  289. } */
  290. const throttleTime = 1000;
  291. $(document).ready(function () {
  292. processChecking();
  293. // 锁定、解锁
  294. $('.lock').click(function () {
  295. lockUtil.handleLockClick($(this));
  296. });
  297. // 新增
  298. $('#add-lib').click(throttle(createLib, throttleTime));
  299. // 重命名
  300. $('#rename').click(throttle(handleEditConfirm, throttleTime));
  301. // 删除
  302. $('#delete').click(handleDeleteConfirm);
  303. // 爬取数据
  304. $('#crawl-confirm').click(throttle(handleCrawlConfirm, throttleTime));
  305. // 导入excel
  306. $('#import-confirm').click(throttle(handleImportConfirm, throttleTime));
  307. // 导出浙江
  308. $('#export-zj-confirm').click(throttle(handleExportZheJiangConfirm, throttleTime))
  309. $('#add').on('hidden.bs.modal', () => {
  310. $('#name-error').hide();
  311. $('#period-error').hide();
  312. });
  313. $('#edit').on('hidden.bs.modal', () => $('#rename-error').hide());
  314. $('#crawl').on('hidden.bs.modal', () => $('#crawl-error').hide());
  315. $('#export-zj').on('hidden.bs.modal', () => {
  316. $('#zj-error').hide()
  317. $('#zj-period-start').val('');
  318. $('#zj-period-end').val('');
  319. });
  320. $('#export-compilation-price').click(() => {
  321. handleExportInfoPriceByCompilation();
  322. })
  323. });
  324. $.ajax({
  325. url: 'http://api.zjtcn.com/user/dyn_code',
  326. type: 'post',
  327. data: { service_id: '2020090003' },
  328. contentType: 'application/x-www-form-urlencoded',
  329. })