/**
  * Finds the SubscribeStockArticle model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return SubscribeStockArticle the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = SubscribeStockArticle::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemple #2
0
 /**
  * 处理要发送的文章
  * @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;
 }