promise2.js
6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Created by shipfi on 2016/11/19.
*/
const path = require('path');
var jsuri = require('jsuri');
var fetch = require('node-fetch');
var fs = require('fs');
var json2csv = require('json2csv');
var csvFields = ['subscribe','openid','nickname','sex','city','province','country','headimgurl','subscribe_time','remark','groupid'];
//var accessToken = '2Y3cDvNzN2UNqEKXXGrnQox3Iv_DssZIlhrYOY5pAy_ZFhm1EkLyiJ68gq1DirrbpMv-YVVuo4P1BH9AMEE2w9C1Bhunj5F0Oj1If6qq0lwo58HZYqUNPyzcFgBN95aJTAGiALDBIO';
var accessToken = 'IX6Bajo9XVnVX02JzImWeQOEEtNohoZB-5EWSDUxYpsDPwZdBOMyzK0S0ruuYwgfW6XObv0VLlruKmnp5k6wwRV_QeAEbVLoBWlRQKp9CUteKkJLwbxob0NMuZc7JqlCSTLfAIDABT';
var getOpenIdFromWx = function(openid=null) {
var wxGetUsersURL = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='+accessToken;
if(openid) {
wxGetUsersURL = wxGetUsersURL+"&next_openid="+openid;
}
//console.log(wxGetUsersURL);
return new Promise(function(resolve,reject){
fetch(wxGetUsersURL).then(function(res){
resolve(res.json());
}).catch(function(err){
reject(err);
});
});
};
var getWxUsersInfo = function(openids,file_id=0){
return new Promise(function(resolve,reject){
if(!openids) reject(0);
var lengths = openids.length;
if(lengths<=0) reject(0);
var times = parseInt(lengths/100)+1;
var wxUserInfoUrl = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token='+accessToken;
var lastOpenId = openids[lengths-1];
// var jsonStream = { 'user_list':[] };
for(var i=0;i<times;i++) {
var iBegin = 0;
var iEnd = 99;
if(openids.length<0)
return;
var tmpOpenIds = openids.splice(iBegin,iEnd);
var tmpJsonStream = { 'user_list':[] };
tmpJsonStream.user_list = tmpOpenIds.map(function(x){
return {"openid":x,"lang":'zh-CN'}
});
var userListLength = tmpJsonStream.user_list.length;
var stringJsonStream = JSON.stringify(tmpJsonStream);
// jsonStream.user_list = openids.splice(iBegin,iEnd).map(function(x){
// return {"openid":x,"lang":'zh-CN'}
// });
if(userListLength>0) {
fetch(wxUserInfoUrl,{method:'POST',body:stringJsonStream}).then(function(res){
return res.json();
}).then(function(json){
console.log('Begin:========================================================>'+iBegin);
var result = json2csv({data:json.user_info_list,fields:csvFields,'hasCSVColumnTitle':false});
console.log(result);
var fileName = path.join(process.cwd(),'/data_'+file_id+'.csv');
fs.appendFile(fileName,"\n",'utf8',function(err){
if(err) throw err;
});
fs.appendFile(fileName,result,'utf8',function(err){
if(err) throw err;
console.log('Saved.');
});
if(i>=times-1) {
resolve(lastOpenId);
}
console.log('End:========================================================>'+iEnd);
}).catch(function(err){
reject(0);
});
}
}
});
// for(var i=0;i<times;i++) {
// jsonStream.user_list = [];
// var iBegin = i*100;
// var iEnd = (i*100+99)>(lengths-1)?(lengths-1):i*100+99;
// if(iBegin>iEnd) break;
// console.log(iBegin+':========================================================>'+iEnd);
// jsonStream.user_list = openids.splice(iBegin,iEnd).map(function(x){
// return {"openid":x,"lang":'zh-CN'}
// });
//
// if(jsonStream.user_list.length>0) {
// fetch(wxUserInfoUrl,{method:'POST',body:JSON.stringify(jsonStream)}).then(function(res){
// return res.json()
// }).then(function(json){
// console.log(JSON.stringify(json.user_info_list));
// // var result = json2csv({data:json.user_info_list,fields:csvFields});
// // //console.log(result);
// // var fileName = path.join(process.cwd(),'/data_'+file_id+'.csv');
// // fs.appendFile(fileName,result,'utf8',function(err){
// // if(err) throw err;
// // console.log('Saved.');
// // });
// });
//
// }
// }
};
var processDatas = function(_nextOpenid=null) {
var openidArrays = [];
var goFetch = function(nextOpenId,fileIndex=0) {
return getOpenIdFromWx(nextOpenId).then(function(json){
//console.log(response);
if(typeof json.data != 'undefined') {
var openids = json.data.openid;
openidArrays = openidArrays.concat(openids);
var openidLength = openids.length;
var lastOpenId = openidLength?openids[openidLength-1]:null;
//console.log(fileIndex+'++++++++++++++++++++++++++++++++++++++++++++++++++++>'+openidLength);
//console.log(openids);
//getWxUsersInfo(openids,fileIndex);
getWxUsersInfo(openids).then(function(theRetLastOpenId){
goFetch(theRetLastOpenId,fileIndex++);
},function(rejRes){
Promise.resolve(rejRes);
});
// if(lastOpenId) {
//
// }else{
// Promise.resolve(openidArrays);
// }
}else{
Promise.resolve(openidArrays);
}
}).catch(function(err){
Promise.reject(err);
});
}
return goFetch(_nextOpenid);
};
// var xxx = [];
// for(var i=0;i<298;i++) {
// xxx.push('OOOOPPPPP'+i);
// }
// getWxUsersInfo(xxx);
var _nextOpenid = null;
processDatas(_nextOpenid).then(function(data){
//console.log(data);
});
//console.log(openIds);