139 lines
3.5 KiB
PHP
139 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace mobileapi\manager;
|
|
|
|
use common\components\DateUtil;
|
|
use common\components\Helper;
|
|
use Exception;
|
|
use Yii;
|
|
use common\models\Card;
|
|
use common\models\CardKeyAssignment;
|
|
use common\models\Customer;
|
|
use common\models\Key;
|
|
use common\models\Ticket;
|
|
use Endroid\QrCode\QrCode;
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
class ApiManager
|
|
{
|
|
/**
|
|
* @throws Exception
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public function getCardPage()
|
|
{
|
|
$device = Yii::$app->user->getIdentity();
|
|
|
|
if (!isset($device)) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
$card = Card::findOne($device->id_card);
|
|
|
|
if (!isset($card)) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
|
|
$customer = Customer::findOne(['id_customer_card' => $device->id_card]);
|
|
|
|
if (!isset($customer)) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
|
|
$qrCode = new QrCode($card->number);
|
|
|
|
return [
|
|
'qrcode' => $qrCode->writeDataUri(),
|
|
'cardNumber' => $card->number,
|
|
'customerName' => $customer->name
|
|
];
|
|
|
|
}
|
|
|
|
public function getTicketPage()
|
|
{
|
|
$device = Yii::$app->user->getIdentity();
|
|
|
|
if (!isset($device)) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
$card = Card::findOne($device->id_card);
|
|
|
|
if (!isset($card)) {
|
|
throw new 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' => DateUtil::parseDateTime( $ticket->start)->getTimestamp(),
|
|
'end' => DateUtil::parseDateTime( $ticket->end)->getTimestamp()
|
|
];
|
|
}
|
|
return [
|
|
'tickets' => $result
|
|
];
|
|
}
|
|
|
|
public function getVirtualKeyPage()
|
|
{
|
|
$device = Yii::$app->user->getIdentity();
|
|
|
|
if (!isset($device)) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
$card = Card::findOne($device->id_card);
|
|
|
|
if (!isset($card)) {
|
|
throw new 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
|
|
];
|
|
|
|
}
|
|
|
|
public function getDashboardPage(){
|
|
return [
|
|
'virtual-key' => $this->getVirtualKeyPage(),
|
|
'card' => $this->getCardPage(),
|
|
'ticket' => $this->getTicketPage(),
|
|
];
|
|
}
|
|
|
|
}
|