|
|
<?php
|
|
|
|
|
|
namespace tencent\wechat\mpweixin;
|
|
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
|
|
class Qrcode extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 获取小程序码(适用于需要的码数量较少的业务场景)
|
|
|
* @param string $path 扫码进入的小程序页面路径,最大长度 128 字节,不能为空
|
|
|
* @param string $file_dirname 文件夹路径
|
|
|
* @param string $file_name 文件名称(必传,可能是底层BUG,不传会报错)
|
|
|
* @param array $optional 可以定义二维码宽度、线条颜色等
|
|
|
* @date 2022-08-24
|
|
|
*/
|
|
|
public function getQRCode($path, $file_dirname, $file_name = '', $optional = [])
|
|
|
{
|
|
|
// 如果没有传文件名称,md5加密时间戳命名 !!!
|
|
|
if (empty($file_name)) {
|
|
|
$file_name = md5(microtime(true));
|
|
|
}
|
|
|
|
|
|
// 实例类
|
|
|
$app = Factory::miniProgram($this->mpWeixinConfig);
|
|
|
|
|
|
|
|
|
// 获取小程序码
|
|
|
$response = $app->app_code->get($path, $optional);
|
|
|
|
|
|
// $response 成功时为 EasyWeChat\Kernel\Http\StreamResponse 实例,失败时为数组或者你指定的 API 返回格式
|
|
|
|
|
|
// 保存小程序码到文件
|
|
|
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
|
|
|
|
|
|
if ($file_name) {
|
|
|
// 自定义文件名,不需要带后缀
|
|
|
$result = $response->saveAs($file_dirname, $file_name);
|
|
|
} else {
|
|
|
// 以内容 md5 为文件名存到本地
|
|
|
$result = $response->save($file_dirname);
|
|
|
}
|
|
|
|
|
|
// 返回为文件名,签名拼接保存目录
|
|
|
return $file_dirname . '/' . $result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取小程序二维码(适用于需要的码数量较少的业务场景)
|
|
|
* @param string $path 扫码进入的小程序页面路径,最大长度 128 字节,不能为空
|
|
|
* @param string $file_dirname 文件夹路径
|
|
|
* @param string $file_name 文件名称(必传,可能是底层BUG,不传会报错)
|
|
|
* @param integer $width 二维码宽度
|
|
|
* @date 2022-08-24
|
|
|
*/
|
|
|
public function createQRCode($path, $file_dirname, $file_name = '', $width = null)
|
|
|
{
|
|
|
// 如果没有传文件名称,md5加密时间戳命名 !!!
|
|
|
if (empty($file_name)) {
|
|
|
$file_name = md5(microtime(true));
|
|
|
}
|
|
|
|
|
|
// 实例类
|
|
|
$app = Factory::miniProgram($this->mpWeixinConfig);
|
|
|
|
|
|
|
|
|
// 获取小程序码
|
|
|
$response = $app->app_code->getQrCode($path, $width);
|
|
|
|
|
|
// $response 成功时为 EasyWeChat\Kernel\Http\StreamResponse 实例,失败时为数组或者你指定的 API 返回格式
|
|
|
|
|
|
// 保存小程序码到文件
|
|
|
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
|
|
|
|
|
|
if ($file_name) {
|
|
|
// 自定义文件名,不需要带后缀
|
|
|
$result = $response->saveAs($file_dirname, $file_name);
|
|
|
} else {
|
|
|
// 以内容 md5 为文件名存到本地
|
|
|
$result = $response->save($file_dirname);
|
|
|
}
|
|
|
|
|
|
// 返回为文件名,签名拼接保存目录
|
|
|
return $file_dirname . '/' . $result;
|
|
|
} else {
|
|
|
return $response;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取小程序二维码(适用于需要的码数量较少的业务场景)
|
|
|
* @param string $path 默认是主页,页面 page,例如 pages/index/index,根路径前不要填加 /,不能携带参数(参数请放在 scene 字段里)
|
|
|
* @param string $scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符
|
|
|
* @param string $file_dirname 文件夹路径
|
|
|
* @param string $file_name 文件名称(必传,可能是底层BUG,不传会报错)
|
|
|
* @param array $optional 可以定义二维码宽度、线条颜色等
|
|
|
* @date 2022-08-24
|
|
|
*/
|
|
|
public function getUnlimitedQRCode($path, $scene, $file_dirname, $file_name = '', $optional = [])
|
|
|
{
|
|
|
if (empty($optional)) {
|
|
|
$optional = [
|
|
|
'page' => $path
|
|
|
];
|
|
|
} else {
|
|
|
$optional = array_merge([
|
|
|
'page' => $path
|
|
|
], $optional);
|
|
|
}
|
|
|
|
|
|
// 实例类
|
|
|
$app = Factory::miniProgram($this->mpWeixinConfig);
|
|
|
|
|
|
|
|
|
// 获取小程序码
|
|
|
$response = $app->app_code->getUnlimit($scene, $optional);
|
|
|
|
|
|
// $response 成功时为 EasyWeChat\Kernel\Http\StreamResponse 实例,失败时为数组或者你指定的 API 返回格式
|
|
|
|
|
|
// 保存小程序码到文件
|
|
|
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
|
|
|
|
|
|
if ($file_name) {
|
|
|
// 自定义文件名,不需要带后缀
|
|
|
$result = $response->saveAs($file_dirname, $file_name);
|
|
|
} else {
|
|
|
// 以内容 md5 为文件名存到本地
|
|
|
$result = $response->save($file_dirname);
|
|
|
}
|
|
|
|
|
|
// 返回为文件名,签名拼接保存目录
|
|
|
return $file_dirname . '/' . $result;
|
|
|
} else {
|
|
|
return $response;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|