index.js 1.59 KB
const path = require('path');
var fetch = require('node-fetch');
var fs = require('fs');
var file = path.join(process.cwd(),'/data.csv');

var json2csv = require('json2csv');
var csvFields = ['accessToken','expires','updateTime'];

//console.log(fetch);

var accessToken = '9FrII2SMyfSQqMgTKXBc2CBE_c-O8MW4Hc1SCO9o7pK3k-BLr-x4oGhWMG_Bzr7Or7xnFr1nx-gGdlorAGUAh6Vtisdusb7gq9bbO_byzYOGUoQ8FZZncgZ3JCvI6ov1AXNcAIDQND';

var getUsersUrl = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='+accessToken;

console.log(getUsersUrl);

var ss = function(url,param){
    return recordWxUsers()
};

var testUrl = 'http://thirdparty.qingger.com/auth/accessToken/MPShop';
recordWxUsers(testUrl).then(function(response){
    console.log(response);
    if(typeof response.call=='function') {
        response.call(testUrl,response.param);
    }
},function(rejResponse){
    console.log(rejResponse);
});


//
// var openids = json.data.openid;
// var openidLength = openids.length;
// var lastOpenId = openidLength?openids[openidLength-1]:null;



var result = json2csv({data:json.data,fields:csvFields});
console.log(result);
fs.writeFile(file,result,'utf8',function(err){
    if(err) throw err;
    console.log('Saved.');
});


function recordWxUsers(url,nextOpenId=null){
    return new Promise(function(resolve,reject){
        fetch(url)
            .then(function(res) {
                return res.json();
            })
            .then(function(json) {
                // 处理json

                return new Promise(function(resolve,reject){
                    fetch(url)
                })
            });
    });
    
}