You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/common/service/qrcode/InviteUser.php

127 lines
3.7 KiB

10 months ago
<?php
namespace app\common\service\qrcode;
use app\common\model\User as UserModel;
use cores\exception\BaseException;
use Exception;
use Grafika\Grafika;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use function base_url;
class InviteUser extends BaseQRcode
{
// 当前客户端 (默认H5)
private string $channel;
// 用户信息
private UserModel $userInfo;
8 months ago
private int $type = 1;
10 months ago
/**
* 构造方法
* Goods constructor.
* @param UserModel $userInfo
* @param string $channel 二维码渠道(小程序码、h5码)
*/
8 months ago
public function __construct(UserModel $userInfo, string $channel = 'H5', int $type = 1)
10 months ago
{
parent::__construct();
8 months ago
$this->type = $type;
10 months ago
// 用户信息
$this->userInfo = $userInfo;
// 当前客户端
$this->channel = $channel;
}
/**
* 拼接海报图
* @return string
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
8 months ago
public function getImage(): string
10 months ago
{
// 判断海报图文件存在则直接返回url
8 months ago
// if (file_exists($this->getPosterPath())) {
// return $this->getPosterUrl();
// }
10 months ago
// 商城ID
$storeId = $this->userInfo['store_id'];
$backdrop = __DIR__ . '/resource/invite_user.png';
8 months ago
// 商品海报背景图
8 months ago
if ($this->type != 1) {
8 months ago
$backdrop = __DIR__ . '/resource/invite_shop.png';
}
10 months ago
// 小程序码参数
$scene = "refereeId:" . ($this->userInfo['user_id'] ?: '');
// 下载小程序码
10 months ago
$page = 'pages/login/index';
10 months ago
$qrcode = $this->getQrcode($storeId, $scene, $page, $this->channel);
8 months ago
//获取图片名称
$arr = explode('/', $qrcode);
$name = end($arr);
return base_url() . 'temp/' . $this->userInfo['store_id'] . '/' . $name . '?t=' . time();
10 months ago
// 拼接海报图
8 months ago
//return $this->savePoster($backdrop, $qrcode);
10 months ago
}
/**
* 拼接海报图
* @param string $backdrop 背景图路径
* @param string $qrcode 二维码图路径
* @return string
* @throws Exception
*/
private function savePoster(string $backdrop, string $qrcode): string
{
// 实例化图像编辑器
$editor = Grafika::createEditor(['Gd']);
// 打开海报背景图
$editor->open($backdropImage, $backdrop);
// 打开小程序码
$editor->open($qrcodeImage, $qrcode);
// 重设小程序码宽高
8 months ago
$editor->resizeExact($qrcodeImage, 110, 110);
10 months ago
// 小程序码添加到背景图
8 months ago
$editor->blend($backdropImage, $qrcodeImage, 'normal', 1.0, 'top-left', 175, 555);
10 months ago
// 保存图片
$editor->save($backdropImage, $this->getPosterPath());
return $this->getPosterUrl();
}
/**
* 海报图文件路径
* @return string
*/
private function getPosterPath(): string
{
// 保存路径
$tempPath = web_path() . "temp/{$this->userInfo['store_id']}/";
!is_dir($tempPath) && mkdir($tempPath, 0755, true);
return $tempPath . $this->getPosterName();
}
/**
* 海报图文件名称
* @return string
*/
private function getPosterName(): string
{
8 months ago
return 'invite_' . md5("{$this->userInfo['user_id']}_$this->channel" . $this->type) . '.png';
10 months ago
}
/**
* 海报图url
* @return string
*/
private function getPosterUrl(): string
{
return base_url() . 'temp/' . $this->userInfo['store_id'] . '/' . $this->getPosterName() . '?t=' . time();
}
}