/** * 发送消息 * @date 2016-02-27T17:36:12+0800 * @author cnzhangxl@foxmail.com * @return [type] [description] */ public function actionMsgSend() { $sleep = 15; $cache_time = 60; $cachename = 'console_wechat_msg-send'; if (Yii::$app->cache->get($cachename)) { exit; } while (true) { Yii::$app->cache->set($cachename, 1, $cache_time); $wx_app_list = WxApp::find()->all(); foreach ($wx_app_list as $app) { $msg_arr = WxMsgSend::find()->where(['send_num' => 0, 'appid' => $app->id])->all(); $options = ['token' => $app->token, 'encodingaeskey' => $app->encodingaeskey, 'appid' => $app->appid, 'appsecret' => $app->appsecret]; $we_obj = new Wechat($options); $user_last = array(); foreach ($msg_arr as $msg_one) { if (isset($user_last[$msg_one->user_id]) && $user_last[$msg_one->user_id] + 10 > time()) { continue; } $user_last[$msg_one->user_id] = time(); $res = false; $msg_one->scenario = 'update'; switch ($msg_one->msg_type) { case 1: //客服消息 $res = $we_obj->sendCustomMessage(json_decode($msg_one->content, true)); break; default: # code... break; } $msg_one->send_at = time(); $msg_one->send_num += 1; if (!$res) { $msg_one->errcode = "" . $we_obj->errCode; } $msg_one->save(); } } sleep($sleep); } return false; }
/** * 处理要发送的文章 * @date 2016-02-26T22:47:21+0800 * @author cnzhangxl@foxmail.com * @return [type] [description] */ public function actionSendToUser() { $sleep = 5; $cache_time = 30; $cachename = 'console_stock_send-to-user'; if (Yii::$app->cache->get($cachename)) { exit; } while (true) { Yii::$app->cache->set($cachename, 1, $cache_time); //获取所有用户订阅的类型 $article_type_arr = SubscribeStockArticle::find()->groupBy('article_type')->all(); foreach ($article_type_arr as $one_type) { //获取订阅此分类的所有用户 $subscribe_list = SubscribeStockArticle::find()->where(['article_type' => $one_type->article_type])->all(); //获取此类型下今天的所有文章 $stock_article_arr = StockArticle::find()->where(['type' => $one_type->article_type])->andWhere(['>', 'pub_at', strtotime(date('Y-m-d'))])->all(); foreach ($stock_article_arr as $stock_article) { //获取此文章最后发送的id $SendSubscribeStockArticle = SendSubscribeStockArticle::find()->where(['stock_article_id' => $stock_article->id])->one(); //从未发送过 if (!$SendSubscribeStockArticle) { $SendSubscribeStockArticle = new SendSubscribeStockArticle(['scenario' => 'create']); $SendSubscribeStockArticle->setAttributes(array('stock_article_id' => $stock_article->id, 'last_data_id' => 0)); $SendSubscribeStockArticle->save(); } if ($SendSubscribeStockArticle) { //获取未发送的文章数据 $article_data_arr = StockArticleData::find()->where(['pid' => $stock_article->id])->andWhere(['>', 'id', $SendSubscribeStockArticle->last_data_id])->all(); $last_data_id = 0; foreach ($article_data_arr as $data) { //为此用户发消息 foreach ($subscribe_list as $subscribe_one) { $WxMsgSend = new WxMsgSend(['scenario' => 'create']); $content_arr = ['touser' => $subscribe_one->user->openid, 'msgtype' => 'text', 'text' => ['content' => $data->content]]; $WxMsgSend->setAttributes(array('user_id' => $subscribe_one->user_id, 'appid' => $subscribe_one->user->appid, 'msg_type' => '1', 'content' => json_encode($content_arr), 'send_num' => 0, 'errcode' => '', 'send_at' => 0)); $WxMsgSend->save(); } $last_data_id = $data->id; } if ($last_data_id) { $SendSubscribeStockArticle->scenario = 'update'; $SendSubscribeStockArticle->last_data_id = $last_data_id; $SendSubscribeStockArticle->save(); } } } } sleep($sleep); } return false; }