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

79 lines
2.2 KiB
PHP
Raw Normal View History

2020-04-25 22:20:29 +08:00
<?php
namespace app\common\service\qrcode;
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()
{
}
/**
* 保存小程序码到文件
2020-08-28 10:41:05 +08:00
* @param $wxappId
2020-04-25 22:20:29 +08:00
* @param $scene
* @param null $page
* @return string
* @throws \app\common\exception\BaseException
2020-08-28 10:41:05 +08:00
* @throws \think\Exception
2020-04-25 22:20:29 +08:00
* @throws \think\exception\DbException
*/
2020-08-28 10:41:05 +08:00
protected function saveQrcode($wxappId, $scene, $page = null)
2020-04-25 22:20:29 +08:00
{
// 文件目录
2020-08-28 10:41:05 +08:00
$dirPath = RUNTIME_PATH . 'image' . '/' . $wxappId;
2020-04-25 22:20:29 +08:00
!is_dir($dirPath) && mkdir($dirPath, 0755, true);
// 文件名称
2020-08-28 10:41:05 +08:00
$fileName = 'qrcode_' . md5($wxappId . $scene . $page) . '.png';
2020-04-25 22:20:29 +08:00
// 文件路径
$savePath = "{$dirPath}/{$fileName}";
if (file_exists($savePath)) return $savePath;
// 小程序配置信息
2020-08-28 10:41:05 +08:00
$wxConfig = WxappModel::getWxappCache($wxappId);
2020-04-25 22:20:29 +08:00
// 请求api获取小程序码
$Qrcode = new Qrcode($wxConfig['app_id'], $wxConfig['app_secret']);
$content = $Qrcode->getQrcode($scene, $page);
// 保存到文件
file_put_contents($savePath, $content);
return $savePath;
}
/**
* 获取网络图片到临时目录
2020-08-28 10:41:05 +08:00
* @param $wxappId
2020-04-25 22:20:29 +08:00
* @param $url
* @param string $mark
* @return string
*/
2020-08-28 10:41:05 +08:00
protected function saveTempImage($wxappId, $url, $mark = 'temp')
2020-04-25 22:20:29 +08:00
{
2020-08-28 10:41:05 +08:00
$dirPath = RUNTIME_PATH . 'image' . '/' . $wxappId;
2020-04-25 22:20:29 +08:00
!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);
curl_close($ch);
$fp = fopen($savePath, 'w');
fwrite($fp, $img);
fclose($fp);
return $savePath;
}
2020-08-28 10:41:05 +08:00
}