<?php
namespace addons\gaga\library\cqrs\libs;
use think\exception\ValidateException;
class Wxaqrcode
{
protected $wxapp_settings;
protected $default_wxapp_settings;
private $appId;
private $appSecret;
private $access_token;
private $error = '';
public function __construct($app_id,$app_secret)
{
if (empty($app_id) || empty($app_secret)) throw new \think\Exception('小程序参数错误');
$this->appId = $app_id;
$this->appSecret = $app_secret;
$access_token = \think\Cache::get('wxa:access_token:' . $this->appId);
if (!$access_token) {
$target_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appId . "&secret=" . $this->appSecret;
$res = $this->http_get($target_url);
$res = json_decode($res, true);
if (isset($res['errcode']) && $res['errcode'] != 0) {
$content = '';
switch (!$res || $res['errcode']) {
case 40001:
$content = "AppSecret错误或者AppSecret不属于这个小程序,请开发者确认AppSecret的正确性";
break;
case 40164:
$content = "调用接口的IP地址不在白名单中,请在接口IP白名单中进行设置";
break;
}
throw new ValidateException($content);
}
$access_token = $res['access_token'];
$expire = $res['expires_in'] ? intval($res['expires_in']) : 7200;
\think\Cache::set('wxa:access_token:' . $this->appId, $res['access_token'], $expire);
}
$this->access_token = $access_token;
}
public function getWxQrcode($page = 'pages/index/index', $scene = '', $width = 430, $filepath = null, $qrtype = 1, $device_no = '', $loop = '')
{
$width = $width ? $width : 430;
if (!file_exists($filepath)) {
mkdir($filepath, 0777, true);
}
if ($qrtype == 1) {
$file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $this->access_token;
$param = json_encode(array(
"scene" => $scene,
"page" => $page,
"width" => $width
));
$res = $this->http_post($url, $param);
if (is_array($res2 = json_decode($res, true))) {
$this->error = $res2['errmsg'];
return false;
}
$res = file_put_contents($file, $res);
if ($res === false) {
$this->error = '小程序码文件写入失败';
return false;
}
} else {
$file = rtrim($filepath, DS) . DS . uniqid() . '.png';
if (!class_exists(\addons\qrcode\library\Service::class)) {
throw new ValidateException('请先安装插件《二维码生成》', []);
}
$qrcodeService = \addons\qrcode\library\Service::qrcode([
'text' => 'https://' . $_SERVER['HTTP_HOST'] . '/UserScan/Charge?station=' . $device_no . '&loop=' . $loop,
'errorlevel' => 'medium',
'size' => $width,
'format' => 'png',
'padding' => 3,
]);
$filePath = $file;
$qrcodeService->writeFile($filePath);
}
if ($page == 'pages/station_led/station_led') {
$qCodeWidth = $width;
$qCodeHight = $width;
$bigImg = imagecreatetruecolor(450, 500);
$white = imagecolorallocate($bigImg, 255, 255, 255);
imagefill($bigImg, 0, 0, $white);
$qCodeImg = imagecreatefromstring(file_get_contents($file));
imagecopyresampled($bigImg, $qCodeImg, 10, 0, 0, 0, 430, 430, $qCodeWidth, $qCodeHight);
$fonttype = './assets/addons/gaga/fonts/wqy-microhei.ttc';
$fontcolor = imagecolorallocate($bigImg, 0x00, 0x00, 0x00);
$content = $this->default_wxapp_settings['app_name'] . ':' . $scene;
$fontSize = 25;
$fontBox = imagettfbbox($fontSize, 0, $fonttype, $content);
$width = imagesx($bigImg);
imagettftext($bigImg, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $qCodeHight + 55, $fontcolor, $fonttype, $content);
@unlink($file);
$file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
imagejpeg($bigImg, $file);
} else if ($page == 'pages/station_del/station_del') {
$qCodeWidth = $width;
$qCodeHight = $width;
$bigImg = imagecreatetruecolor(450, 500);
$white = imagecolorallocate($bigImg, 255, 255, 255);
imagefill($bigImg, 0, 0, $white);
$qCodeImg = imagecreatefromstring(file_get_contents($file));
imagecopyresampled($bigImg, $qCodeImg, 10, 0, 0, 0, 430, 430, $qCodeWidth, $qCodeHight);
$fonttype = './assets/addons/gaga/fonts/wqy-microhei.ttc';
$fontcolor = imagecolorallocate($bigImg, 0x00, 0x00, 0x00);
$tmp = substr($scene, 6);
list($tmp_no, $tmp_loop) = explode('loop', $tmp);
$content = $tmp_no . '-' . $tmp_loop;
$fontSize = 25;
$fontBox = imagettfbbox($fontSize, 0, $fonttype, $content);
$width = imagesx($bigImg);
imagettftext($bigImg, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $qCodeHight + 55, $fontcolor, $fonttype, $content);
@unlink($file);
$file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
imagejpeg($bigImg, $file);
}
return $file;
}
public function getError()
{
return $this->error;
}
public function getQrcode($page = 'pages/index/index', $width = '150', $filepath = null)
{
$width = $width ? $width : 150;
$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" . $this->access_token;
$param = json_encode(array("path" => $page, "width" => $width));
$res = $this->http_post($url, $param);
if (is_array($res2 = json_decode($res, true))) {
$this->error = $res2['errmsg'];
return false;
}
if ($filepath) {
if (!file_exists($filepath)) {
mkdir($filepath, 0777, true);
}
$file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
$res = file_put_contents($file, $res);
if ($res === false) {
return false;
}
return $file;
} else {
header('content-type:image/jpeg');
die($res);
}
}
private function http_get($url)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
private function http_post($url, $param, $post_file = false)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
}