76 lines
1.9 KiB
PHP
76 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace officialaccount;
|
|
|
|
|
|
use common\HttpClient;
|
|
use common\Result;
|
|
|
|
class QrCode extends AfficialAccount
|
|
{
|
|
|
|
private $tick_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
|
private $temporary_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
|
|
private $permanent_qr_code_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
|
|
|
/**
|
|
* @param $scene_id
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public function temporaryQrCode($scene_id)
|
|
{
|
|
$requestParam['expire_seconds'] = 24 * 3600 * 30;
|
|
$requestParam['action_name'] = 'QR_SCENE';
|
|
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
|
|
|
$tick = $this->get_tick($requestParam);
|
|
|
|
$client = HttpClient::NewRequest();
|
|
return $client->get($this->temporary_url, ['ticket' => $tick]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $scene_id
|
|
* @return array|mixed|\common\Result
|
|
* @throws \Exception
|
|
*/
|
|
public function permanent_qr_code($scene_id)
|
|
{
|
|
$requestParam['action_name'] = 'QR_LIMIT_SCENE';
|
|
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
|
|
|
$tick = $this->get_tick($requestParam);
|
|
|
|
$client = HttpClient::NewRequest();
|
|
return $client->get($this->permanent_qr_code_url, ['ticket' => $tick]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $requestParam
|
|
* @return mixed
|
|
* @throws \Exception
|
|
* tick
|
|
*/
|
|
private function get_tick($requestParam)
|
|
{
|
|
$request = HttpClient::NewRequest();
|
|
$request->setCallback(function ($body) {
|
|
if (is_array($body)) {
|
|
$param = ['code' => $body['errcode'], 'message' => $body['errmsg'], 'data' => $body];
|
|
} else {
|
|
$param = ['code' => 0, 'data' => json_decode($body, true)];
|
|
}
|
|
return new Result($param);
|
|
});
|
|
$data = $request->post($this->tick_url . $this->config->getAccessToken(), json_encode($requestParam));
|
|
if (!$data->isResultsOK()) {
|
|
throw new \Exception($data->getMessage());
|
|
}
|
|
return $data->getData('ticket');
|
|
}
|
|
|
|
}
|