This commit is contained in:
尖叫 2023-12-07 14:27:07 +08:00
parent ccf87d323f
commit f335a773e2
3 changed files with 40 additions and 13 deletions

View File

@ -1777,20 +1777,13 @@ class Driver
function getAccessToken(int $applet_id = 0, int $type = 1) function getAccessToken(int $applet_id = 0, int $type = 1)
{ {
$access_token = ''; $access_token = '';
if ($type == 1) {
$appid = 'wx3668302906e3894f';
$app_secret = '3ef948febd085fb89908f22b4a79c52f';
}
if ($applet = Applet::getApplet(['applet_id' => $applet_id, 'status' => 1])) { if ($applet = Applet::getApplet(['applet_id' => $applet_id, 'status' => 1])) {
$access_token = $applet['access_token'];
if ($type == 1) {
$appid = 'wx3668302906e3894f';
$app_secret = '3ef948febd085fb89908f22b4a79c52f';
} else {
$appid = 'wx89c12dd426a55a2e';
$app_secret = '33e66bcf944f9810abbb5ddd7825403d';
}
if (empty($access_token) || time() > $applet['expires_in']) { if (empty($access_token) || time() > $applet['expires_in']) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$app_secret"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$app_secret";
$result = json_decode(Http::get($url), true); $result = json_decode(Http::get($url), true);
@ -1807,6 +1800,34 @@ class Driver
return $access_token; return $access_token;
} }
/**
* 获取令牌 - 开放平台
* $type 请求类型 1小程序2公众号
*/
function getWxAccessToken(int $applet_id = 0, $openid = '')
{
$appid = 'wx89c12dd426a55a2e';
$app_secret = '33e66bcf944f9810abbb5ddd7825403d';
$key = 'access_token' . $applet_id . $openid;
$access_token = Cache::get($key);
if (empty($access_token)) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$app_secret";
$result = json_decode(Http::get($url), true);
if (isset($result['access_token'])) {
$access_token = $result['access_token'];
Cache::set($key, $access_token, 3600);
return $access_token;
}
}
return $access_token;
}
/** /**
* 请求数据验证 * 请求数据验证
**/ **/

View File

@ -72,7 +72,7 @@ class Index
//发送公众号消息 //发送公众号消息
$wx = new Driver; $wx = new Driver;
$access_token = $wx->getAccessToken(10001, 2); $access_token = $wx->getWxAccessToken(10001, $openid);
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
$queryarr = [ $queryarr = [
'touser' => $openid, 'touser' => $openid,

View File

@ -0,0 +1,6 @@
[2023-12-07 10:51:52]
"{\"errcode\":40003,\"errmsg\":\"invalid openid rid: 6571334b-0f222da0-712c3848\"}"
[2023-12-07 14:25:09]
"{\"errcode\":41001,\"errmsg\":\"access_token missing rid: 65716545-36dcfa64-66d8f35c\"}"