tool.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* eslint-disable strict */
  2. const Service = require('egg').Service;
  3. const svgCaptcha = require('svg-captcha');
  4. // const cheerio = require('cheerio');
  5. // const puppeteer = require('puppeteer');
  6. const charset = require('superagent-charset');
  7. const superagent = charset(require('superagent'));
  8. const cheerio = require('cheerio');
  9. const fs = require('fs');
  10. const { CrudService } = require('naf-framework-mongoose/lib/service');
  11. class ToolsService extends CrudService {
  12. // 产生验证码
  13. async captcha() {
  14. const captcha = svgCaptcha.create({
  15. size: 4, // 大小
  16. fontSize: 50, // 字体大小
  17. width: 100, // 宽
  18. height: 40, // 高
  19. bacground: '#cc9966', // 背景色
  20. });
  21. return captcha;
  22. }
  23. // 发送短信验证码
  24. async sendmessage(ctx, app, randomstr) {
  25. const message = '【吉林省就业中心】您的验证码为:' + randomstr + ',请在30分钟内完成输入,欢迎使用吉林省智慧就业企业服务平台。';
  26. const data = '?Id=300&Name=wwqcgh&Psw=jljyzx-wwqcgh&Message=' + message + '&Phone=' + ctx.query.mobile + '&Timestamp=0';
  27. // const data = '?Id=300&Name=wwqcgh&Psw=jljyzx-wwqcgh&Message=' + message + '&Phone=' + phone + '&Timestamp=0';
  28. const path = ctx.app.config.messageDir + data;
  29. const result = await app.curl(path, {
  30. method: 'GET',
  31. dataType: 'text/xml',
  32. });
  33. return result;
  34. }
  35. // async creeper() {
  36. // // 目标链接 吉林省人事考试网第一页
  37. // const targetUrl = 'http://www.jlzkb.com/cms/root/ksbmList.vm?dir=L-iAg-ivleaKpeWQjS_kuovkuJrljZXkvY3mi5vogZjogIPor5U&page=1&rows=10';
  38. // const columnTitle = '吉林省事业单位招聘';
  39. // // 查看是否有这个栏目,没有则创建一个
  40. // let column = await this.service.column.model.find({ news_type: '0', title: columnTitle });
  41. // if (column.length === 0) {
  42. // column = await this.service.column.model.create({ site: '99991', news_type: '0', title: columnTitle, type: '', parent_id: '', parent: '', is_use: '' });
  43. // } else {
  44. // column = column[0];
  45. // }
  46. // await this.creeperCreate(targetUrl, column);
  47. // }
  48. // // 输入路径返回文本
  49. // async creeperCreate(targetUrl, column) {
  50. // // 目标链接 吉林省人事考试网第一页
  51. // // 用来暂时保存解析到的内容和图片地址数据
  52. // const hrefOld = targetUrl;
  53. // const hrefAdd = 'http://www.jlzkb.com/cms/root/';
  54. // const uri = 'http://www.jlzkb.com';
  55. // // const imgs = [];
  56. // // 创建附件文件夹(暂定直接跳到该网站下载)
  57. // // this.mkdir('./attachment');
  58. // // 发起请求
  59. // superagent.get(targetUrl).charset('utf-8').buffer(true)
  60. // .end((error, res) => {
  61. // if (error) { // 请求出错,打印错误,返回
  62. // console.log(error);
  63. // return;
  64. // }
  65. // // cheerio需要先load html
  66. // const $ = cheerio.load(res.text);
  67. // // 循环列表,获取标题、a标签路径、日期,然后根据a标签路径再次进行爬出内容、保存即可
  68. // $('#DivInfoList tr').each((index, element) => {
  69. // // 这些数据都是存放在news中的
  70. // const title = $(element).find('td a').attr('title');
  71. // const thisHref = $(element).find('td a').attr('href');
  72. // const time = $(element).find('td[width="12%"]').text();
  73. // const publishTime = time.substring(1, 11);
  74. // // 这里可以给时间做判断当前日期(如果需要的话可以做为判断条件)
  75. // const nowDate = new Date().toLocaleDateString();
  76. // if (publishTime !== nowDate) {
  77. // // 为undefined时,不需要进行下一步了
  78. // if (thisHref !== undefined) {
  79. // const thisAllHref = hrefAdd + thisHref;
  80. // // 请求内容
  81. // superagent.get(thisAllHref).charset('utf-8').buffer(true)
  82. // .end((error, res) => {
  83. // if (error) { // 请求出错,打印错误,返回
  84. // console.log(error);
  85. // return;
  86. // }
  87. // const $ = cheerio.load(res.text);
  88. // // 获取内容保存
  89. // const content = $('#fontzoom').children('p').clone();
  90. // content.find(':nth-child(n)').remove();
  91. // const attachment = [];
  92. // // 每页都查询是否有附件存在,如果有,下载到本地,保存即可
  93. // $('#fontzoom p a').each((index, element) => {
  94. // const thisHref = $(element).attr('href');
  95. // if (thisHref.substring(0, 4) !== 'http') {
  96. // const url = uri + thisHref;
  97. // const fileName = $(element).text();
  98. // // const filepath = this.downloadAttachment(url, fileName);
  99. // const file = {
  100. // name: fileName,
  101. // uri: url,
  102. // };
  103. // attachment.push(file);
  104. // }
  105. // });
  106. // const news = this.service.news.model.create({
  107. // site: column.site,
  108. // title,
  109. // pic: '',
  110. // content,
  111. // type: '',
  112. // parent_id: column.id,
  113. // parent: column.title,
  114. // publish_time: publishTime,
  115. // attachment,
  116. // is_use: '0'
  117. // });
  118. // });
  119. // }
  120. // }
  121. // });
  122. // // 点击下一页
  123. // const href = $('#DivPageControl a').eq(2).attr('href');
  124. // const hrefNew = hrefAdd + href;
  125. // // 第一次路径与第二次路径比较,相同,就不需要调自己了
  126. // console.log(hrefNew);
  127. // console.log(hrefOld);
  128. // if (hrefNew !== hrefOld) {
  129. // // this.creeperCreate(hrefNew, column);
  130. // // over
  131. // }
  132. // });
  133. // }
  134. // // 创建目录
  135. // async mkdir(_path) {
  136. // if (fs.existsSync(_path)) {
  137. // console.log(`${_path}目录已存在`);
  138. // } else {
  139. // fs.mkdir(_path, error => {
  140. // if (error) {
  141. // return console.log(`创建${_path}目录失败`);
  142. // }
  143. // console.log(`创建${_path}目录成功`);
  144. // });
  145. // }
  146. // }
  147. // // -------------------------------------------------------这里应返回路径供给前台下载(图片存放问题一并解决)-----------------------------------------------------
  148. // // 下载爬到的附件
  149. // async downloadAttachment(thisHref, fileName) {
  150. // // 下载图片存放到指定目录
  151. // const stream = fs.createWriteStream('./attachment/' + fileName);
  152. // const req = superagent.get(thisHref); // 响应流
  153. // req.pipe(stream);
  154. // console.log(thisHref);
  155. // console.log(fileName);
  156. // return '这里返回路径保存即可';
  157. // // console.log('开始下载>>>>>>>>>>>>>>');
  158. // }
  159. }
  160. module.exports = ToolsService;