Exemplo n.º 1
0
 /**
  * 获取图文素材
  * @method getNews
  * @since 0.0.1
  * @return {object}
  * @example $this->getNews();
  */
 public function getNews()
 {
     return $this->hasOne(WechatNews::classname(), ['id' => 'news_id']);
 }
Exemplo n.º 2
0
 /**
  * 新增图文消息
  *
  * @since 0.0.1
  * @param {integer} $news_id 图文素材id
  * @return {integer}
  * @example \Yii::$app->wechat->addNews($news_id);
  */
 public function addNews($news_id)
 {
     $news = WechatNews::findOne($news_id);
     if (!$news) {
         throw new ErrorException('数据查询失败');
     } else {
         if ($newsMedia = WechatNewsMedia::findOne(['appid' => $this->app->appid, 'news_id' => $news->id])) {
             return $newsMedia->id;
         }
     }
     $articles = $news->getArticles($this);
     $data = $this->getData('/cgi-bin/material/add_news', ['access_token' => $this->getAccessToken()], Json::encode(['articles' => $articles]));
     if ($this->errcode == 0) {
         $newsMedia = new WechatNewsMedia();
         $newsMedia->appid = $this->app->appid;
         $newsMedia->news_id = $news->id;
         $newsMedia->media_id = $data['media_id'];
         $newsMedia->thumb_material_media_ids = Json::encode(ArrayHelper::getColumn($articles, 'thumb_material_media_id'));
         if ($details = $this->getNews($newsMedia->media_id)) {
             $newsMedia->urls = Json::encode(ArrayHelper::getColumn($details['news_item'], 'url'));
             $newsMedia->thumb_urls = Json::encode(ArrayHelper::getColumn($details['news_item'], 'thumb_url'));
         }
         if ($newsMedia->save()) {
             return $newsMedia->id;
         }
     }
     return 0;
 }