This repository has been archived on 2024-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
yoshop/source/application/common/service/qrcode/Base.php
2021-07-16 16:31:39 +08:00

94 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\service\qrcode;
use app\common\exception\BaseException;
use app\common\library\wechat\Qrcode;
use app\common\model\Wxapp as WxappModel;
/**
* 二维码服务基类
* Class Base
* @package app\common\service\qrcode
*/
class Base
{
/**
* 构造方法
* Base constructor.
*/
public function __construct()
{
}
/**
* 保存小程序码到文件
* @param $wxappId
* @param $scene
* @param null $page
* @return string
* @throws \app\common\exception\BaseException
* @throws \think\Exception
* @throws \think\exception\DbException
*/
protected function saveQrcode($wxappId, $scene, $page = null)
{
// 文件目录
$dirPath = RUNTIME_PATH . 'image' . '/' . $wxappId;
!is_dir($dirPath) && mkdir($dirPath, 0755, true);
// 文件名称
$fileName = 'qrcode_' . md5($wxappId . $scene . $page) . '.png';
// 文件路径
$savePath = "{$dirPath}/{$fileName}";
if (file_exists($savePath)) return $savePath;
// 小程序配置信息
$wxConfig = WxappModel::getWxappCache($wxappId);
// 请求api获取小程序码
$Qrcode = new Qrcode($wxConfig['app_id'], $wxConfig['app_secret']);
$content = $Qrcode->getQrcode($scene, $page);
// 保存到文件
file_put_contents($savePath, $content);
return $savePath;
}
/**
* 获取网络图片到临时目录
* @param $wxappId
* @param $url
* @param string $mark
* @return string
* @throws BaseException
*/
protected function saveTempImage($wxappId, $url, $mark = 'temp')
{
$dirPath = RUNTIME_PATH . 'image' . '/' . $wxappId;
!is_dir($dirPath) && mkdir($dirPath, 0755, true);
$savePath = $dirPath . '/' . $mark . '_' . md5($url) . '.png';
if (file_exists($savePath)) return $savePath;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$img = curl_exec($ch);
if ($img === false) {
$this->throwError('CURL错误' . curl_error($ch));
}
curl_close($ch);
$fp = fopen($savePath, 'w');
fwrite($fp, $img);
fclose($fp);
return $savePath;
}
/**
* 返回错误信息
* @param $msg
* @throws BaseException
*/
private function throwError($msg)
{
throw new BaseException(['msg' => $msg]);
}
}