186 lines
6.8 KiB
PHP
186 lines
6.8 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" => $this->convert_vi_to_en($value->name),
|
|
"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;
|
|
}
|
|
|
|
public function actionResetData() {
|
|
\Yii::$app->db->createCommand()->truncateTable('capture_logs')->execute();
|
|
\Yii::$app->db->createCommand()->truncateTable('list_management')->execute();
|
|
}
|
|
|
|
public function convert_vi_to_en($str) {
|
|
$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str);
|
|
$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", "e", $str);
|
|
$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", "i", $str);
|
|
$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", "o", $str);
|
|
$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", "u", $str);
|
|
$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", "y", $str);
|
|
$str = preg_replace("/(đ)/", "d", $str);
|
|
$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", "A", $str);
|
|
$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", "E", $str);
|
|
$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", "I", $str);
|
|
$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", "O", $str);
|
|
$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", "U", $str);
|
|
$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "Y", $str);
|
|
$str = preg_replace("/(Đ)/", "D", $str);
|
|
//$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
|
|
return $str;
|
|
}
|
|
|
|
}
|