appid = Env::get('wechat.appid', ''); $this->appsecret = Env::get('wechat.appsecret', ''); } /** * 获取微信公众号 access_token(带缓存) */ private function getAccessToken() { $cacheKey = 'wechat_access_token'; $token = Cache::get($cacheKey); if ($token) { return $token; } $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}"; $res = json_decode(myGet($url), true); if (!isset($res['access_token'])) { return false; } Cache::set($cacheKey, $res['access_token'], $res['expires_in'] - 200); return $res['access_token']; } /** * 获取公众号图文素材列表 * * @param int offset 起始位置,默认0 * @param int count 获取数量,默认10,最大20 */ public function getArticleList() { $data = $this->request->param(); $offset = isset($data['offset']) ? intval($data['offset']) : 0; $count = isset($data['count']) ? intval($data['count']) : 10; if ($count > 20) { $count = 20; } $accessToken = $this->getAccessToken(); if (!$accessToken) { return jsonError('获取access_token失败,请检查appid和appsecret配置'); } $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={$accessToken}"; $postData = [ 'type' => 'news', 'offset' => $offset, 'count' => $count, ]; $res = json_decode(myPost1($url, $postData), true); if (isset($res['errcode']) && $res['errcode'] != 0) { return jsonError('微信接口错误: ' . ($res['errmsg'] ?? '未知错误')); } $list = []; if (isset($res['item'])) { foreach ($res['item'] as $item) { $articles = []; $newsItems = isset($item['content']['news_item']) ? $item['content']['news_item'] : []; foreach ($newsItems as $news) { $articles[] = [ 'title' => $news['title'] ?? '', 'thumb_media_id' => $news['thumb_media_id'] ?? '', 'thumb_url' => $news['thumb_url'] ?? '', 'author' => $news['author'] ?? '', 'digest' => $news['digest'] ?? '', 'content_source_url' => $news['content_source_url'] ?? '', 'url' => $news['url'] ?? '', ]; } $list[] = [ 'media_id' => $item['media_id'], 'update_time' => isset($item['content']['update_time']) ? date('Y-m-d H:i:s', $item['content']['update_time']) : '', 'articles' => $articles, ]; } } return jsonSuccess([ 'total_count' => $res['total_count'] ?? 0, 'item_count' => $res['item_count'] ?? 0, 'list' => $list, ]); } /** * 获取公众号图文素材详情 * * @param string media_id 素材的media_id */ public function getArticleDetail() { $data = $this->request->param(); if (!isset($data['media_id']) || $data['media_id'] == '') { return jsonError('media_id不能为空'); } $accessToken = $this->getAccessToken(); if (!$accessToken) { return jsonError('获取access_token失败,请检查appid和appsecret配置'); } $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token={$accessToken}"; $postData = [ 'media_id' => $data['media_id'], ]; $res = json_decode(myPost1($url, $postData), true); if (isset($res['errcode']) && $res['errcode'] != 0) { return jsonError('微信接口错误: ' . ($res['errmsg'] ?? '未知错误')); } $articles = []; $newsItems = isset($res['news_item']) ? $res['news_item'] : []; foreach ($newsItems as $news) { $articles[] = [ 'title' => $news['title'] ?? '', 'thumb_media_id' => $news['thumb_media_id'] ?? '', 'thumb_url' => $news['thumb_url'] ?? '', 'author' => $news['author'] ?? '', 'digest' => $news['digest'] ?? '', 'content' => $news['content'] ?? '', 'content_source_url' => $news['content_source_url'] ?? '', 'url' => $news['url'] ?? '', ]; } return jsonSuccess([ 'media_id' => $data['media_id'], 'articles' => $articles, ]); } /** * 获取已发布的文章列表(发布接口) * * @param int offset 起始位置,默认0 * @param int count 获取数量,默认10,最大20 * @param int no_content 1=不返回content字段,0=返回,默认0 */ public function getPublishedArticles() { $data = $this->request->param(); $offset = isset($data['offset']) ? intval($data['offset']) : 0; $count = isset($data['count']) ? intval($data['count']) : 10; $noContent = isset($data['no_content']) ? intval($data['no_content']) : 0; if ($count > 20) { $count = 20; } $accessToken = $this->getAccessToken(); if (!$accessToken) { return jsonError('获取access_token失败,请检查appid和appsecret配置'); } $url = "https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token={$accessToken}"; $postData = [ 'offset' => $offset, 'count' => $count, 'no_content' => $noContent, ]; $res = json_decode(myPost1($url, $postData), true); if (isset($res['errcode']) && $res['errcode'] != 0) { return jsonError('微信接口错误: ' . ($res['errmsg'] ?? '未知错误')); } $list = []; if (isset($res['item'])) { foreach ($res['item'] as $item) { $articles = []; $newsItems = isset($item['content']['news_item']) ? $item['content']['news_item'] : []; foreach ($newsItems as $news) { $article = [ 'title' => $news['title'] ?? '', 'author' => $news['author'] ?? '', 'digest' => $news['digest'] ?? '', 'thumb_url' => $news['thumb_url'] ?? '', 'content_source_url' => $news['content_source_url'] ?? '', 'url' => $news['url'] ?? '', 'is_deleted' => $news['is_deleted'] ?? false, ]; if (!$noContent) { $article['content'] = $news['content'] ?? ''; } $articles[] = $article; } $list[] = [ 'article_id' => $item['article_id'] ?? '', 'update_time' => isset($item['content']['update_time']) ? date('Y-m-d H:i:s', $item['content']['update_time']) : '', 'articles' => $articles, ]; } } return jsonSuccess([ 'total_count' => $res['total_count'] ?? 0, 'item_count' => $res['item_count'] ?? 0, 'list' => $list, ]); } /** * 获取已发布的文章详情 * * @param string article_id 发布文章的article_id */ public function getPublishedArticleDetail() { $data = $this->request->param(); if (!isset($data['article_id']) || $data['article_id'] == '') { return jsonError('article_id不能为空'); } $accessToken = $this->getAccessToken(); if (!$accessToken) { return jsonError('获取access_token失败,请检查appid和appsecret配置'); } $url = "https://api.weixin.qq.com/cgi-bin/freepublish/getarticle?access_token={$accessToken}"; $postData = [ 'article_id' => $data['article_id'], ]; $res = json_decode(myPost1($url, $postData), true); if (isset($res['errcode']) && $res['errcode'] != 0) { return jsonError('微信接口错误: ' . ($res['errmsg'] ?? '未知错误')); } $articles = []; $newsItems = isset($res['news_item']) ? $res['news_item'] : []; foreach ($newsItems as $news) { $articles[] = [ 'title' => $news['title'] ?? '', 'author' => $news['author'] ?? '', 'digest' => $news['digest'] ?? '', 'thumb_url' => $news['thumb_url'] ?? '', 'content' => $news['content'] ?? '', 'content_source_url' => $news['content_source_url'] ?? '', 'url' => $news['url'] ?? '', 'is_deleted' => $news['is_deleted'] ?? false, ]; } return jsonSuccess([ 'article_id' => $data['article_id'], 'articles' => $articles, ]); } }