JTool.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. let exportObj = {
  3. url : '',
  4. protocol : '',
  5. origin : '',
  6. url_relative : '',
  7. $ : null,
  8. /**
  9. * 展开td的colspan
  10. * @author benzhan
  11. */
  12. fixColspan($table) {
  13. let $ = this.$;
  14. let $trs = $table.find('tr');
  15. for (let i = 0; i < $trs.length; i++) {
  16. let $tr = $($trs[i]);
  17. let $tds = $tr.find('td[colspan]');
  18. for (let j = 0; j < $tds.length; j++) {
  19. let $td = $($tds[j]);
  20. let span = $td.attr('colspan');
  21. for (let k = 1; k < span; k++) {
  22. $td.after('<td></td>');
  23. }
  24. $td.removeAttr('colspan');
  25. }
  26. }
  27. },
  28. formatDate: function(date) {
  29. let y, m, d = '01';
  30. if (typeof date === 'string') {
  31. let parts = date.match(/[\d]{4}-[\d]{1,2}-[\d]{1,2}/);
  32. if (parts && parts.length) {
  33. return parts[0];
  34. }
  35. let parts1 = date.match(/([\d]{4})[\s]*年/);
  36. let parts2 = date.match(/([\d]{1,2})[\s]*月/);
  37. let parts3 = date.match(/([\d]{1,2})[\s]*日/);
  38. if ((parts1 && parts1.length || parts3 && parts3.length) && parts2.length ) {
  39. y = parseInt(parts1[1]);
  40. m = parseInt(parts2[1]);
  41. d = parseInt(parts3 && parts3[1]) || 1;
  42. } else {
  43. return '';
  44. }
  45. } else {
  46. if (typeof date === 'number') {
  47. date = new Date(date);
  48. }
  49. y = date.getFullYear();
  50. m = date.getMonth() + 1;
  51. d = date.getDate();
  52. }
  53. m = m < 10 ? '0' + m : m;
  54. d = d < 10 ? ('0' + d) : d;
  55. return y + '-' + m + '-' + d;
  56. },
  57. formatDateTime: function(date) {
  58. let dateStr = this.formatDate(date);
  59. let h, minute, second = '00';
  60. if (typeof date === 'string') {
  61. let parts = date.match(/[\d]{1,2}:[\d]{1,2}(:[\d]{1,2})?/);
  62. if (parts && parts.length) {
  63. return dateStr + ' ' + parts[0];
  64. }
  65. let parts1 = date.match(/([\d]{1,2})[\s]*时/);
  66. let parts2 = date.match(/([\d]{1,2})[\s]*分/);
  67. let parts3 = date.match(/([\d]{1,2})[\s]*时/);
  68. if (parts1 && parts1.length && parts2 && parts2.length) {
  69. h = parseInt(parts1[1]);
  70. minute = parseInt(parts2[1]);
  71. second = parseInt(parts3 && parts3[1]) || 0;
  72. } else {
  73. return dateStr;
  74. }
  75. } else {
  76. if (typeof date === 'number') {
  77. date = new Date(date);
  78. }
  79. h = date.getHours();
  80. second = date.getSeconds();
  81. minute = date.getMinutes();
  82. }
  83. h = h < 10 ? ('0' + h) : h;
  84. minute = minute < 10 ? ('0' + minute) : minute;
  85. second = second < 10 ? ('0' + second) : second;
  86. return dateStr + ' ' + h + ':' + minute + ':' + second;
  87. },
  88. initUrl(url) {
  89. this.url = url;
  90. let parts = url.match(/(http[s]?:)\/\/[^\/]+[\/]?/);
  91. this.origin = parts[0];
  92. this.protocol = parts[1];
  93. this.url_relative = url.substr(0, url.lastIndexOf('/') + 1);
  94. },
  95. initJquery($) {
  96. this.$ = $;
  97. },
  98. formatUrl(href) {
  99. href = href.trim();
  100. let origin = this.origin;
  101. if (/http[s]?:\/\//.test(href)) {
  102. return href;
  103. } else if (href.substr(0, 2) === '//') {
  104. return this.protocol + href;
  105. } else if (href[0] === '/') {
  106. if (origin[origin.length - 1] === '/') {
  107. return origin + href.substr(1);
  108. } else {
  109. return origin + href;
  110. }
  111. } else {
  112. return this.url_relative + href;
  113. }
  114. },
  115. formatRichText(content) {
  116. let $ = this.$;
  117. let $content = $('<div>' + content.trim() + '</div>');
  118. $content.find('script').remove();
  119. $content.find('img').each(function() {
  120. let formatSrc = exportObj.formatUrl($(this).attr('src'));
  121. $(this).attr('src', formatSrc);
  122. });
  123. $content.find('a').each(function() {
  124. let href = $(this).attr('href');
  125. if (href) {
  126. let formatHref = exportObj.formatUrl(href);
  127. $(this).attr('href', formatHref);
  128. }
  129. $(this).attr('rel', 'nofollow');
  130. });
  131. return $content.html();
  132. },
  133. md5(text) {
  134. let crypto = require('crypto');
  135. return crypto.createHash('md5').update(text).digest('hex');
  136. },
  137. setNextTime(task_id, interval) {
  138. const php = require('phpjs');
  139. const TableHelper = require('../framework/lib/TableHelper.js');
  140. // next_crawl_time = 0;
  141. const where = {task_id};
  142. // 修改最后更新时间
  143. let last_crawl_time = php.date('Y-m-d H:i:s');
  144. let next_crawl_time = php.time() + interval;
  145. const objTask = new TableHelper('task', 'crawl');
  146. objTask.updateObject({last_crawl_time, next_crawl_time}, where);
  147. }
  148. }
  149. // 兼容老版本
  150. exportObj.formaRichText = exportObj.formatRichText;
  151. module.exports = exportObj;