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

co 并发,发送微信模板消息,为什么只能接受到几条数据 #34

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

Comments

@anxiaowen2
Copy link

anxiaowen2 commented Dec 31, 2017

封装了一个co发送一条模板消息的方法,然后co 调用并发,但是却不能接受到所有的模板消息,微信用户接受模板消息有限制吗?
1:封装方法

function* cosendfc(prepareid){
    let API = require('wechat-api');
    let api = new API(defaultAppid, defaultsecrut, function (callback) {
        // 传入一个获取全局token的方法
        client.hget(hash_currTickets,defaultAppid, function(err, ret){
            if(err){          
                return callback('[90]hget error. err=' + err);
            }
            // return callback(null,JSON.parse(ret));
            var currACTicket = JSON.parse(ret);
            if (currACTicket !== undefined && currACTicket !== null && currACTicket !== '') {           
                return callback(null,currACTicket);
            }else{           
                return callback(null,{
                    access_token: '',
                    expire_time: 0
                });
            }
        });
        
    }, function (token, callback) {
    // 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
        client.hset(hash_currTickets, defaultAppid, JSON.stringify(token), function(err, ret){
            if(err){
                return callback('[109]hset error. err=' + err);
            }
            return callback(null);
        });
    });
    var sendinfo = '';
    let statu =  Promise.resolve(new Promise(
        (resolve,reject) => client.hget(store_prepare,prepareid,(err, res) => err ? reject(err) : resolve(res))
    ));
    let length =   Promise.resolve(new Promise(
        (resolve,reject) => client.llen(prepareid,(err, res) => err ? reject(err) : resolve(res))
    ));
    let lpopcontent = Promise.resolve(new Promise(
        (resolve,reject) => client.lpop(prepareid,(err, res) => err ? reject(err) : resolve(res))
    ));
    sendinfo = JSON.parse(lpopcontent);
    console.log(sendinfo);
    let openid = sendinfo.openid;
    let templateId = sendinfo.templateId;
    let data = sendinfo.data;
    let url = sendinfo.url;
    let uid = sendinfo.uid;
    data.first.value = Math.floor(Math.random() * 10000 + 1);//测试的代码,上线需注释	
    data.keyword1.value = Math.floor(Math.random() * 10000 + 1);//测试的代码,上线需注释	
    let sendtesult =  Promise.resolve(new Promise(
        (resolve,reject) => api.sendTemplate(openid,templateId,url,data,(err, res) => err ? resolve(err) : resolve(res))
    ));
    return sendtesult;
}

2:co并发处理

	co(function* () {       
        var rs = yield [
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            //10
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            cosendfc(prepareid),
            //20
          ];
		return rs;
	  }).then(function (value) {
        console.log(value);
        return callback(null,value);
      });
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