AIParking_Intops_Server/controllers/ApiController.php
2020-02-01 16:47:12 +07:00

354 lines
14 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\helpers\FileHelper;
use app\models\User;
use app\models\Gate;
use app\models\Camera;
use app\models\Card;
use app\models\Logs;
/**
* CardController implements the CRUD actions for Card model.
*/
class ApiController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionLogin() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$user = User::findOne([
'username' => $post['username'],
'password' => md5($post['password'])
]);
Yii::$app->response->format = "json";
if ($user) {
$gate = Gate::findOne(['user_id' => $user->id]);
if (!$gate) {
return [
'status' => false,
'data' => "Tài khoản chưa được cấu hình phân quyền"
];
} else {
if ($gate->api_config == null || $gate->api_config === "") {
return [
'status' => false,
'data' => "Cổng chưa được cấu hình API"
];
}
}
$cameras = Camera::find()->andWhere(['gate' => $gate->id])->all();
if (count($cameras) == 0) {
return [
'status' => false,
'data' => "Cổng chưa được cấu hình camera"
];
}
return [
'status' => true,
'data' => [
"userID" => $user->id,
"name" => $user->first_name,
"api" => json_decode($gate->api_config, true),
"score" => 8,
"scoreLong" => 6,
"camera1" => $gate->getCameraInfo(1),
"camera2" => $gate->getCameraInfo(2),
"camera3" => $gate->getCameraInfo(3),
"camera4" => $gate->getCameraInfo(4),
"camera5" => $gate->getCameraInfo(5),
"camera6" => $gate->getCameraInfo(6),
"camera7" => $gate->getCameraInfo(7),
"camera8" => $gate->getCameraInfo(8),
"statistic" => Logs::statistics(),
"cascadeConfig" => json_decode($gate->cascade_config, true),
"laneConfig" => json_decode($gate->lane_config, true)
]
];
} else {
return [
'status' => false,
'data' => "Tên tài khoản hoặc mật khẩu không đúng"
];
}
}
}
public function actionCheckCard() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$cardInfo = Card::findOne(['code' => $post['card']]);
Yii::$app->response->format = "json";
if ($cardInfo) {
$lastLog = Logs::find()->andWhere(['card_id' => $cardInfo->id])->orderBy(['time_in' => SORT_DESC])->one();
if ($lastLog) {
if ($lastLog->type == "in") {
return [
'status' => true,
'type' => "out",
'cardID' => sprintf("%06d", $cardInfo->id),
'cardRealID' => $cardInfo->id,
'log_id' => $lastLog->id,
'cardType' => $cardInfo->cardType,
'vehicleType' => $cardInfo->vehicleType,
'plate' => $lastLog->plate_in,
'time' => date("H:i:s d/m/Y", $lastLog->time_in),
'plateImage' => base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/" . $lastLog->plate_image_in)),
'frameImage' => base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/" . $lastLog->frame_image_in)),
'faceImage' => $lastLog->face_image_in === "" ? "" : base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/" . $lastLog->face_image_in)),
'name' => $lastLog->name_in,
'plate' => $cardInfo->plate
];
} else {
return [
'status' => true,
'type' => "in",
'cardID' => sprintf("%06d", $cardInfo->id),
'cardRealID' => $cardInfo->id,
'cardType' => $cardInfo->cardType,
'vehicleType' => $cardInfo->vehicleType,
'plate' => $cardInfo->plate
];
}
} else {
return [
'status' => true,
'type' => "in",
'cardID' => sprintf("%06d", $cardInfo->id),
'cardRealID' => $cardInfo->id,
'cardType' => $cardInfo->cardType,
'vehicleType' => $cardInfo->vehicleType,
'plate' => $cardInfo->plate
];
}
} else {
return [
'status' => false
];
}
}
}
public function actionSaveLogs() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$RootFolder = Yii::getAlias('@webroot') . "/data/uploads";
$temp = $post['time'];
$temp = explode(" ", $temp);
$DateFolder = str_replace("/", "_", $temp[1]);
$time = date_format(date_create_from_format('H:i:s d/m/Y', $post['time']), 'U');
$targetPathFrame = $RootFolder . "/" . $DateFolder . "/frame/" . $post['type'];
FileHelper::createDirectory($targetPathFrame, 0777);
$FrameFileName = "frame_" . $time . ".png";
file_put_contents($targetPathFrame . "/" . $FrameFileName, base64_decode($post['frameImage']));
$post['frameImage'] = $DateFolder . "/frame/" . $post['type'] . "/" . $FrameFileName;
if ($post['plateImage'] !== "") {
$targetPathPlate = $RootFolder . "/" . $DateFolder . "/plate/" . $post['type'];
FileHelper::createDirectory($targetPathPlate, 0777);
$PlateFileName = "plate_" . $time . ".png";
file_put_contents($targetPathPlate . "/" . $PlateFileName, base64_decode($post['plateImage']));
$post['plateImage'] = $DateFolder . "/plate/" . $post['type'] . "/" . $PlateFileName;
} else {
$post['plateImage'] = null;
}
if (isset($post['faceImage'])) {
$targetPathFace = $RootFolder . "/" . $DateFolder . "/face/" . $post['type'];
FileHelper::createDirectory($targetPathFace, 0777);
$FaceFileName = "face_" . $time . ".png";
file_put_contents($targetPathFace . "/" . $FaceFileName, base64_decode($post['faceImage']));
$post['faceImage'] = $DateFolder . "/face/" . $post['type'] . "/" . $FaceFileName;
}
$post['time'] = $time;
if ($post['type'] === "in") {
$model = new Logs();
Yii::$app->response->format = "json";
$id = $model->create($post);
if ($id) {
return [
'status' => true,
'logID' => $id,
"cost" => "0"
];
} else {
return [
'status' => false
];
}
} else {
$model = Logs::findOne($post['log_id']);
$model->plate_out = $post['plate'];
$model->plate_image_out = $post['plateImage'];
$model->frame_image_out = $post['frameImage'];
$model->face_image_out = isset($post['faceImage']) ? $post['faceImage'] : "";
$model->name_out = isset($post['name']) ? $post['name'] : "";
$model->time_out = $time;
$model->camera_out = $post['camera'];
$model->mod_out = $post['mod'];
Yii::$app->response->format = "json";
if ($model->save()) {
return [
'status' => true,
'logID' => $model->id,
'cost' => $model->cost
];
} else {
return [
'status' => false
];
}
}
}
}
public function actionUpdateLogs() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$model = Logs::findOne($post['logID']);
$model->plate_in = $post['plate_in'];
$model->plate_out = $post['plate_out'];
$model->save();
return true;
}
}
public function actionStatistics() {
Yii::$app->response->format = "json";
return Logs::statistics();
}
public function convert($code) {
$temp = explode("-", $code)[0];
$hex = dechex($temp);
$t = [];
for ($i = 0; $i < strlen($hex) / 2; $i++) {
$t[] = substr($hex, $i * 2, 2);
}
$t = array_reverse($t);
$t = implode("", $t);
return hexdec($t);
}
public function actionImport() {
$file_type = \PHPExcel_IOFactory::identify("data/test.xlsx");
$objReader = \PHPExcel_IOFactory::createReader($file_type);
$objPHPExcel = $objReader->load("data/test.xlsx");
$sheet_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$objPHPExcel = new \PHPExcel();
$count = 1;
$objPHPExcel->setActiveSheetIndex(0);
$toExcelFile[] = [
"Mã NV",
"Họ tên",
"Đơn vị",
"Mã thẻ gốc",
"Giới tính",
"Mã thẻ đã chuyển đổi"
];
$cards = Card::find()->all();
foreach ($sheet_data as $k => $row) {
$ExportData[] = $row["A"];
$ExportData[] = $row["B"];
$ExportData[] = $row["C"];
$ExportData[] = $row["D"];
$ExportData[] = $row["E"];
$ExportData[] = $this->convert($row["D"]);
$toExcelFile[] = $ExportData;
unset($ExportData);
}
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->getColumnDimension('B')->setWidth(30);
$rowCount = 1;
for ($i = 0; $i < count($toExcelFile); $i++) {
$column = 'A';
$row = $toExcelFile[$i];
for ($j = 0; $j < count($row); $j++) {
if (!isset($row[$j]))
$value = NULL;
elseif ($row[$j] != "")
$value = strip_tags($row[$j]);
else
$value = "";
$activeSheet->setCellValue($column . $rowCount, $value);
$column = chr(ord($column) + 1);
}
$rowCount++;
}
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="ket-qua.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
public function actionExport() {
$objPHPExcel = new \PHPExcel();
$count = 1;
$objPHPExcel->setActiveSheetIndex(0);
$toExcelFile[] = [
"STT",
"Mã thẻ"
];
$cards = Card::find()->all();
foreach ($cards as $k => $v) {
$ExportData[] = $v->id;
$ExportData[] = $v->code;
$toExcelFile[] = $ExportData;
unset($ExportData);
}
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->getColumnDimension('B')->setWidth(30);
$rowCount = 1;
for ($i = 0; $i < count($toExcelFile); $i++) {
$column = 'A';
$row = $toExcelFile[$i];
for ($j = 0; $j < count($row); $j++) {
if (!isset($row[$j]))
$value = NULL;
elseif ($row[$j] != "")
$value = strip_tags($row[$j]);
else
$value = "";
$activeSheet->setCellValue($column . $rowCount, $value);
$column = chr(ord($column) + 1);
}
$rowCount++;
}
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="ket-qua.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
}