card page
This commit is contained in:
parent
3ca3d48ffe
commit
40369fb515
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ resources/upload
|
|||||||
resources/tmp
|
resources/tmp
|
||||||
resources/cards
|
resources/cards
|
||||||
composer.lock
|
composer.lock
|
||||||
|
resources/main/js/tg_app/config_local.js
|
@ -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="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/bootstrap.min.css">
|
||||||
<link rel="stylesheet" href="<?= $resources ?>/css/style.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>
|
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@ -40,16 +41,10 @@
|
|||||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||||
<ul class="nav navbar-nav ml-auto">
|
<ul class="nav navbar-nav ml-auto">
|
||||||
<li class="nav-item active">
|
<li class="nav-item active">
|
||||||
<a class="nav-link" href="/admin/logout">Выход</a>
|
<a class="nav-link" href="/miniapp/card">Карта</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="#">About</a>
|
<a class="nav-link" href="/miniapp/sale">Акции</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>
|
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@ -67,20 +62,19 @@
|
|||||||
<button type="button" class="btn-close closeAlertBtn"></button>
|
<button type="button" class="btn-close closeAlertBtn"></button>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<div id="username_tag"></div>
|
|
||||||
<div id="user_id_tag"></div>
|
|
||||||
<?= $content ?>
|
<?= $content ?>
|
||||||
</div>
|
</div>
|
||||||
</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 src="https://cdn.jsdelivr.net/npm/eruda"></script>
|
||||||
<script>
|
<script>
|
||||||
// Initialize Eruda
|
// Initialize Eruda
|
||||||
eruda.init();
|
eruda.init();
|
||||||
</script>
|
</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>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,3 +1 @@
|
|||||||
<?php
|
<div id="tg_app"></div>
|
||||||
|
|
||||||
echo "Hello Telegram";
|
|
||||||
|
@ -30,8 +30,8 @@ class CardService
|
|||||||
$model->info = $form_model->getItem('info') ?? 42;
|
$model->info = $form_model->getItem('info') ?? 42;
|
||||||
$model->program = $form_model->getItem('program') ?? 74;
|
$model->program = $form_model->getItem('program') ?? 74;
|
||||||
$model->balance = $form_model->getItem('balance') ?? 0;
|
$model->balance = $form_model->getItem('balance') ?? 0;
|
||||||
$model->cvc = $form_model->getItem('cvc');
|
$model->cvc = $form_model->getItem('cvc') ?? 101;
|
||||||
$model->pin = $form_model->getItem('pin');
|
$model->pin = $form_model->getItem('pin') ?? 1111;
|
||||||
$model->username = $form_model->getItem('username');
|
$model->username = $form_model->getItem('username');
|
||||||
$model->card_template_id = $form_model->getItem('card_template_id');
|
$model->card_template_id = $form_model->getItem('card_template_id');
|
||||||
$model->status = $form_model->getItem('status');
|
$model->status = $form_model->getItem('status');
|
||||||
|
@ -4,6 +4,7 @@ namespace kernel\app_modules\tgbot\controllers;
|
|||||||
|
|
||||||
use app\modules\tgbot\models\Tgbot;
|
use app\modules\tgbot\models\Tgbot;
|
||||||
use JetBrains\PhpStorm\NoReturn;
|
use JetBrains\PhpStorm\NoReturn;
|
||||||
|
use kernel\app_modules\card\models\Card;
|
||||||
use kernel\app_modules\card\models\forms\CreateCardForm;
|
use kernel\app_modules\card\models\forms\CreateCardForm;
|
||||||
use kernel\app_modules\card\services\CardService;
|
use kernel\app_modules\card\services\CardService;
|
||||||
use kernel\helpers\Debug;
|
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();
|
$tgBot = $this->model->where("bot_id", $data['bot_id'])->where("dialog_id", $data['dialog_id'])->first();
|
||||||
|
|
||||||
if (!$tgBot){
|
if (!$tgBot) {
|
||||||
foreach ($this->model->getFillable() as $item){
|
foreach ($this->model->getFillable() as $item) {
|
||||||
$this->model->{$item} = $data[$item] ?? null;
|
$this->model->{$item} = $data[$item] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,12 +50,21 @@ class TgBotRestController extends RestController
|
|||||||
|
|
||||||
$this->model->save();
|
$this->model->save();
|
||||||
|
|
||||||
$resArr = $tgBot->toArray();
|
$resArr = $this->model->toArray();
|
||||||
$resArr['has_card'] = CardService::userHasCard($resArr['user_id']);
|
|
||||||
|
$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);
|
$this->renderApi($resArr);
|
||||||
}
|
}
|
||||||
$resArr = $tgBot->toArray();
|
$resArr = $tgBot->toArray();
|
||||||
$resArr['has_card'] = CardService::userHasCard($resArr['user_id']);
|
|
||||||
|
|
||||||
$this->renderApi($resArr);
|
$this->renderApi($resArr);
|
||||||
}
|
}
|
||||||
@ -74,7 +84,7 @@ class TgBotRestController extends RestController
|
|||||||
$form->setItem('pin', 1111);
|
$form->setItem('pin', 1111);
|
||||||
$form->setItem('status', 1);
|
$form->setItem('status', 1);
|
||||||
|
|
||||||
if ($form->validate()){
|
if ($form->validate()) {
|
||||||
$model = $cardService->create($form);
|
$model = $cardService->create($form);
|
||||||
|
|
||||||
$this->renderApi($model->load(['cardFile'])->toArray());
|
$this->renderApi($model->load(['cardFile'])->toArray());
|
||||||
@ -92,4 +102,16 @@ class TgBotRestController extends RestController
|
|||||||
$this->renderApi([]);
|
$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([]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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->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-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']);
|
||||||
});
|
});
|
@ -31,7 +31,7 @@ $table->beforePrint(function () {
|
|||||||
$table->columns([
|
$table->columns([
|
||||||
"status" => [
|
"status" => [
|
||||||
"value" => function ($cell) {
|
"value" => function ($cell) {
|
||||||
return \app\modules\tgbot\models\Tgbot::getStatus()[$cell];
|
return \app\modules\tgbot\models\Tgbot::getStatus()[$cell] ?? 0;
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"username" => [
|
"username" => [
|
||||||
|
@ -11,6 +11,10 @@ class UserService
|
|||||||
|
|
||||||
public function create(FormModel $form_model): false|User
|
public function create(FormModel $form_model): false|User
|
||||||
{
|
{
|
||||||
|
$model = User::where("username", $form_model->getItem('username'))->first();
|
||||||
|
if ($model){
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
$model = new User();
|
$model = new User();
|
||||||
$model->username = $form_model->getItem('username');
|
$model->username = $form_model->getItem('username');
|
||||||
$model->email = $form_model->getItem('email');
|
$model->email = $form_model->getItem('email');
|
||||||
|
7
resources/main/css/tgApp.css
Normal file
7
resources/main/css/tgApp.css
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#cardBox {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
BIN
resources/main/images/l.gif
Normal file
BIN
resources/main/images/l.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
@ -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);
|
||||||
|
|
||||||
});
|
});
|
78
resources/main/js/tg_app/TgApp.js
Normal file
78
resources/main/js/tg_app/TgApp.js
Normal 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}
|
||||||
|
|
6
resources/main/js/tg_app/config.js
Normal file
6
resources/main/js/tg_app/config.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
const config = {
|
||||||
|
botId: 6911686987,
|
||||||
|
apiUrl: 'https://6b7b-185-5-38-195.ngrok-free.app/',
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {config}
|
Loading…
x
Reference in New Issue
Block a user