123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace library\services;
- use app\model\routine\RoutineCode;
- use app\model\system\Attachment;
- use app\model\wechat\WechatQrcode as QrcodeModel;
- class QrcodeService
- {
- /**
- * 获取临时二维码 单个
- * */
- public static function getTemporaryQrcode($type, $id)
- {
- return QrcodeModel::getTemporaryQrcode($type, $id)->toArray();
- }
- /**
- * 获取永久二维码 单个
- * */
- public static function getForeverQrcode($type, $id)
- {
- return QrcodeModel::getForeverQrcode($type, $id)->toArray();
- }
- public static function getQrcode($id, $type = 'id')
- {
- return QrcodeModel::getQrcode($id, $type);
- }
- 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, bool $force = false)
- {
- try {
- $imageInfo = Attachment::getInfo($name, 'name');
- $siteUrl = config('app')['API_URL'];
- if (!$imageInfo) {
- $codeUrl = UtilService::setHttpType($siteUrl . $link, request()->isSsl() ? 0 : 1);//二维码链接
- $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
- if (is_string($imageInfo) && $force)
- return false;
- if (is_array($imageInfo)) {
- Attachment::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 int $id
- * @param int $uid
- * @param int $type 1 = 拼团,2 = 秒杀
- * @return bool|string
- */
- public static function getRoutineQrcodePath(int $id, int $uid, int $type, array $parame = [])
- {
- $page = '';
- $namePath = '';
- $data = 'id=' . $id . '&pid=' . $uid;
- switch ($type) {
- case 1:
- $page = 'pages/activity/goods_combination_details/index';
- $namePath = 'combination_' . $id . '_' . $uid . '.jpg';
- break;
- case 2:
- $page = 'pages/activity/goods_seckill_details/index';
- $namePath = 'seckill_' . $id . '_' . $uid . '.jpg';
- if (isset($parame['stop_time']) && $parame['stop_time']) {
- $data .= '&time=' . $parame['stop_time'];
- $namePath = $parame['stop_time'] . $namePath;
- }
- break;
- }
- if (!$page || !$namePath) {
- return false;
- }
- try {
- $imageInfo = Attachment::getInfo($namePath, 'name');
- $siteUrl = config('app')['API_URL'];
- if (!$imageInfo) {
- $res = RoutineCode::getPageCode($page, $data, 280);
- if (!$res) return false;
- $uploadType = (int)sys_config('upload_type', 1);
- $upload = UploadService::init();
- $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
- if ($res === false) {
- return false;
- }
- $imageInfo = $upload->getUploadInfo();
- $imageInfo['image_type'] = $uploadType;
- if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
- else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
- if (!$remoteImage['status']) return false;
- Attachment::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['att_dir'];
- if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
- return $url;
- } catch (\Throwable $e) {
- return false;
- }
- }
- }
|