117 lines
2.9 KiB
PHP
117 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace mobileapi\manager;
|
|
|
|
use common\models\Card;
|
|
use common\models\CardKeyAssignment;
|
|
use common\models\Key;
|
|
use common\models\Ticket;
|
|
use Endroid\QrCode\QrCode;
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
class ApiManager
|
|
{
|
|
public function getCardPage()
|
|
{
|
|
$customer = \Yii::$app->user->getIdentity();
|
|
|
|
if (!isset($customer)) {
|
|
throw new \yii\web\NotFoundHttpException();
|
|
}
|
|
$card = Card::findOne($customer->id_customer_card);
|
|
|
|
if (!isset($card)) {
|
|
throw new \yii\web\NotFoundHttpException();
|
|
}
|
|
|
|
$qrCode = new QrCode($card->number);
|
|
|
|
return [
|
|
'qrcode' => $qrCode->writeDataUri(),
|
|
'cardNumber' => $card->number,
|
|
'customerName' => $customer->name
|
|
];
|
|
|
|
}
|
|
|
|
function getTicketPage()
|
|
{
|
|
$customer = \Yii::$app->user->getIdentity();
|
|
|
|
if (!isset($customer)) {
|
|
throw new \yii\web\NotFoundHttpException();
|
|
}
|
|
$card = Card::findOne($customer->id_customer_card);
|
|
|
|
if (!isset($card)) {
|
|
throw new \yii\web\NotFoundHttpException();
|
|
}
|
|
$tickets = Ticket::readActive($card);
|
|
|
|
$result = [];
|
|
foreach ($tickets as $ticket) {
|
|
$result[] = [
|
|
'idTicket' => $ticket->id_ticket,
|
|
'type' => [
|
|
'idType' => $ticket->ticketType->id_ticket_type,
|
|
'name' => $ticket->ticketType->name,
|
|
],
|
|
'usageCount' => $ticket->usage_count,
|
|
'start' => $ticket->start,
|
|
'end' => $ticket->end
|
|
];
|
|
}
|
|
return [
|
|
'tickets' => $result
|
|
];
|
|
}
|
|
|
|
public function getVirtualKeyPage()
|
|
{
|
|
$customer = \Yii::$app->user->getIdentity();
|
|
|
|
if (!isset($customer)) {
|
|
throw new \yii\web\NotFoundHttpException();
|
|
}
|
|
$card = Card::findOne($customer->id_customer_card);
|
|
|
|
if (!isset($card)) {
|
|
throw new \yii\web\NotFoundHttpException();
|
|
}
|
|
|
|
$keyObject = null;
|
|
try {
|
|
$keyAssignment = CardKeyAssignment::findOne(['id_card' => $card->id_card]);
|
|
|
|
if (!isset($keyAssignment)) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
|
|
$key = Key::findOne(['id_key' => $keyAssignment->id_key]);
|
|
|
|
if (!isset($key)) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
|
|
$qrCode = new QrCode($keyAssignment->virtual_key);
|
|
|
|
$keyObject = [
|
|
'qrcode' => $qrCode->writeDataUri(),
|
|
'createdAt' => $card->created_at,
|
|
'key' => $key->number,
|
|
'idKey' => $key->id_key
|
|
];
|
|
} catch (\Exception $e) {
|
|
// failed to get key
|
|
\Yii::error('Failed to get virtual key: ' . $e->getMessage());
|
|
}
|
|
|
|
return [
|
|
'key' => $keyObject
|
|
];
|
|
|
|
}
|
|
|
|
|
|
}
|