This commit is contained in:
2025-01-23 20:02:06 +03:00
parent 485c11de5f
commit 08cdf44b67
16 changed files with 240 additions and 23 deletions

View File

@ -3,6 +3,8 @@
namespace app\modules\tgbot\controllers;
use app\modules\tgbot\models\Tgbot;
use kernel\app_modules\tag\service\TagService;
use kernel\modules\post\models\Post;
class TgBotRestController extends \kernel\app_modules\tgbot\controllers\TgBotRestController
{
@ -12,9 +14,15 @@ class TgBotRestController extends \kernel\app_modules\tgbot\controllers\TgBotRes
$dialog = Tgbot::where("dialog_id", $id)->first();
if ($dialog){
$this->renderApi([
'html' => '<a class="btn btn-primary" href="/miniapp/scan">Сканировать</a>',
'html' => '<a class="btn btn-primary" href="/miniapp/scanner">Сканировать</a>',
]);
}
}
public function actionGetNews(): void
{
}
}

View File

@ -3,7 +3,9 @@
namespace app\modules\tgbot\controllers;
use app\modules\tgbot\models\Tgbot;
use app\modules\tgbot\services\TgBotService;
use Cassandra\Decimal;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\tag\service\TagService;
use kernel\Controller;
use kernel\helpers\Debug;
@ -40,4 +42,16 @@ class TgMainController extends Controller
$this->cgView->render("news.php", ['news' => $news]);
}
public function actionScanner(): void
{
$this->cgView->render("scanner.php");
}
public function actionCardAction(int $cardId): void
{
$card = Card::where("id", $cardId)->first();
$this->cgView->render("card_action.php", ['card' => $card]);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace app\modules\tgbot\middlewares;
use app\modules\tgbot\models\Tgbot;
use app\modules\tgbot\services\TgBotService;
use kernel\Middleware;
class TgBotAuthMiddleware extends Middleware
{
function handler(): void
{
if(isset($_COOKIE['dialog_id']))
{
$model = Tgbot::where("dialog_id", $_COOKIE['dialog_id'])->first();
if ($model){
TgBotService::$currentDialog = $model;
return;
}
}
exit();
}
}

View File

@ -5,9 +5,14 @@ use kernel\CgRouteCollector;
include KERNEL_APP_MODULES_DIR . "/tgbot/routs/tgbot.php";
App::$collector->filter("tg_bot_auth", [\app\modules\tgbot\middlewares\TgBotAuthMiddleware::class, "handler"]);
App::$collector->group(["prefix" => "miniapp"], function (CGRouteCollector $router) {
App::$collector->get('/', [\app\modules\tgbot\controllers\TgMainController::class, 'actionMain']);
App::$collector->get('/news', [\app\modules\tgbot\controllers\TgMainController::class, 'actionNews']);
App::$collector->get('/promo', [\app\modules\tgbot\controllers\TgMainController::class, 'actionPromo']);
App::$collector->group(["before" => "tg_bot_auth"], function (CGRouteCollector $router){
App::$collector->get('/news', [\app\modules\tgbot\controllers\TgMainController::class, 'actionNews']);
App::$collector->get('/promo', [\app\modules\tgbot\controllers\TgMainController::class, 'actionPromo']);
App::$collector->get('/scanner', [\app\modules\tgbot\controllers\TgMainController::class, 'actionScanner']);
App::$collector->get('/card_action/{cardId}', [\app\modules\tgbot\controllers\TgMainController::class, 'actionCardAction']);
});
});

View File

@ -12,4 +12,6 @@ use kernel\services\ModuleService;
class TgBotService extends \kernel\app_modules\tgbot\services\TgBotService
{
public static null|Tgbot $currentDialog = null;
}

View File

@ -22,6 +22,7 @@
<link rel="stylesheet" href="<?= $resources ?>/css/style.css">
<link rel="stylesheet" href="<?= $resources ?>/css/tgApp.css">
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script src="https://unpkg.com/html5-qrcode" type="text/javascript"></script>
</head>
<body>

View File

@ -0,0 +1,23 @@
<?php
/**
* @var \kernel\app_modules\card\models\Card $card
*/
use kernel\helpers\Html;
?>
<div class="row">
<div class="col-12 m-1">
<?php
echo Html::img(src: "data:image/png;base64, " . \kernel\app_modules\card\services\CardFileService::createCardPNG($card, true));
?>
</div>
</div>
<div class="row">
<div class="col-12 m-1">
<a class="btn btn-primary" href="/miniapp/add_purchase">Добавить покупку</a>
</div>
</div>

View File

@ -0,0 +1,30 @@
<div id="tg_app">
</div>
<div id="scanner_box"></div>
<script>
function domReady(fn) {
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
setTimeout(fn, 1000);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
domReady(function () {
// If found you qr code
function onScanSuccess(decodeText, decodeResult) {
window.location.href = "/miniapp/card_action/" + decodeText;
}
let htmlscanner = new Html5QrcodeScanner(
"scanner_box",
{fps: 10, qrbox: {width: 250, height: 250}},
);
htmlscanner.render(onScanSuccess);
});
</script>