You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/common/service/qrcode/BaseQRcode.php

173 lines
6.7 KiB

11 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\common\service\qrcode;
8 months ago
use app\common\enum\Client as ClientEnum;
use app\common\library\Download;
use app\common\library\wechat\Qrcode as WechatQrcode;
11 months ago
use app\common\model\h5\Setting as H5SettingModel;
use app\common\model\wxapp\Setting as WxappSettingModel;
use app\common\service\BaseService;
use cores\exception\BaseException;
use EasyWeChat\Factory;
11 months ago
/**
* 二维码服务基类
* Class Base
* @package app\common\service\qrcode
*/
class BaseQRcode extends BaseService
{
/**
* 保存微二维码到文件
* @param int $storeId
* @param string $scene 场景值 (例如: uid:10001)
* @param string|null $page 页面地址
* @param string $channel 二维码渠道(小程序码、h5码)
* @return string
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getQrcode(int $storeId, string $scene, string $page = null, string $channel = 'H5'): string
{
// 根据指定渠道生成不同的二维码
if ($channel === ClientEnum::MP_WEIXIN) {
return $this->getMpWeiXinQrcode($storeId, $scene, $page, $channel);
}
return $this->getH5Qrcode($storeId, $scene, $page, $channel);
}
8 months ago
public function getOtherQrcode(int $storeId, string $url, string $channel = 'H5'): string
9 months ago
{
// 根据指定渠道生成不同的二维码
$savePath = $this->getFilePath($storeId, $url, "", $channel);
if (file_exists($savePath)) return $savePath;
// 生成二维码
\PHPQRCode\QRcode::png($url, $savePath, 'L', 15, 1);
return $savePath;
}
11 months ago
/**
* 保存微二维码到文件 (微信小程序码)
* @param int $storeId 商城ID
* @param string $scene 场景值 (例如: uid:10001)
* @param string|null $page 页面地址
* @param string $channel 二维码渠道(小程序码、h5码)
* @return string
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws BaseException
*/
protected function getMpWeiXinQrcode(int $storeId, string $scene, string $page = null, string $channel = 'H5'): string
{
// 获取二维码路径
$savePath = $this->getFilePath($storeId, $scene, $page, $channel);
if (file_exists($savePath)) {
return $savePath;
}
$pathInfo = pathinfo($savePath);
11 months ago
// 小程序配置信息
$wxConfig = WxappSettingModel::getConfigBasic($storeId);
$config = [
'app_id' => $wxConfig['app_id'],
'secret' => $wxConfig['app_secret'],
'response_type' => 'array',// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'log' => [
'level' => 'debug',
'file' => app()->getRuntimePath().'wechat.log',
],
];
$app = Factory::miniProgram($config);
$response = $app->app_code->getUnlimit($scene, [
'page' => $page,
'width' => 430,
]);
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$filename = $response->saveAs($pathInfo['dirname'], $pathInfo['basename']);
}
return $savePath;
// var_dump($content);
// exit();
11 months ago
// 请求api获取小程序码
// $Qrcode = new WechatQrcode($wxConfig['app_id'], $wxConfig['app_secret']);
// $content = $Qrcode->getQrcode($scene, $page);
11 months ago
// 保存到文件
// file_put_contents($savePath, $content);
// return $savePath;
11 months ago
}
/**
* 保存微二维码到文件 (H5二维码)
* @param int $storeId 商城ID
* @param string $scene 参数值 (例如: uid:10001)
* @param string|null $page 页面url
* @param string $channel 二维码渠道(小程序码、h5码)
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function getH5Qrcode(int $storeId, string $scene, string $page = null, string $channel = 'H5'): string
{
// 获取二维码路径
$savePath = $this->getFilePath($storeId, $scene, $page, $channel);
if (file_exists($savePath)) return $savePath;
// 二维码参数
$path = $page ?: 'pages/index/index';
$route = $path . (!empty($scene) ? '?scene=' . urlencode($scene) : null);
// 获取H5端的访问域名
$url = H5SettingModel::getH5Url($storeId) . "#/{$route}";
// 生成二维码
\PHPQRCode\QRcode::png($url, $savePath, 'L', 15, 1);
return $savePath;
}
/**
* 获取二维码路径
* @param int $storeId
* @param string $scene
* @param string|null $page
* @param string $channel 二维码渠道(小程序码、h5码)
* @return string
*/
private function getFilePath(int $storeId, string $scene, string $page = null, string $channel = 'H5'): string
{
// 文件目录
9 months ago
//$dirPath = runtime_root_path() . "image/{$storeId}";
8 months ago
$dirPath = root_path() . "public/" . "temp/$storeId";
11 months ago
!is_dir($dirPath) && mkdir($dirPath, 0755, true);
// 文件名称
8 months ago
$fileName = 'qrcode_' . md5("$storeId$scene$page-$channel") . '.png';
11 months ago
// 文件路径
8 months ago
return "$dirPath/$fileName";
11 months ago
}
/**
* 获取网络图片到临时目录
* @param int $storeId 商城ID
* @param string $url 图片链接
* @param string $prefix
* @return string
* @throws BaseException
*/
protected function saveTempImage(int $storeId, string $url, string $prefix = 'temp'): string
{
$Download = new Download;
return $Download->saveTempImage($storeId, $url, $prefix);
}
9 months ago
}