123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace liuniu;
- use app\admin\model\wechat\WechatQrcode as QrcodeModel;
- use app\common\model\Attachment;
- use think\Request;
- class QrcodeService
- {
- /**
- * 获取临时二维码 单个
- * @param $type
- * @param $id
- * @return array
- */
- public static function getTemporaryQrcode($type, $id)
- {
- return QrcodeModel::getTemporaryQrcode($type, $id)->toArray();
- }
- /**
- * 获取永久二维码 单个
- * @param $type
- * @param $id
- * @return array
- */
- public static function getForeverQrcode($type, $id)
- {
- return QrcodeModel::getForeverQrcode($type, $id)->toArray();
- }
- /**
- * 从数据库获取二维码
- * @param $id
- * @param string $type
- * @return array|\think\Model|null
- */
- public static function getQrcode($id, $type = 'id')
- {
- return QrcodeModel::getQrcode($id, $type);
- }
- /**
- * 增加某个二维码扫描的次数
- * @param $id
- * @param string $type
- * @return mixed
- */
- public static function scanQrcode($id, $type = 'id')
- {
- return QrcodeModel::scanQrcode($id, $type);
- }
- /**
- * 获取二维码完整路径,不存在则自动生成
- * @param string $name 路径名
- * @param string $link 需要生成二维码的跳转路径
- * @param int $type https 1 = http , 0 = https
- * @param bool $force 是否返回false
- * @return bool|mixed|string
- */
- public static function getWechatQrcodePath(string $name, string $link, int $type = 1, bool $force = false)
- {
- try {
- $imageInfo = Attachment::getInfo($name, 'name');
- $siteUrl = Request::instance()->domain();
- if (!$imageInfo) {
- $codeUrl = UtilService::setHttpType($siteUrl . $link, $type);//二维码链接
- $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
- if (is_string($imageInfo) && $force)
- return false;
- if (is_array($imageInfo)) {
- SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
- $url = $imageInfo['dir'];
- } else {
- $url = '';
- $imageInfo = ['image_type' => 0];
- }
- } else $url = $imageInfo['att_dir'];
- if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
- return $url;
- } catch (\Throwable $e) {
- if ($force)
- return false;
- else
- return '';
- }
- }
- /**
- * 获取二维码
- * @param $url
- * @param $name
- * @return array|bool|string
- */
- public static function getQRCodePath($url, $name)
- {
- if (!strlen(trim($url)) || !strlen(trim($name))) return false;
- try {
- $uploadType = 1;
- //TODO 没有选择默认使用本地上传
- if (!$uploadType) $uploadType = 1;
- $uploadType = (int)$uploadType;
- $siteUrl = sys_config('site_url');
- if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
- $info = [];
- $outfile = Config::get('qrcode.cache_dir');
- $code = new QRcode();
- $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
- $content = file_get_contents('.' . $wapCodePath);
- if ($uploadType === 1) {
- $info["code"] = 200;
- $info["name"] = $name;
- $info["dir"] = $wapCodePath;
- $info["time"] = time();
- $info['size'] = 0;
- $info['type'] = 'image/png';
- $info["image_type"] = 1;
- $info['thumb_path'] = $wapCodePath;
- return $info;
- } else {
- $upload = new Upload($uploadType, [
- 'accessKey' => sys_config('accessKey'),
- 'secretKey' => sys_config('secretKey'),
- 'uploadUrl' => sys_config('uploadUrl'),
- 'storageName' => sys_config('storage_name'),
- 'storageRegion' => sys_config('storage_region'),
- ]);
- $res = $upload->to($outfile)->validate()->stream($content, $name);
- if ($res === false) {
- return $upload->getError();
- }
- $info = $upload->getUploadInfo();
- $info['image_type'] = $uploadType;
- return $info;
- }
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- }
|