tool.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. class ToolsService extends Service {
  7. // 产生验证码
  8. async captcha() {
  9. const captcha = svgCaptcha.create({
  10. size: 4, // 大小
  11. fontSize: 50, // 字体大小
  12. width: 100, // 宽
  13. height: 40, // 高
  14. bacground: '#cc9966', // 背景色
  15. });
  16. return captcha;
  17. }
  18. // 发送短信验证码
  19. async sendmessage(ctx, app, randomstr) {
  20. const message = '【吉林省就业中心】您的验证码为:' + randomstr + ',请在30分钟内完成输入,欢迎使用吉林省智慧就业企业服务平台。';
  21. const data = '?Id=300&Name=wwqcgh&Psw=jljyzx-wwqcgh&Message=' + message + '&Phone=' + ctx.query.mobile + '&Timestamp=0';
  22. // const data = '?Id=300&Name=wwqcgh&Psw=jljyzx-wwqcgh&Message=' + message + '&Phone=' + phone + '&Timestamp=0';
  23. const path = ctx.app.config.messageDir + data;
  24. const result = await app.curl(path, {
  25. method: 'GET',
  26. dataType: 'text/xml',
  27. });
  28. return result;
  29. }
  30. async spider(ctx, app) {
  31. const url = 'http://job.ccut.edu.cn/module/getindustry?start_page=1&k=&type_id=300025&count=15&start=1&_=1564563909055';
  32. const result = await app.curl(url, {
  33. method: 'GET',
  34. dataType: 'json',
  35. });
  36. // const dataarray = result.data.data;
  37. // for(let i = 0;i < dataarray.length; i++){
  38. // const dataInfo = dataarray[i];
  39. // const info_id = dataInfo.info_id;
  40. // const path = 'http://job.ccut.edu.cn/detail/industry?menu_id=&type_id=300025&id=' + info_id;
  41. // const res = await app.curl(path, {
  42. // method: 'GET',
  43. // dataType: 'json',
  44. // });
  45. // console.log(result.data);
  46. // const data = JSON.parse(response.data);
  47. // console.log(data);
  48. // // console.log(info_id);
  49. // // const browser = await puppeteer.launch({
  50. // // headless: true, // 使用无头浏览器抓取
  51. // // });
  52. // // const page = await browser.newPage();
  53. // // // 设置客户端
  54. // // await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36');
  55. // // await page.goto('http://job.ccut.edu.cn/detail/industry?menu_id=&type_id=300025&id='+info_id, {
  56. // // waitUntil: [ 'domcontentloaded' ],
  57. // // });
  58. // // await page.screenshot({ path: 'static.png' });
  59. // // const data = await page.evaluate(() => {
  60. // // var storage = {};
  61. // // // 单页爬取
  62. // // var $div = $('#data_details');
  63. // // storage = {
  64. // // title: $div.find('.dh-tit').html(),
  65. // // time: $div.find('.dh-info').html(),
  66. // // content: $div.find('.details-content').html(),
  67. // // };
  68. // // return storage;
  69. // // });
  70. // //console.log(resultInfo.data.data);
  71. // }
  72. return result.data.data;
  73. // console.log(result.data.toString('utf8',0,result.data.length));
  74. // const data = JSON.parse(result.data);
  75. // eslint-disable-next-line no-undef
  76. // const $ = cheerio.load(result);
  77. // const resultStr = [];
  78. // $('#DivInfoList').find('table').find('tr').each(function(index,ele) {
  79. // console.log(111);
  80. // var obj = {};
  81. // obj.jobName = $(this).find('td').find('a').text();
  82. // //obj.salary = $(this).find('.red').text();
  83. // resultStr.push(obj);
  84. // });
  85. // const browser = await puppeteer.launch({
  86. // headless: true, // 使用无头浏览器抓取
  87. // });
  88. // const page = await browser.newPage();
  89. // // 设置客户端
  90. // await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36');
  91. // await page.goto('http://job.ccut.edu.cn/module/getindustry?start_page=1&k=&type_id=300025&count=15&start=1&_=1564563909055', {
  92. // waitUntil: [ 'domcontentloaded' ],
  93. // });
  94. // await page.screenshot({ path: 'static.png' });
  95. // const data = await page.evaluate(() => {
  96. // var storage = [];
  97. // // 单页爬取
  98. // var $table = $('#DivInfoList table tbody');
  99. // if ($table.length) {
  100. // $table.find('tr').each(function (index, item) {
  101. // if (index !== 0) {
  102. // var $td = $(item).find('td');
  103. // storage.push({
  104. // title: $td.eq(1).find('a').html(),
  105. // time: $td.eq(2).html(),
  106. // });
  107. // }
  108. // });
  109. // }
  110. // return storage;
  111. // });
  112. // console.log(data);
  113. // await browser.close();
  114. // return JSON.stringify(data);
  115. }
  116. }
  117. module.exports = ToolsService;