123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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');
- })
|