92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace app\adminapi\controller\shortlink;
|
|
|
|
use app\adminapi\controller\BaseAdminController;
|
|
use app\adminapi\lists\shortlink\ShortLinkLists;
|
|
use app\adminapi\lists\shortlink\ShortLinkLogLists;
|
|
use app\common\model\ShortLink;
|
|
use app\common\service\ShortLinkPosterDataService;
|
|
use app\common\service\ShortLinkQrCodeService;
|
|
|
|
class ShortLinkController extends BaseAdminController
|
|
{
|
|
/**
|
|
* @notes 短链接列表
|
|
*/
|
|
public function lists()
|
|
{
|
|
return $this->dataLists(new ShortLinkLists());
|
|
}
|
|
|
|
/**
|
|
* @notes 短链接点击日志
|
|
*/
|
|
public function logLists()
|
|
{
|
|
return $this->dataLists(new ShortLinkLogLists());
|
|
}
|
|
|
|
/**
|
|
* @notes 删除短链接
|
|
*/
|
|
public function delete()
|
|
{
|
|
$id = $this->request->post('id/d', 0);
|
|
if (empty($id)) {
|
|
return $this->fail('参数错误');
|
|
}
|
|
ShortLink::destroy($id);
|
|
return $this->success('删除成功', [], 1, 1);
|
|
}
|
|
|
|
/**
|
|
* @notes 重新生成二维码
|
|
*/
|
|
public function regenerateQrcode()
|
|
{
|
|
$id = $this->request->post('id/d', 0);
|
|
if (empty($id)) {
|
|
return $this->fail('参数错误');
|
|
}
|
|
|
|
$link = ShortLink::findOrEmpty($id);
|
|
if ($link->isEmpty()) {
|
|
return $this->fail('短链接不存在');
|
|
}
|
|
|
|
// 删除旧二维码与海报合成文件
|
|
$oldUri = (string) $link->getData('qrcode');
|
|
$candidates = [];
|
|
if ($oldUri !== '') {
|
|
$candidates[] = $oldUri;
|
|
}
|
|
// 同时清理可能的同名残留文件(海报版与纯二维码版)
|
|
$candidates[] = 'uploads/qrcode/shortlink/' . $link->code . '.png';
|
|
$candidates[] = 'uploads/qrcode/shortlink/' . $link->code . '_poster.png';
|
|
$candidates[] = 'uploads/qrcode/shortlink/' . $link->code . '.svg';
|
|
foreach (array_unique($candidates) as $uri) {
|
|
$abs = public_path() . $uri;
|
|
if (is_file($abs)) {
|
|
@unlink($abs);
|
|
}
|
|
}
|
|
|
|
$shortUrl = request()->domain() . '/s/' . $link->code;
|
|
$posterData = ShortLinkPosterDataService::build(
|
|
(string) $link->page_type,
|
|
$link->page_id,
|
|
(int) $link->user_id
|
|
);
|
|
$qrcode = ShortLinkQrCodeService::create($shortUrl, $link->code, $posterData['poster_image_uri'], $posterData);
|
|
|
|
$link->qrcode = $qrcode['uri'];
|
|
$link->save();
|
|
|
|
return $this->success('重新生成成功', [
|
|
'qrcode' => $qrcode['uri'],
|
|
'qrcode_url' => $qrcode['url'],
|
|
], 1, 1);
|
|
}
|
|
}
|