111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\controllers;
|
|
|
|
use Yii;
|
|
use yii\web\Controller;
|
|
use yii\filters\VerbFilter;
|
|
use yii\helpers\FileHelper;
|
|
use app\models\CaptureLogs;
|
|
use app\models\ListManagement;
|
|
|
|
/**
|
|
* 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 actionSaveLogs() {
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->bodyParams;
|
|
$model = new \app\models\FaceLogs();
|
|
$model->create($post);
|
|
Yii::$app->response->format = "json";
|
|
return ["stt" => true];
|
|
}
|
|
}
|
|
|
|
public function actionSyncUrl() {
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->bodyParams;
|
|
$sync = \app\models\SyncUrl::find()->one();
|
|
if ($sync) {
|
|
$sync->data = $post['url'];
|
|
$sync->save();
|
|
} else {
|
|
$model = new \app\models\SyncUrl();
|
|
$model->create($post);
|
|
}
|
|
Yii::$app->response->format = "json";
|
|
return ["stt" => true];
|
|
}
|
|
}
|
|
|
|
public function actionGetLogs() {
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->bodyParams;
|
|
$time = date_format(date_create_from_format('Y-m-d H:i:s', $post['time']), 'U');
|
|
$key = $this->generateRandomString();
|
|
$RootFolder = Yii::getAlias('@webroot') . "/data/uploads";
|
|
$targetPath = $RootFolder . "/face";
|
|
$fileName = "face_" . $key . "_" . $time . ".png";
|
|
FileHelper::createDirectory($targetPath, 0777);
|
|
file_put_contents($targetPath . "/" . $fileName, base64_decode($post['image']));
|
|
|
|
if ($post["id"] == 0) {
|
|
$model = new CaptureLogs();
|
|
$model->create([
|
|
"Staff" => 0,
|
|
"Time" => $time,
|
|
"Image" => $fileName
|
|
]);
|
|
}
|
|
Yii::$app->response->format = "json";
|
|
return ["status" => "success"];
|
|
}
|
|
}
|
|
|
|
public function actionGetAllFeatures() {
|
|
$listManagement = ListManagement::find()->all();
|
|
$allFeatures = [];
|
|
foreach ($listManagement as $key => $value) {
|
|
$features = json_decode($value->image, true);
|
|
$f = [];
|
|
foreach ($features as $k => $v) {
|
|
$f[] = $v['features'];
|
|
}
|
|
$allFeatures[] = [
|
|
"id" => $value->id,
|
|
"name" => $value->name,
|
|
"features" => $f
|
|
];
|
|
}
|
|
Yii::$app->response->format = "json";
|
|
return $allFeatures;
|
|
}
|
|
|
|
public function generateRandomString($length = 10) {
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
return $randomString;
|
|
}
|
|
|
|
}
|