const express = require('express') const app = express() const axios = require('axios') const query = require('./mysqler') const bodyParser = require("body-parser"); const schedule = require('node-schedule'); const data = ['3948352701','5208966720','2589432731','3917225829','3231047610','1005570772','2448184413','5667617555','1785533160','5672166950','5668213132','5357670210','5625133989','5178549320','3054615393','5973233937','5973637065','6003809445','1988127344','6017601357','6359395544','5972859195','5664785772','5305147245','6030122701','6026399343','2960359192','3280880197','2295650225','6017599835','5971792547','5669689463','5669328912','2253203771','6359373279','6031302315','5719743142','6039760659','1822890914','6030402215','6030660675','2782636765','6039367340','6361527487','6356947724']; app.use('/', express.static(__dirname + '/')) app.use(bodyParser.urlencoded({ extended: false })); app.all('*', (req, res, next) => { res.header("Access-Control-Allow-Origin", req.headers.origin); //设置来源 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); res.header("Access-Control-Max-Age", "604800000"); res.header("Access-Control-Allow-Credentials", true); res.header("X-Powered-By", ' 3.2.1'); // res.header("Content-Type", "application/json;charset=utf-8"); next(); }); const httpreq = async (msg) => { try { let {data} = await axios({ method: 'get', url: 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + msg }); let redata = { weiboname: data.data.userInfo.screen_name, weiboid: msg, weibofan: data.data.userInfo.followers_count, weibopost: data.data.userInfo.statuses_count } return redata } catch (error) { console.log(error) } } Date.prototype.format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } const guid=()=>{ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); } const retime = (msg) => { var day1 = new Date(); day1.setDate(day1.getDate() + msg); var s1 = day1.format("yyyy-MM-dd") return s1 } const inssql = (msg) => { try { let sql = "INSERT INTO `digital`(`name`, `id`, `fan`, `time`, `post`) VALUES ('"+msg.weiboname+"', '"+msg.weiboid+"', '"+msg.weibofan+"', NOW(), '"+msg.weibopost+"');" query(sql, [1], function(err,results,fields){ }); } catch (error) { console.log(error) } } const select = (t1, t2, callback) => { try { let time1 = retime(t1) let time2 = retime(t2) let sql = "SELECT * FROM digital WHERE time BETWEEN '"+ time1 +"' and '"+ time2 +"';" query(sql, [1], function(err,results,fields){ callback(results) }); } catch (error) { console.log(error) } } const selintime = (msg, callback) => { try { let sql = "select * from digital where DATE(time) = '"+ msg +"';" query(sql, [1], function(err,results,fields){ callback(results) }); } catch (error) { } } const getlastmonth = () => { var nowdays = new Date(); var year = nowdays.getFullYear(); var month = nowdays.getMonth(); if(month==0) { month=12; year=year-1; } if (month < 10) { month = "0" + month; } var myDate = new Date(year, month, 0); var lastDay = year + "-" + month + "-" + myDate.getDate() return lastDay } const selintimeer = (callback) => { try { let lastmonth = getlastmonth() let sql = "SELECT * FROM digital WHERE DATE(time) = '"+ lastmonth +"' OR DATE_FORMAT( time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ORDER BY id;" // let sql = "SELECT * FROM `digital` WHERE DATE_FORMAT( time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ORDER BY id;" query(sql, [1], function(err,results,fields){ callback(results) }); } catch (error) { } } const selintimeer2 = (tm1, tm2, callback) => { try { let sql = "select * from `digital` where time BETWEEN '"+tm1+"' and '"+tm2+"' ORDER BY id;" query(sql, [1], function(err,results,fields){ callback(results) }); } catch (error) { } } const start = async () => { try{ let sql = "select * from WBAccountNumber where WbState='1' order by AddTime desc" query(sql, [1], function(err,results,fields){ results.map(async (i)=>{ let data = await httpreq(i.WbNumber) inssql(data) }) }); } catch(error) { } // data.map(async(i) => { // let data = await httpreq(i) // inssql(data) // }) } const getyue = (tm1, tm2, tm, callback) => { try{ let sql = "SELECT * FROM digital WHERE DATE(time) = '"+tm+"' or year(time) = '"+tm1+"' and month(time) = '"+tm2+"' ORDER BY id" query(sql, [1], function(err,results,fields){ callback(results) }); } catch(error) { } } const getdata = (callback) => { try { let lastmonth = getlastmonth() let sql = "SELECT b.* FROM WBAccountNumber as a INNER JOIN digital as b on a.WbNumber = b.id where WbState = 1 AND DATE_FORMAT( b.time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) or DATE(b.time) = '"+lastmonth+"' ORDER BY b.id, b.time;" query(sql, [1], function(err,results,fields){ callback(results) }); } catch (err) { } } const zhiding = (tim1, time2, callback) => { try { let lastmonth = getlastmonth() let sql = "SELECT b.* FROM WBAccountNumber as a INNER JOIN digital as b on a.WbNumber = b.id where WbState = 1 AND year(b.time) = '"+tim1+"' and month(b.time) = '"+time2+"' or DATE(b.time) = '"+lastmonth+"' ORDER BY b.id, b.time;" query(sql, [1], function(err,results,fields){ callback(results) }); } catch(err) { } } // setInterval(()=>{ // start() // }, 24*60*60*1000) app.get('/getIndex', async (req, res) => { select(0, 1, (msg1)=>{ select(1, 2, (msg2)=>{ res.send({ d1: msg1, d2: msg2 }) }) }) }) app.get('/gettime', (req, res) => { let time = req.query.time; selintime(time,(results)=> { res.send(results) }) }) app.get('/all', (req, res) => { selintimeer((results)=> { res.send(results) }) }) app.get('/setime', (req, res) => { let time1 = req.query.starttime; let time2 = req.query.endtime; selintimeer2(time1,time2,(msg)=>{ res.send(msg) }) }) app.get('/getyue', (req, res) => { let time2 = req.query.time.split('-'); let time = req.query.lasttime; getyue(time2[0], time2[1], time, (msg) => { res.send(msg) }) }) // app.get('/selectall',(req,res)=>{ let sql="select * from WBAccountNumber where WbState='1' order by AddTime desc"; query(sql,(error, results, fields)=> { if (error){ res.json('error') }else{ res.json(results); }; },()=>{}) }) app.get('/selectbyid',(req,res)=>{ let wbId=req.query.id; let sql="select * from WBAccountNumber where WbId='"+wbId+"'"; query(sql,(error, results, fields)=> { if (error){ res.json('error') }else{ res.json(results); }; },()=>{}) }) app.get('/insert',(req,res)=>{ let wbNum=req.query.num; let wbName=req.query.name; let id=guid(); let day1 = new Date(); let s1 = day1.format("yyyy-MM-dd hh:mm:ss"); let sql='insert into WBAccountNumber values("'+id+'","'+wbNum+'","'+wbName+'","'+s1+'","1")'; query(sql, function (error, results, fields) { if (error){ res.json('error') }else{ res.json('success'); }; },()=>{}); }) app.get('/update',(req,res)=>{ let wbId=req.query.id; let wbName=req.query.name; let sql='update WBAccountNumber set WbName="'+wbName+'" where WbId="'+wbId+'"'; query(sql, function (error, results, fields) { if (error){ res.json('error') }else{ res.json('success'); }; },()=>{}); }) app.get('/delete',(req,res)=>{ let wbId=req.query.id; let sql='update WBAccountNumber set WbState="0" where WbId="'+wbId+'"'; query(sql, function (error, results, fields) { if (error){ res.json('error') }else{ res.json('success'); }; },()=>{}); }) // app.get('/getdata', (req, res) => { getdata((msg)=> { res.send(msg) }) }) app.get('/zhiding', (req, res) => { let time2 = req.query.time.split('-'); zhiding(time2[0], time2[1], (msg)=>{ res.send(msg) }) }) // schedule.scheduleJob('30 1 8 * * *', function(){ // start() // }); app.listen(2080, function() { console.log('http://localhost:2080'); })