main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. const matched = window.location.search.match(/filter=(.+)/);
  2. const compilationID = matched && matched[1] || '';
  3. // 节流
  4. function throttle(fn, time) {
  5. let canRun = true;
  6. return function () {
  7. if (!canRun) {
  8. return;
  9. }
  10. canRun = false;
  11. const rst = fn.apply(this, arguments);
  12. // 事件返回错误,说明没有发起请求,允许直接继续触发事件,不进行节流处理
  13. if (rst === false) {
  14. canRun = true;
  15. return;
  16. }
  17. setTimeout(() => canRun = true, time);
  18. }
  19. }
  20. const periodReg = /\d{4}-((0[1-9])|(1[0-2]))$/;
  21. const quaterReg = /\d{4}年-[一二三四]{1}季度$/;
  22. function createLib() {
  23. const name = $('#name').val();
  24. if (!name) {
  25. $('#name-error').show();
  26. return false;
  27. }
  28. const period = $('#period').val();
  29. if (!period || (!periodReg.test(period) && !quaterReg.test(period))) {
  30. $('#period-error').show();
  31. return false;
  32. }
  33. $('#add-lib-form').submit();
  34. }
  35. let curLib = {};
  36. //设置当前库
  37. function setCurLib(libID) {
  38. curLib.id = libID;
  39. curLib.name = $(`#${libID}`).text();
  40. curLib.compilationID = $(`#${libID}`).data('compilationid');
  41. }
  42. // 点击编辑按钮
  43. function handleEditClick(libID) {
  44. setCurLib(libID);
  45. $('#edit').modal('show');
  46. }
  47. // 点击确认编辑
  48. function handleEditConfirm() {
  49. const rename = $('#rename-text').val();
  50. if (!rename) {
  51. $('#rename-error').show();
  52. return false;
  53. }
  54. $('#edit').modal('hide');
  55. ajaxPost('/priceInfo/renameLib', { libID: curLib.id, name: rename })
  56. .then(() => $(`#${curLib.id} a`).text(rename));
  57. }
  58. // 删除需要连需点击三次才可删除
  59. let curDelCount = 0;
  60. // 点击删除按钮
  61. function handleDeleteClick(libID) {
  62. setCurLib(libID);
  63. curDelCount = 0;
  64. $('#del').modal('show');
  65. }
  66. // 删除确认
  67. function handleDeleteConfirm() {
  68. curDelCount++;
  69. if (curDelCount === 3) {
  70. $.bootstrapLoading.start();
  71. curDelCount = -10; // 由于需要连续点击,因此没有对此事件进行节流,为防止多次请求,一旦连续点击到三次,马上清空次数。
  72. $('#del').modal('hide');
  73. ajaxPost('/priceInfo/deleteLib', { libID: curLib.id })
  74. .then(() => $(`#${curLib.id}`).parent().remove())
  75. .finally(() => $.bootstrapLoading.end());
  76. }
  77. }
  78. // 获取地区
  79. let curAreas = [];
  80. let curAreaMap = {};
  81. const getAreas = async () => {
  82. const areas = await ajaxPost('/priceInfo/getAreas', { compilationID: curLib.compilationID });
  83. return areas;
  84. }
  85. // 获取地区下拉选项映射,key为父地区、value为子地区
  86. const getAreaMap = async () => {
  87. curAreas = await getAreas();
  88. const areaMap = {};
  89. curAreas.forEach(item => {
  90. const { name } = item;
  91. if (name) {
  92. let [parent, sub] = name.split('-');
  93. if (!sub) {
  94. sub = parent;
  95. }
  96. if (!areaMap[parent]) {
  97. areaMap[parent] = [sub];
  98. } else {
  99. areaMap[parent].push(sub);
  100. }
  101. }
  102. });
  103. return areaMap;
  104. }
  105. // 根据地区选项获取地区ID
  106. const getAreaIDByOption = ($parent, $sub) => {
  107. const parentArea = $parent.val();
  108. const subArea = $sub.val();
  109. const areaItem = curAreas.find(item => [parentArea, `${parentArea}-${subArea}`].includes(item.name));
  110. console.log(areaItem);
  111. return areaItem?.ID;
  112. }
  113. // 设置地区下拉项
  114. const setAreaOption = ($parent, $sub, areaMap) => {
  115. $parent.unbind();
  116. const parentAreas = Object.keys(areaMap);
  117. $parent.empty();
  118. const parentOptionsHtml = parentAreas.map(parentArea => `<option value="${parentArea}">${parentArea}</option>`).join('');
  119. $parent.append(parentOptionsHtml);
  120. const setSubs = (subsArea) => {
  121. $sub.empty();
  122. const subOptionsHtml = subsArea.map(subArea => `<option value="${subArea}">${subArea}</option>`).join('');
  123. $sub.append(subOptionsHtml);
  124. }
  125. setSubs(areaMap[parentAreas[0]] || []);
  126. $parent.change(() => {
  127. const curParent = $parent.val();
  128. setSubs(areaMap[curParent] || []);
  129. getAreaIDByOption();
  130. });
  131. }
  132. let importType = 'originalData';
  133. const importTitleMap = {
  134. originalData: '原始数据',
  135. mainSub: '主从数据',
  136. keys: '关键字数据',
  137. };
  138. // 点击导入按钮
  139. async function handleImportClick(libID, type) {
  140. try {
  141. setCurLib(libID);
  142. importType = type;
  143. $('#import-title').text(`导入${importTitleMap[type] || '数据'}`);
  144. if (['mainSub', 'keys'].includes(importType)) {
  145. $('#import-area').show();
  146. const areaMap = await getAreaMap();
  147. setAreaOption($('#import-parent-area'), $('#import-sub-area'), areaMap);
  148. } else {
  149. $('#import-area').hide();
  150. }
  151. $('#import').modal('show');
  152. } catch (error) {
  153. alert(error.message);
  154. }
  155. }
  156. // 导入确认
  157. function handleImportConfirm() {
  158. $.bootstrapLoading.start();
  159. const self = $(this);
  160. try {
  161. const areaID = getAreaIDByOption($('#import-parent-area'), $('#import-sub-area'));
  162. if (!areaID) {
  163. throw '请选择地区!';
  164. }
  165. const formData = new FormData();
  166. const file = $("input[name='import_data']")[0];
  167. if (file.files.length <= 0) {
  168. throw '请选择文件!';
  169. }
  170. formData.append('file', file.files[0]);
  171. formData.append('libID', curLib.id);
  172. formData.append('areaID', areaID);
  173. formData.append('importType', importType);
  174. $.ajax({
  175. url: '/priceInfo/importExcel',
  176. type: 'POST',
  177. data: formData,
  178. cache: false,
  179. contentType: false,
  180. processData: false,
  181. beforeSend: function () {
  182. self.attr('disabled', 'disabled');
  183. self.text('上传中...');
  184. },
  185. success: function (response) {
  186. self.removeAttr('disabled');
  187. self.text('确定导入');
  188. if (response.err === 0) {
  189. $.bootstrapLoading.end();
  190. const message = response.msg !== undefined ? response.msg : '';
  191. if (message !== '') {
  192. alert(message);
  193. }
  194. // 成功则关闭窗体
  195. $('#import').modal("hide");
  196. } else {
  197. $.bootstrapLoading.end();
  198. const message = response.msg !== undefined ? response.msg : '上传失败!';
  199. alert(message);
  200. }
  201. },
  202. error: function () {
  203. $.bootstrapLoading.end();
  204. alert("与服务器通信发生错误");
  205. self.removeAttr('disabled');
  206. self.text('确定导入');
  207. }
  208. });
  209. } catch (error) {
  210. alert(error);
  211. $.bootstrapLoading.end();
  212. }
  213. }
  214. // 点击导出按钮
  215. async function handleExportClick(libID) {
  216. try {
  217. setCurLib(libID);
  218. $('#export').modal('show');
  219. const areaMap = await getAreaMap();
  220. setAreaOption($('#export-parent-area'), $('#export-sub-area'), areaMap);
  221. } catch (error) {
  222. alert(error.message);
  223. }
  224. }
  225. // 导出确认
  226. function handleExportConfirm() {
  227. const areaID = getAreaIDByOption($('#export-parent-area'), $('#export-sub-area'));
  228. if (!areaID) {
  229. alert('请选择地区!');
  230. return;
  231. }
  232. $('#export').modal('hide');
  233. window.location.href = `/priceInfo/export?libID=${curLib.id}&areaID=${areaID}`;
  234. }
  235. // 导出信息价库数据
  236. function handleExportInfoPriceByLib(libID) {
  237. setCurLib(libID);
  238. window.location.href = `/priceInfo/exportInfoPriceByLib?libID=${libID}`;
  239. }
  240. // 导出编办信息价数据
  241. function handleExportInfoPriceByCompilation() {
  242. if (!compilationID) {
  243. alert('请选择编办!');
  244. return;
  245. }
  246. window.location.href = `/priceInfo/exportInfoPriceByCompilation?compilationID=${compilationID}`;
  247. }
  248. const { ProcessStatus, CRAWL_LOG_KEY } = window.PRICE_INFO_CONST;
  249. const CHECKING_TIME = 5000;
  250. // 检测爬取、导入是否完成
  251. function processChecking(key, cb) {
  252. checking();
  253. function checking() {
  254. ajaxPost('/priceInfo/processChecking', { key })
  255. .then(handleResolve)
  256. .catch(handleReject)
  257. }
  258. let timer;
  259. function handleResolve({ key, status, errorMsg }) {
  260. if (status === ProcessStatus.START) {
  261. if (!$('#progressModal').is(':visible')) {
  262. const title = key === CRAWL_LOG_KEY ? '爬取数据' : '导入数据';
  263. const text = key === CRAWL_LOG_KEY ? '正在爬取数据,请稍候……' : '正在导入数据,请稍候……';
  264. $.bootstrapLoading.progressStart(title, true);
  265. $("#progress_modal_body").text(text);
  266. }
  267. timer = setTimeout(checking, CHECKING_TIME);
  268. } else if (status === ProcessStatus.FINISH) {
  269. if (timer) {
  270. clearTimeout(timer);
  271. }
  272. $.bootstrapLoading.progressEnd();
  273. if (cb) {
  274. cb();
  275. }
  276. } else {
  277. if (timer) {
  278. clearTimeout(timer);
  279. }
  280. if (errorMsg) {
  281. alert(errorMsg);
  282. }
  283. if (cb) {
  284. cb();
  285. }
  286. $.bootstrapLoading.progressEnd();
  287. }
  288. }
  289. function handleReject(err) {
  290. if (timer) {
  291. clearInterval(timer);
  292. }
  293. alert(err);
  294. $.bootstrapLoading.progressEnd();
  295. }
  296. }
  297. // 爬取数据确认
  298. function handleCrawlConfirm() {
  299. const from = $('#period-start').val();
  300. const to = $('#period-end').val();
  301. if (!(periodReg.test(from) && periodReg.test(to)) && !(quaterReg.test(from) && quaterReg.test(to))) {
  302. $('#crawl-error').show();
  303. return false;
  304. }
  305. $('#crawl').modal('hide');
  306. ajaxPost('/priceInfo/crawlData', { from, to, compilationID }, 0) // 没有timeout
  307. .then(() => {
  308. processChecking(CRAWL_LOG_KEY, () => window.location.reload());
  309. })
  310. }
  311. /* function handleCrawlConfirm() {
  312. const from = $('#period-start').val();
  313. const to = $('#period-end').val();
  314. if (!periodReg.test(from) || !periodReg.test(to)) {
  315. $('#crawl-error').show();
  316. return false;
  317. }
  318. $('#crawl').modal('hide');
  319. $.bootstrapLoading.progressStart('爬取数据', true);
  320. $("#progress_modal_body").text('正在爬取数据,请稍候……');
  321. // 不用定时器的话,可能finally处理完后,进度条界面才显示,导致进度条界面没有被隐藏
  322. const time = setInterval(() => {
  323. if ($('#progressModal').is(':visible')) {
  324. clearInterval(time);
  325. ajaxPost('/priceInfo/crawlData', { from, to, compilationID }, 0) // 没有timeout
  326. .then(() => {
  327. window.location.reload();
  328. })
  329. .finally(() => $.bootstrapLoading.progressEnd());
  330. }
  331. }, 500);
  332. } */
  333. const throttleTime = 1000;
  334. $(document).ready(function () {
  335. processChecking();
  336. // 锁定、解锁
  337. $('.lock').click(function () {
  338. lockUtil.handleLockClick($(this));
  339. });
  340. // 新增
  341. $('#add-lib').click(throttle(createLib, throttleTime));
  342. // 重命名
  343. $('#rename').click(throttle(handleEditConfirm, throttleTime));
  344. // 删除
  345. $('#delete').click(handleDeleteConfirm);
  346. // 爬取数据
  347. $('#crawl-confirm').click(throttle(handleCrawlConfirm, throttleTime));
  348. // 导出excel
  349. $('#export-confirm').click(throttle(handleExportConfirm, throttleTime));
  350. // 导入excel
  351. $('#import-confirm').click(throttle(handleImportConfirm, throttleTime));
  352. $('#add').on('hidden.bs.modal', () => {
  353. $('#name-error').hide();
  354. $('#period-error').hide();
  355. });
  356. $('#edit').on('hidden.bs.modal', () => $('#rename-error').hide());
  357. $('#crawl').on('hidden.bs.modal', () => $('#crawl-error').hide());
  358. $('#export-compilation-price').click(() => {
  359. handleExportInfoPriceByCompilation();
  360. })
  361. });