main.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. const express = require('express')
  2. const app = express()
  3. const axios = require('axios')
  4. const query = require('./mysqler')
  5. const bodyParser = require("body-parser");
  6. const schedule = require('node-schedule');
  7. 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'];
  8. app.use('/', express.static(__dirname + '/'))
  9. app.use(bodyParser.urlencoded({ extended: false }));
  10. app.all('*', (req, res, next) => {
  11. res.header("Access-Control-Allow-Origin", req.headers.origin); //设置来源
  12. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  13. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  14. res.header("Access-Control-Max-Age", "604800000");
  15. res.header("Access-Control-Allow-Credentials", true);
  16. res.header("X-Powered-By", ' 3.2.1');
  17. // res.header("Content-Type", "application/json;charset=utf-8");
  18. next();
  19. });
  20. const httpreq = async (msg) => {
  21. try {
  22. let {data} = await axios({
  23. method: 'get',
  24. url: 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + msg
  25. });
  26. let redata = {
  27. weiboname: data.data.userInfo.screen_name,
  28. weiboid: msg,
  29. weibofan: data.data.userInfo.followers_count,
  30. weibopost: data.data.userInfo.statuses_count
  31. }
  32. return redata
  33. } catch (error) {
  34. console.log(error)
  35. }
  36. }
  37. Date.prototype.format = function (fmt) {
  38. var o = {
  39. "M+": this.getMonth() + 1, //月份
  40. "d+": this.getDate(), //日
  41. "h+": this.getHours(), //小时
  42. "m+": this.getMinutes(), //分
  43. "s+": this.getSeconds(), //秒
  44. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  45. "S": this.getMilliseconds() //毫秒
  46. };
  47. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  48. for (var k in o)
  49. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  50. return fmt;
  51. }
  52. const guid=()=>{
  53. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  54. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  55. return v.toString(16);
  56. });
  57. }
  58. const retime = (msg) => {
  59. var day1 = new Date();
  60. day1.setDate(day1.getDate() + msg);
  61. var s1 = day1.format("yyyy-MM-dd")
  62. return s1
  63. }
  64. const inssql = (msg) => {
  65. try {
  66. let sql = "INSERT INTO `digital`(`name`, `id`, `fan`, `time`, `post`) VALUES ('"+msg.weiboname+"', '"+msg.weiboid+"', '"+msg.weibofan+"', NOW(), '"+msg.weibopost+"');"
  67. query(sql, [1], function(err,results,fields){
  68. });
  69. } catch (error) {
  70. console.log(error)
  71. }
  72. }
  73. const select = (t1, t2, callback) => {
  74. try {
  75. let time1 = retime(t1)
  76. let time2 = retime(t2)
  77. let sql = "SELECT * FROM digital WHERE time BETWEEN '"+ time1 +"' and '"+ time2 +"';"
  78. query(sql, [1], function(err,results,fields){
  79. callback(results)
  80. });
  81. } catch (error) {
  82. console.log(error)
  83. }
  84. }
  85. const selintime = (msg, callback) => {
  86. try {
  87. let sql = "select * from digital where DATE(time) = '"+ msg +"';"
  88. query(sql, [1], function(err,results,fields){
  89. callback(results)
  90. });
  91. } catch (error) {
  92. }
  93. }
  94. const getlastmonth = () => {
  95. var nowdays = new Date();
  96. var year = nowdays.getFullYear();
  97. var month = nowdays.getMonth();
  98. if(month==0)
  99. {
  100. month=12;
  101. year=year-1;
  102. }
  103. if (month < 10) {
  104. month = "0" + month;
  105. }
  106. var myDate = new Date(year, month, 0);
  107. var lastDay = year + "-" + month + "-" + myDate.getDate()
  108. return lastDay
  109. }
  110. const selintimeer = (callback) => {
  111. try {
  112. let lastmonth = getlastmonth()
  113. let sql = "SELECT * FROM digital WHERE DATE(time) = '"+ lastmonth +"' OR DATE_FORMAT( time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ORDER BY id;"
  114. // let sql = "SELECT * FROM `digital` WHERE DATE_FORMAT( time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ORDER BY id;"
  115. query(sql, [1], function(err,results,fields){
  116. callback(results)
  117. });
  118. } catch (error) {
  119. }
  120. }
  121. const selintimeer2 = (tm1, tm2, callback) => {
  122. try {
  123. let sql = "select * from `digital` where time BETWEEN '"+tm1+"' and '"+tm2+"' ORDER BY id;"
  124. query(sql, [1], function(err,results,fields){
  125. callback(results)
  126. });
  127. } catch (error) {
  128. }
  129. }
  130. const start = async () => {
  131. try{
  132. let sql = "select * from WBAccountNumber where WbState='1' order by AddTime desc"
  133. query(sql, [1], function(err,results,fields){
  134. results.map(async (i)=>{
  135. let data = await httpreq(i.WbNumber)
  136. inssql(data)
  137. })
  138. });
  139. } catch(error) {
  140. }
  141. // data.map(async(i) => {
  142. // let data = await httpreq(i)
  143. // inssql(data)
  144. // })
  145. }
  146. const getyue = (tm1, tm2, tm, callback) => {
  147. try{
  148. let sql = "SELECT * FROM digital WHERE DATE(time) = '"+tm+"' or year(time) = '"+tm1+"' and month(time) = '"+tm2+"' ORDER BY id"
  149. query(sql, [1], function(err,results,fields){
  150. callback(results)
  151. });
  152. } catch(error) {
  153. }
  154. }
  155. const getdata = (callback) => {
  156. try {
  157. let lastmonth = getlastmonth()
  158. 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;"
  159. query(sql, [1], function(err,results,fields){
  160. callback(results)
  161. });
  162. } catch (err) {
  163. }
  164. }
  165. const zhiding = (tim1, time2, callback) => {
  166. try {
  167. let lastmonth = getlastmonth()
  168. 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;"
  169. query(sql, [1], function(err,results,fields){
  170. callback(results)
  171. });
  172. } catch(err) {
  173. }
  174. }
  175. // setInterval(()=>{
  176. // start()
  177. // }, 24*60*60*1000)
  178. app.get('/getIndex', async (req, res) => {
  179. select(0, 1, (msg1)=>{
  180. select(1, 2, (msg2)=>{
  181. res.send({
  182. d1: msg1,
  183. d2: msg2
  184. })
  185. })
  186. })
  187. })
  188. app.get('/gettime', (req, res) => {
  189. let time = req.query.time;
  190. selintime(time,(results)=> {
  191. res.send(results)
  192. })
  193. })
  194. app.get('/all', (req, res) => {
  195. selintimeer((results)=> {
  196. res.send(results)
  197. })
  198. })
  199. app.get('/setime', (req, res) => {
  200. let time1 = req.query.starttime;
  201. let time2 = req.query.endtime;
  202. selintimeer2(time1,time2,(msg)=>{
  203. res.send(msg)
  204. })
  205. })
  206. app.get('/getyue', (req, res) => {
  207. let time2 = req.query.time.split('-');
  208. let time = req.query.lasttime;
  209. getyue(time2[0], time2[1], time, (msg) => {
  210. res.send(msg)
  211. })
  212. })
  213. //
  214. app.get('/selectall',(req,res)=>{
  215. let sql="select * from WBAccountNumber where WbState='1' order by AddTime desc";
  216. query(sql,(error, results, fields)=> {
  217. if (error){
  218. res.json('error')
  219. }else{
  220. res.json(results);
  221. };
  222. },()=>{})
  223. })
  224. app.get('/selectbyid',(req,res)=>{
  225. let wbId=req.query.id;
  226. let sql="select * from WBAccountNumber where WbId='"+wbId+"'";
  227. query(sql,(error, results, fields)=> {
  228. if (error){
  229. res.json('error')
  230. }else{
  231. res.json(results);
  232. };
  233. },()=>{})
  234. })
  235. app.get('/insert',(req,res)=>{
  236. let wbNum=req.query.num;
  237. let wbName=req.query.name;
  238. let id=guid();
  239. let day1 = new Date();
  240. let s1 = day1.format("yyyy-MM-dd hh:mm:ss");
  241. let sql='insert into WBAccountNumber values("'+id+'","'+wbNum+'","'+wbName+'","'+s1+'","1")';
  242. query(sql, function (error, results, fields) {
  243. if (error){
  244. res.json('error')
  245. }else{
  246. res.json('success');
  247. };
  248. },()=>{});
  249. })
  250. app.get('/update',(req,res)=>{
  251. let wbId=req.query.id;
  252. let wbName=req.query.name;
  253. let sql='update WBAccountNumber set WbName="'+wbName+'" where WbId="'+wbId+'"';
  254. query(sql, function (error, results, fields) {
  255. if (error){
  256. res.json('error')
  257. }else{
  258. res.json('success');
  259. };
  260. },()=>{});
  261. })
  262. app.get('/delete',(req,res)=>{
  263. let wbId=req.query.id;
  264. let sql='update WBAccountNumber set WbState="0" where WbId="'+wbId+'"';
  265. query(sql, function (error, results, fields) {
  266. if (error){
  267. res.json('error')
  268. }else{
  269. res.json('success');
  270. };
  271. },()=>{});
  272. })
  273. //
  274. app.get('/getdata', (req, res) => {
  275. getdata((msg)=> {
  276. res.send(msg)
  277. })
  278. })
  279. app.get('/zhiding', (req, res) => {
  280. let time2 = req.query.time.split('-');
  281. zhiding(time2[0], time2[1], (msg)=>{
  282. res.send(msg)
  283. })
  284. })
  285. // schedule.scheduleJob('30 1 8 * * *', function(){
  286. // start()
  287. // });
  288. app.listen(2080, function() {
  289. console.log('http://localhost:2080');
  290. })