card page

This commit is contained in:
Kavalar 2025-01-05 17:23:49 +03:00
parent 3ca3d48ffe
commit 40369fb515
13 changed files with 145 additions and 36 deletions

3
.gitignore vendored
View File

@ -5,4 +5,5 @@ views_cache
resources/upload
resources/tmp
resources/cards
composer.lock
composer.lock
resources/main/js/tg_app/config_local.js

View File

@ -20,6 +20,7 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
<link rel="stylesheet" href="<?= $resources ?>/css/bootstrap.min.css">
<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>
</head>
<body>
@ -40,16 +41,10 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="/admin/logout">Выход</a>
<a class="nav-link" href="/miniapp/card">Карта</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Portfolio</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
<a class="nav-link" href="/miniapp/sale">Акции</a>
</li>
</ul>
</div>
@ -67,20 +62,19 @@
<button type="button" class="btn-close closeAlertBtn"></button>
</div>
<?php endif; ?>
<div id="username_tag"></div>
<div id="user_id_tag"></div>
<?= $content ?>
</div>
</div>
<script src="<?= $resources ?>/js/jquery.min.js"></script>
<script src="<?= $resources ?>/js/popper.js"></script>
<script src="<?= $resources ?>/js/bootstrap.min.js"></script>
<script src="<?= $resources ?>/js/main.js"></script>
<script type="module" src="<?= $resources ?>/js/tg.js"></script>
<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
<script>
// Initialize Eruda
eruda.init();
</script>
<script src="<?= $resources ?>/js/jquery.min.js"></script>
<script src="<?= $resources ?>/js/popper.js"></script>
<script src="<?= $resources ?>/js/bootstrap.min.js"></script>
<script src="<?= $resources ?>/js/main.js"></script>
<script src="<?= $resources ?>/js/tg.js"></script>
</body>
</html>

View File

@ -1,3 +1 @@
<?php
echo "Hello Telegram";
<div id="tg_app"></div>

View File

@ -30,8 +30,8 @@ class CardService
$model->info = $form_model->getItem('info') ?? 42;
$model->program = $form_model->getItem('program') ?? 74;
$model->balance = $form_model->getItem('balance') ?? 0;
$model->cvc = $form_model->getItem('cvc');
$model->pin = $form_model->getItem('pin');
$model->cvc = $form_model->getItem('cvc') ?? 101;
$model->pin = $form_model->getItem('pin') ?? 1111;
$model->username = $form_model->getItem('username');
$model->card_template_id = $form_model->getItem('card_template_id');
$model->status = $form_model->getItem('status');

View File

@ -4,6 +4,7 @@ namespace kernel\app_modules\tgbot\controllers;
use app\modules\tgbot\models\Tgbot;
use JetBrains\PhpStorm\NoReturn;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\forms\CreateCardForm;
use kernel\app_modules\card\services\CardService;
use kernel\helpers\Debug;
@ -28,8 +29,8 @@ class TgBotRestController extends RestController
$tgBot = $this->model->where("bot_id", $data['bot_id'])->where("dialog_id", $data['dialog_id'])->first();
if (!$tgBot){
foreach ($this->model->getFillable() as $item){
if (!$tgBot) {
foreach ($this->model->getFillable() as $item) {
$this->model->{$item} = $data[$item] ?? null;
}
@ -49,12 +50,21 @@ class TgBotRestController extends RestController
$this->model->save();
$resArr = $tgBot->toArray();
$resArr['has_card'] = CardService::userHasCard($resArr['user_id']);
$resArr = $this->model->toArray();
$cardService = new CardService();
$cardForm = new CreateCardForm();
$cardForm->load([
'user_id' => $user->id,
'username' => $username,
'card_template_id' => 105545,
'status' => 1,
]);
$cardService->create($cardForm);
$this->renderApi($resArr);
}
$resArr = $tgBot->toArray();
$resArr['has_card'] = CardService::userHasCard($resArr['user_id']);
$this->renderApi($resArr);
}
@ -74,7 +84,7 @@ class TgBotRestController extends RestController
$form->setItem('pin', 1111);
$form->setItem('status', 1);
if ($form->validate()){
if ($form->validate()) {
$model = $cardService->create($form);
$this->renderApi($model->load(['cardFile'])->toArray());
@ -92,4 +102,16 @@ class TgBotRestController extends RestController
$this->renderApi([]);
}
#[NoReturn] public function actionGetCardByDialog(int $dialog_id, int $bot_id): void
{
$model = \kernel\app_modules\tgbot\models\Tgbot::where(['dialog_id' => $dialog_id, 'bot_id' => $bot_id])->first();
if ($model) {
$card = Card::where("user_id", $model->user_id)->first();
if ($card) {
$this->renderApi($card->load('cardFile')->toArray());
}
}
$this->renderApi([]);
}
}

View File

@ -30,4 +30,5 @@ App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
$router->post('/tg-bot/create-card', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionCreateCard']);
$router->get('/tg-bot/get-by-dialog/{dialog_id}/{bot_id}', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionGetByDialog']);
});
$router->get('/tg-bot/get-card-by-dialog/{dialog_id}/{bot_id}', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionGetCardByDialog']);
});

View File

@ -31,7 +31,7 @@ $table->beforePrint(function () {
$table->columns([
"status" => [
"value" => function ($cell) {
return \app\modules\tgbot\models\Tgbot::getStatus()[$cell];
return \app\modules\tgbot\models\Tgbot::getStatus()[$cell] ?? 0;
}
],
"username" => [

View File

@ -11,7 +11,11 @@ class UserService
public function create(FormModel $form_model): false|User
{
$model = new User();
$model = User::where("username", $form_model->getItem('username'))->first();
if ($model){
return $model;
}
$model = new User();
$model->username = $form_model->getItem('username');
$model->email = $form_model->getItem('email');
$model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);

View File

@ -0,0 +1,7 @@
#cardBox {
text-align: center;
}
.card {
margin-bottom: 10px;
}

BIN
resources/main/images/l.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

View File

@ -1,9 +1,7 @@
document.addEventListener("DOMContentLoaded", () => {
let tg = window.Telegram.WebAppUser;
let username_tag = document.getElementById("username_tag")
let user_id_tag = document.getElementById("user_id_tag")
username_tag.innerHTML = tg.initDataUnsafe.user.username;
user_id_tag.innerHTML = "tg.initDataUnsafe.user.id";
console.log(123)
import {TgApp} from "./tg_app/TgApp.js";
document.addEventListener("DOMContentLoaded", () => {
let tg = window.Telegram.WebApp;
let tgApp = new TgApp("tg_app", tg.initDataUnsafe.user.id);
});

View File

@ -0,0 +1,78 @@
import config from "./config_local.js";
class TgApp {
constructor(containerId, userId) {
this.container = document.getElementById(containerId);
this.createCardBox();
this.createDefaultBox();
this.userId = userId;
this.getCard();
}
setUserId(userId){
this.userId = userId;
}
createCardBox() {
this.cardBox = document.createElement("div");
this.cardBox.setAttribute("id", "cardBox");
this.cardBox.innerHTML = templates.preloader();
this.container.appendChild(this.cardBox);
}
createDefaultBox() {
this.defaultBox = document.createElement("div");
this.defaultBox.setAttribute("id", "defaultBox");
this.defaultBox.innerHTML = templates.defaultBox();
this.container.appendChild(this.defaultBox);
}
getCard(){
let botId = config.config.botId;
fetch(config.config.apiUrl + `api/tg-bot/get-card-by-dialog/${this.userId}/${botId}`, {
method: 'GET', // Здесь так же могут быть GET, PUT, DELETE
headers: {
// Добавляем необходимые заголовки
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then((data) => {
this.cardBox.innerHTML = templates.cardBox(data.card_file.file, data.balance)
// {title: "foo", body: "bar", userId: 1, id: 101}
})
}
}
const templates =
{
preloader: () => {
return '<img src="/resources/main/images/l.gif">';
},
defaultBox: () => {
return `<div class="card">
<img src="https://mdbcdn.b-cdn.net/img/new/standard/nature/184.webp" class="card-img-top" alt="Fissure in Sandstone"/>
<div class="card-body">
<h5 class="card-title">Акция на Старый Новый Год</h5>
<p class="card-text">Акция на Старый Новый Год Акция на Старый Новый Год Акция на Старый Новый Год Акция на Старый Новый Год</p>
<a href="#!" data-mdb-ripple-init class="btn btn-primary">Принять участие</a>
</div>
</div>`;
},
cardBox: (cardUrl, balance) => {
return `<div class="card" style="padding: 10px;">
<img src="${cardUrl}" class="card-img-top"/>
<div class="card-body">
<h6 class="card-title">Баланс: ${balance}</h6>
</div>
</div>`;
}
}
export {TgApp}

View File

@ -0,0 +1,6 @@
const config = {
botId: 6911686987,
apiUrl: 'https://6b7b-185-5-38-195.ngrok-free.app/',
}
export default {config}