Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async.parallel并发发送模板消息,为什么只能接受到十几条模板消息了 #35

Open
anxiaowen2 opened this issue Dec 31, 2017 · 0 comments

Comments

@anxiaowen2
Copy link

1:封装的发送模板消息的方法
采用request.post 发送模板消息 request.post(url,{ json: { touser:openid,template_id:templateId,url:url,data:data } },function (error, response, body){});

function sendmessage (prepareid,sendsuccess,sendfail,callback){
async.waterfall([
//start
function(callback){
client.hget(store_prepare,prepareid,function(err,res){
if(err || res==null){
return callback('[135]获取list状态失败');
}
// console.log(res);
return callback(null,res);
});
},
function(status,callback){
if(status==0){
return callback('[140]list处于暂停状态');
}else{
client.llen(prepareid,function(err,length){
if(err){
return callback('[144]获取list长度失败');
}
return callback(null,length);
});
}
},
function(length,callback){
// console.log(length);
if(length<=0){
return callback('[153]list已为空');
}else{
client.lpop(prepareid,function(err,templateinfo){
if(err || templateinfo==null){
return callback('[157]获取list数据失败');
}
// console.log(templateinfo);
return callback(null,templateinfo);
});
}
},
function(templateinfo,callback){
client.hget('currwxtickets',defaultAppid,function(err,ret){
if(err){
return callback('[4579]'+err);
}
var ret = JSON.parse(ret);
var get_access_token = ret.accessToken;
return callback(null,templateinfo,get_access_token);
});
},
function(templateinfo,get_access_token,callback){
var sendinfo = JSON.parse(templateinfo);
var openid = sendinfo.openid;
var templateId = sendinfo.templateId;
var data = sendinfo.data;
var url = sendinfo.url;
var uid = sendinfo.uid;
data.first.value = Math.floor(Math.random() * 10000 + 1);//测试的代码,上线需注释
data.keyword1.value = Math.floor(Math.random() * 10000 + 1);//测试的代码,上线需注释
var request = require('request');
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='+get_access_token;
request.post(
url,
{ json: { touser: openid,template_id:templateId,url:url,data:data } },
function (error, response, body) {
//console.log(error);
//console.log(response);
if (!error && response.statusCode == 200) {
store.result = response.body;
var store_string = JSON.stringify(store);
return callback(null,1,store_string);
}else{
}
}
);
2:async.parallel调用方法并发发送模板消息,但是最后有时候只能接受到10几条数据
async.parallel([
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
function(callback){
sendmessage(prepareid,sendsuccess,sendfail,callback);
},
//20
],function(err,res){
//返回结果,就不贴代码了
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant