diff --git a/.gitignore b/.gitignore index 6595dc0..59972f8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ views_cache resources/upload resources/tmp resources/cards -composer.lock \ No newline at end of file +composer.lock +resources/main/js/tg_app/config_local.js \ No newline at end of file diff --git a/app/modules/tgbot/views/tgbot/layout/main.php b/app/modules/tgbot/views/tgbot/layout/main.php index de0d12f..c3c76ed 100644 --- a/app/modules/tgbot/views/tgbot/layout/main.php +++ b/app/modules/tgbot/views/tgbot/layout/main.php @@ -20,6 +20,7 @@ + @@ -40,16 +41,10 @@ @@ -67,20 +62,19 @@ -
-
+ + + + + + - - - - - \ No newline at end of file diff --git a/app/modules/tgbot/views/tgbot/main/index.php b/app/modules/tgbot/views/tgbot/main/index.php index 085dd30..dd3f4eb 100644 --- a/app/modules/tgbot/views/tgbot/main/index.php +++ b/app/modules/tgbot/views/tgbot/main/index.php @@ -1,3 +1 @@ - diff --git a/kernel/app_modules/card/services/CardService.php b/kernel/app_modules/card/services/CardService.php index efb4116..0a613ec 100644 --- a/kernel/app_modules/card/services/CardService.php +++ b/kernel/app_modules/card/services/CardService.php @@ -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'); diff --git a/kernel/app_modules/tgbot/controllers/TgBotRestController.php b/kernel/app_modules/tgbot/controllers/TgBotRestController.php index 139e376..e3ed85b 100644 --- a/kernel/app_modules/tgbot/controllers/TgBotRestController.php +++ b/kernel/app_modules/tgbot/controllers/TgBotRestController.php @@ -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([]); + } + } \ No newline at end of file diff --git a/kernel/app_modules/tgbot/routs/tgbot.php b/kernel/app_modules/tgbot/routs/tgbot.php index 020f712..30f8959 100644 --- a/kernel/app_modules/tgbot/routs/tgbot.php +++ b/kernel/app_modules/tgbot/routs/tgbot.php @@ -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']); }); \ No newline at end of file diff --git a/kernel/app_modules/tgbot/views/tgbot/index.php b/kernel/app_modules/tgbot/views/tgbot/index.php index 35249ca..446765b 100644 --- a/kernel/app_modules/tgbot/views/tgbot/index.php +++ b/kernel/app_modules/tgbot/views/tgbot/index.php @@ -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" => [ diff --git a/kernel/modules/user/service/UserService.php b/kernel/modules/user/service/UserService.php index 9915c2a..ebb26bc 100644 --- a/kernel/modules/user/service/UserService.php +++ b/kernel/modules/user/service/UserService.php @@ -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); diff --git a/resources/main/css/tgApp.css b/resources/main/css/tgApp.css new file mode 100644 index 0000000..bf0e104 --- /dev/null +++ b/resources/main/css/tgApp.css @@ -0,0 +1,7 @@ +#cardBox { + text-align: center; +} + +.card { + margin-bottom: 10px; +} \ No newline at end of file diff --git a/resources/main/images/l.gif b/resources/main/images/l.gif new file mode 100644 index 0000000..f236150 Binary files /dev/null and b/resources/main/images/l.gif differ diff --git a/resources/main/js/tg.js b/resources/main/js/tg.js index 35b2de0..9db6bd0 100644 --- a/resources/main/js/tg.js +++ b/resources/main/js/tg.js @@ -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); + }); \ No newline at end of file diff --git a/resources/main/js/tg_app/TgApp.js b/resources/main/js/tg_app/TgApp.js new file mode 100644 index 0000000..22daa15 --- /dev/null +++ b/resources/main/js/tg_app/TgApp.js @@ -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 ''; + }, + defaultBox: () => { + return `
+ Fissure in Sandstone +
+
Акция на Старый Новый Год
+

Акция на Старый Новый Год Акция на Старый Новый Год Акция на Старый Новый Год Акция на Старый Новый Год

+ Принять участие +
+
`; + }, + cardBox: (cardUrl, balance) => { + return `
+ +
+
Баланс: ${balance}
+
+
`; + } + } + +export {TgApp} + diff --git a/resources/main/js/tg_app/config.js b/resources/main/js/tg_app/config.js new file mode 100644 index 0000000..29d8851 --- /dev/null +++ b/resources/main/js/tg_app/config.js @@ -0,0 +1,6 @@ +const config = { + botId: 6911686987, + apiUrl: 'https://6b7b-185-5-38-195.ngrok-free.app/', +} + +export default {config} \ No newline at end of file