BiFace_Server_Lite/controllers/ApiController.php
2020-12-15 14:47:02 +07:00

162 lines
5.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;
use app\models\common;
/**
* 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 = common::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']));
$model = new CaptureLogs();
$model->create([
"Staff" => $post["id"],
"Time" => $time,
"Image" => $fileName,
"Confidence" => strval($post["confidence"])
]);
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->code,
"features" => $f
];
}
Yii::$app->response->format = "json";
return $allFeatures;
}
public function actionData() {
if (Yii::$app->request->post()) {
Yii::$app->response->format = "json";
return ListManagement::find()->orderBy(['id' => SORT_DESC])->all();
}
}
public function actionFullData() {
if (Yii::$app->request->post()) {
Yii::$app->response->format = "json";
$lists = ListManagement::find()->andWhere(["IN", "code", Yii::$app->request->post()])->orderBy(['id' => SORT_DESC])->all();
$res = [];
foreach ($lists as $key => $value) {
$f = [];
$features = json_decode($value->image, true);
foreach ($features as $k => $v) {
$f[] = ["url" => base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/face/" . $v['url'])), "features" => $v['features']];
}
$res[] = [
"id" => $value->id,
"code" => $value->code,
"type" => $value->type,
"name" => $value->name,
"gender" => $value->gender,
"birthday" => $value->birthday,
"telephone" => $value->telephone,
"department" => $value->address,
"time" => $value->time,
"images" => $f
];
}
return $res;
}
}
public function actionGetAllImage() {
Yii::$app->response->format = "json";
$lists = ListManagement::find()->orderBy(['id' => SORT_DESC])->all();
$res = [];
foreach ($lists as $key => $value) {
$f = [];
$features = json_decode($value->image, true);
foreach ($features as $k => $v) {
$f[] = Yii::$app->request->hostInfo . "/data/uploads/face/" . $v['url'];
}
$res[] = [
"id" => $value->id,
"code" => $value->code,
"type" => $value->type,
"name" => $value->name,
"gender" => $value->gender,
"birthday" => $value->birthday,
"telephone" => $value->telephone,
"address" => $value->address,
"time" => $value->time,
"image" => $f
];
}
return $res;
}
}