master
ELF 5 years ago
parent 7bc6cac01a
commit d29984aaeb

@ -55,26 +55,42 @@ class HomeController extends Controller
$secret = 'd3219c70a45c5e1954be4608b288d38b'; $secret = 'd3219c70a45c5e1954be4608b288d38b';
$tokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret; $tokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
$result = file_get_contents($tokenUrl); $content = file_get_contents(RUNTIME_PATH . 'Data/wechat_ticket');
$result = json_decode($result, true); $ticketData = null;
if (!$result || isset($result['errcode'])) { if ($content) {
$this->ajaxReturn([ $ticketData = json_decode($content, true);
'status' => false,
'msg' => '获取失败',
]);
} }
$accessToken = $result['access_token'];
$isExpired = false;
$ticketUrl = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $accessToken . '&type=jsapi'; if ($ticketData) {
$result = file_get_contents($ticketUrl); $isExpired = time() > $ticketData['expired_at'] ? true : false;
$result = json_decode($result, true); }
if (!$result || $result['errcode'] != 0) {
$this->ajaxReturn([ $ticket = '';
'status' => false, if (!$ticketData || $isExpired) {
'msg' => '获取失败', $ticket = $ticketData['ticket'];
]); $result = file_get_contents($tokenUrl);
$result = json_decode($result, true);
if (!$result || isset($result['errcode'])) {
$this->ajaxReturn([
'status' => false,
'msg' => '获取失败',
]);
}
$accessToken = $result['access_token'];
$ticketUrl = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $accessToken . '&type=jsapi';
$result = file_get_contents($ticketUrl);
$result = json_decode($result, true);
if (!$result || $result['errcode'] != 0) {
$this->ajaxReturn([
'status' => false,
'msg' => '获取失败',
]);
}
$ticket = $result['ticket'];
file_put_contents(RUNTIME_PATH . 'Data/wechat_ticket', json_encode(['ticket' => $ticket, 'expired_at' => time() + 3600]));
} }
$ticket = $result['ticket'];
$randStr = 'aaabbbxeclsx'; $randStr = 'aaabbbxeclsx';
$time = time(); $time = time();

Loading…
Cancel
Save