更新wechat代码
This commit is contained in:
parent
90cdfa61ad
commit
c1038b7337
@ -3,7 +3,10 @@ namespace app\index\controller;
|
||||
|
||||
use think\facade\Request;
|
||||
use think\facade\Log;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Cache;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
use app\index\model\AdminConfig;
|
||||
|
||||
class WechatController extends BaseController
|
||||
{
|
||||
@ -29,7 +32,7 @@ class WechatController extends BaseController
|
||||
$timestamp = Request::get('timestamp');
|
||||
$nonce = Request::get('nonce');
|
||||
$echostr = Request::get('echostr');
|
||||
$token = "token2024";
|
||||
$token = AdminConfig::where('config_name', 'wechat_token')->value('config_value');
|
||||
$tmpArr = array($token, $timestamp, $nonce);
|
||||
sort($tmpArr, SORT_STRING);
|
||||
$tmpStr = implode($tmpArr);
|
||||
@ -90,6 +93,7 @@ class WechatController extends BaseController
|
||||
{
|
||||
|
||||
$accessToken = $this->getGZHAccessToken();
|
||||
print_r($accessToken);
|
||||
if (!$accessToken) {
|
||||
// 处理获取access_token失败的情况
|
||||
return;
|
||||
@ -124,42 +128,76 @@ class WechatController extends BaseController
|
||||
// 获取公众号Access token
|
||||
public function getGZHAccessToken()
|
||||
{
|
||||
$access_token = Cache::get('gzh_access_token');
|
||||
if (!$access_token) {
|
||||
$config = [
|
||||
'appid' => 'appid',
|
||||
'secret' => 'secret',
|
||||
'grant_type' => 'client_credential'
|
||||
];
|
||||
$response = $this->curlPost('https://api.weixin.qq.com/cgi-bin/token', $config);
|
||||
$data = json_decode($response, true);
|
||||
$access_token = $data["access_token"];
|
||||
Cache::set('gzh_access_token', $access_token, $data["expires_in"] - 200);
|
||||
}
|
||||
return $access_token;
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
public function curlPost($url = '', $postData = '', $options = array())
|
||||
{
|
||||
if (is_array($postData)) {
|
||||
$postData = http_build_query($postData);
|
||||
}
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
|
||||
if (!empty($options)) {
|
||||
curl_setopt_array($ch, $options);
|
||||
}
|
||||
//https请求 不验证证书和host
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
$data = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
// 从数据库获取配置
|
||||
$appid = AdminConfig::where('config_name', 'wechat_appid')->value('config_value');
|
||||
$secret = AdminConfig::where('config_name', 'wechat_appsecret')->value('config_value');
|
||||
|
||||
return $data;
|
||||
if (empty($appid) || empty($secret)) {
|
||||
throw new \Exception('微信配置信息未设置');
|
||||
}
|
||||
|
||||
// 构建请求URL
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
|
||||
|
||||
try {
|
||||
// 使用 GuzzleHttp 发送请求
|
||||
$client = new Client();
|
||||
$response = $client->get($url);
|
||||
$data = json_decode($response->getBody(), true);
|
||||
$response = $client->get($url);
|
||||
$data = json_decode($response->getBody(), true);
|
||||
|
||||
if (!isset($data['access_token'])) {
|
||||
throw new \Exception("获取access_token失败: {$data['errmsg']}", $data['errcode'] ?? -1);
|
||||
}
|
||||
|
||||
return $data['access_token'];
|
||||
} catch (\Exception $e) {
|
||||
Log::error('获取access_token失败:' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
//获取登录二维码的 ticket
|
||||
public function getLoginTicket()
|
||||
{
|
||||
// 获取access_token
|
||||
$access_token = $this->getGZHAccessToken();
|
||||
|
||||
// 构建请求URL
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
|
||||
|
||||
// 准备二维码图片文件
|
||||
$file = request()->file('qrcode');
|
||||
if (!$file) {
|
||||
return json(['code' => 1, 'msg' => '请上传二维码图片']);
|
||||
}
|
||||
|
||||
// 将文件转换为CURLFile对象
|
||||
$file_path = $file->getPathname();
|
||||
$file_name = $file->getOriginalName();
|
||||
$file_type = $file->getMime();
|
||||
|
||||
$data = [
|
||||
'media' => new \CURLFile($file_path, $file_type, $file_name)
|
||||
];
|
||||
|
||||
// 发送请求获取ticket
|
||||
$result = $this->curlPost($url, $data);
|
||||
$response = json_decode($result, true);
|
||||
|
||||
if (isset($response['errcode']) && $response['errcode'] != 0) {
|
||||
return json(['code' => 1, 'msg' => '获取ticket失败:' . $response['errmsg']]);
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取ticket成功',
|
||||
'data' => [
|
||||
'media_id' => $response['media_id'],
|
||||
'created_at' => $response['created_at'],
|
||||
'type' => $response['type']
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -30,7 +30,8 @@
|
||||
"phpoffice/phpspreadsheet": "^1.25",
|
||||
"phpmailer/phpmailer": "^6.9",
|
||||
"overtrue/wechat": "^5.36",
|
||||
"endroid/qr-code": "^4.6"
|
||||
"endroid/qr-code": "^4.6",
|
||||
"guzzlehttp/guzzle": "^7.9"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/var-dumper": "^4.2",
|
||||
|
||||
6
composer.lock
generated
6
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "50bdaec3985055faecd44de7a6b9a1f3",
|
||||
"content-hash": "4fd98121a1c29d8e7e638b3ae8591ac0",
|
||||
"packages": [
|
||||
{
|
||||
"name": "bacon/bacon-qr-code",
|
||||
@ -3991,12 +3991,12 @@
|
||||
],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": {},
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=7.2.5"
|
||||
},
|
||||
"platform-dev": {},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.6.0"
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user