更新wechat代码
This commit is contained in:
@@ -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']
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user