获取图文消息

@since 0.0.1
@param {string} [$media_id] 媒体文件ID
@param {boolean} [$all=true] 是否返回所有
@param {integer} [$page=1] 页码
@param {integer} [$count=20] 每页数量
@return {array}
@example \Yii::$app->wechat->getNews($media_id, $all, $page, $count);
324710
public function getNews($media_id = null, $all = true, $page = 1, $count = 20) {
		return $media_id ? $this->getMaterial($media_id) : $this->getMaterials('news', $all, $page, $count);
	}
978222
public function addMedia($material_id) {
		$material = WechatMaterial::findOne($material_id);
		if(!$material) {
			throw new ErrorException('数据查询失败');
		}

		$data = $this->getData('/cgi-bin/media/upload', [
			'access_token' => $this->getAccessToken(),
			'type' => $material->type,
		], ['media' => '@' . $material->localFile]);
		$material->cleanTmp();

		if($this->errcode == 0) {
			$materialMedia = new WechatMaterialMedia;
			$materialMedia->appid = $this->app->appid;
			$materialMedia->material_id = $material->id;
			$materialMedia->media_id = $data['media_id'];
			$materialMedia->expired_at = $data['created_at'] + $this->effectiveTimeOfTemporaryMaterial;
			if($materialMedia->save()) {
				return $materialMedia->id;
			}
		}

		return 0;
	}
407540
public function getUserMedia($id = 'self', $limit = 0)
    {
        $params = array();

        if ($limit > 0) {
            $params['count'] = $limit;
        }

        return $this->_makeCall('users/' . $id . '/media/recent', strlen($this->getAccessToken()), $params);
    }
983524
public function getMaterial($media_id, $outType = null)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
        $this->registerApi($url, __FUNCTION__, func_get_args());
        $result = Tools::post($url, ['media_id' => $media_id]);
        if (json_decode($result)) {
            return Tools::json2arr($result);
        }
        return is_null($outType) ? $result : $outType($result);
    }
546767
def download_media(self, media_id):
        
        return requests.get(
            url="https://api.weixin.qq.com/cgi-bin/media/get",
            params={
                "access_token": self.token,
                "media_id": media_id
            }
        )
813060
public function sendImage($toUser, $media_id)
    {
        $ret = array();
        $ret['touser'] = $toUser;
        $ret['msgtype'] = 'image';
        $ret['image']['media_id'] = $media_id;
        return $this->send($ret);
    }
664210
public function previewMpnews($media_id = '', $userid = '', $type = 'openid')
    {
        $queryStr = [];

        if ($type == 'towxname') {
            $queryStr['towxname'] = $userid;
        } else {
            $queryStr['touser'] = $userid;
        }

        $queryStr['mpnews']['media_id'] = $media_id;
        $queryStr['msgtype']            = 'mpnews';

        $this->module = 'message';

        $res = $this->_post('mass/preview', $queryStr);

        return $res;
    }
119622
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;
	}
799582
public function getMedia()
    {
        $mediaMapper = ($this->container->dataMapper)('MediaMapper');

        $data = $mediaMapper->find();

        $template = <<<HTML
            {% import "@admin/media/_mediaCardMacro.html" as file %}
            <div class="card-wrapper">
            {% for media in page.media %}
              {{ file.mediaCard(media) }}
            {% endfor %}
            </div>
HTML;

        $mediaHtml = $this->container->view->fetchFromString($template, ['page' => ['media' => $data]]);

        $response = $this->response->withHeader('Content-Type', 'application/json');

        return $response->write(json_encode(["html" => $mediaHtml]));
    }
320713
public function getMediaWithHttpInfo($media_id, $is_video = false)
    {
        if (!$this->access_token && !$this->getAccessToken()) {
            return false;
        }
        $url_prefix = $is_video ? str_replace('https', 'http', self::API_URL_PREFIX) : self::API_URL_PREFIX;
        $url = $url_prefix . self::MEDIA_GET_URL . "access_token={$this->access_token}" . '&media_id=' . $media_id;
        $oCurl = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        $result = array();
        if (intval($aStatus["http_code"]) !== 200) {
            return false;
        }
        if ($sContent) {
            if (is_string($sContent)) {
                $json = json_decode($sContent, true);
                if (isset($json['errcode'])) {
                    $this->errCode = $json['errcode'];
                    $this->errMsg = $json['errmsg'];
                    return $this->checkRetry(__FUNCTION__, func_get_args());
                }
            }
            $result['content'] = $sContent;
            $result['info'] = $aStatus;
            return $result;
        }
        return false;
    }
358744
public function sendImageByTag($tag_id, $media_id, $is_to_all = false)
    {
        $ret = array();
        $ret['filter']['tag_id'] = $tag_id;
        $ret['filter']['is_to_all'] = $is_to_all;
        $ret['msgtype'] = 'image';
        $ret['image']['media_id'] = $media_id;
        return $this->sendAll($ret);
    }
157934
public function paginateImages ($offset = 0, $count = 20)
    {
        $json = $this->paginate(MediaType::IMAGE, $offset, $count);
        $resultSet = [];
        
        foreach ($json->item as $item) {
            $image = new Paginated\Image($item->media_id, $item->name, $item->update_time);
            
            if (isset($item->url) && strlen($item->url) > 0) {
                $image = $image->withURL($item->url);
            }
            
            $resultSet[] = $image;
        }

        return new Paginated\ImageResultSet($resultSet, $json->total_count, $offset);
    }
718881
public function getForeverMedia($media_id,$is_video=false){
        if (!$this->access_token && !$this->checkAuth()) return false;
        $data = array('media_id' => $media_id);
        //#TODO 暂不确定此接口是否需要让视频文件走http协议
        //如果要获取的素材是视频文件时，不能使用https协议，必须更换成http协议
        //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
        $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this->access_token,self::json_encode($data));
        if ($result)
        {
            if (is_string($result)) {
                $json = json_decode($result,true);
                if ($json) {
                    if (isset($json['errcode'])) {
                        $this->errCode = $json['errcode'];
                        $this->errMsg = $json['errmsg'];
                        return false;
                    }
                    return $json;
                } else {
                    return $result;
                }
            }
            return $result;
        }
        return false;
    }
268096
public function getMedia($media_id){
		if (!$this->access_token && !$this->checkAuth()) return false;
		$result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
		if ($result)
		{
			$json = json_decode($result,true);
			if (isset($json['errcode'])) {
				$this->errCode = $json['errcode'];
				$this->errMsg = $json['errmsg'];
				return false;
			}
			return $result;
		}
		return false;
	}
126165
public function getAction($id, Request $request)
    {
        try {
            $locale = $this->getRequestParameter($request, 'locale', true);
            $mediaManager = $this->getMediaManager();
            $view = $this->responseGetById(
                $id,
                function($id) use ($locale, $mediaManager) {
                    $media = $mediaManager->getById($id, $locale);
                    $collection = $media->getEntity()->getCollection();

                    if (SystemCollectionManagerInterface::COLLECTION_TYPE === $collection->getType()->getKey()) {
                        $this->getSecurityChecker()->checkPermission(
                            'sulu.media.system_collections',
                            PermissionTypes::VIEW
                        );
                    }

                    $this->getSecurityChecker()->checkPermission(
                        new SecurityCondition(
                            $this->getSecurityContext(),
                            $locale,
                            $this->getSecuredClass(),
                            $collection->getId()
                        ),
                        PermissionTypes::VIEW
                    );

                    return $media;
                }
            );
        } catch (MediaNotFoundException $e) {
            $view = $this->view($e->toArray(), 404);
        } catch (MediaException $e) {
            $view = $this->view($e->toArray(), 400);
        }

        return $this->handleView($view);
    }
241508
function (project_id, page, per_page, cb) {

            var params = {};

            if (project_id) {

                params.project_id = project_id;

            }

            if (page) {

                params.page = page;

            }

            if (per_page) {

                params.per_page = per_page;

            }

            _util.buildQuery('medias', params, function (error, data) {

                return cb(null, data);

            });

        }
391414
public function listData($list, $page = 1, $size = 10)
    {
        $reData['totalPages'] = (int)$list->lastPage();
        $reData['totalCount'] = (int)$list->total();
        $reData['page'] = (int)$page;
        $reData['limit'] = (int)$size;
        $reData['count'] = (int)$list->count();

        if ($page == 1) {
            $reData['firstPage'] = true; //是否为首页
            $reData['hasPrePage'] = false; //是否有上一页
            $reData['prePage'] = 0; //上一页的页数
            if ($reData['totalPages'] == 1) {
                $reData['hasNextPage'] = false; //是否有下一页
                $reData['nextPage'] = 0;
            } else {
                $reData['hasNextPage'] = true; //是否有下一页
                $reData['nextPage'] = 2; //下一页的页数
            }
        } else {
            $reData['firstPage'] = false; //是否为首页
            if ($reData['totalPages'] == 1) {
                $reData['hasPrePage'] = false; //是否有上一页
                $reData['hasNextPage'] = false; //是否有下一页
                $reData['prePage'] = 1; //上一页的页数
                $reData['nextPage'] = 0; //下一页的页数
            } elseif ($page < $reData['totalPages']) {
                $reData['hasPrePage'] = true; //是否有上一页
                $reData['hasNextPage'] = true; //是否有下一页
                $reData['prePage'] = $page - 1; //上一页的页数
                $reData['nextPage'] = $page + 1; //下一页的页数
            } else {
                $reData['hasPrePage'] = true; //是否有上一页
                $reData['hasNextPage'] = false; //是否有下一页
                $reData['prePage'] = $page - 1; //上一页的页数
                $reData['nextPage'] = 0; //下一页的页数
            }
        }

        if ($page == $reData['totalPages']) {
            $reData['lastPage'] = true; //是否是最末页
        } else {
            $reData['lastPage'] = false; //是否是最末页
        }

        $reData['data'] = $list->toArray()['data'];

        return $reData;
    }