From fc0f2ccc0fdcc775da146bbaf85f276f475236e9 Mon Sep 17 00:00:00 2001 From: dongpd Date: Tue, 22 Dec 2020 16:17:34 +0700 Subject: [PATCH] gioi han so mau cho 1 doi tuong --- config/params.php | 3 +- controllers/ListManagementController.php | 58 ++++++++++++++---------- views/capture-logs/index.tpl | 5 +- web/js/capture-logs.js | 9 +++- web/js/list-management.js | 6 ++- 5 files changed, 51 insertions(+), 30 deletions(-) diff --git a/config/params.php b/config/params.php index 78ad81c1..d871c418 100644 --- a/config/params.php +++ b/config/params.php @@ -2,5 +2,6 @@ return [ 'adminEmail' => 'admin@example.com', - "maxLogs" => 100000 + "maxLogs" => 100000, + "maxPicture" => 5 ]; diff --git a/controllers/ListManagementController.php b/controllers/ListManagementController.php index 69731104..b6cf785d 100644 --- a/controllers/ListManagementController.php +++ b/controllers/ListManagementController.php @@ -108,6 +108,8 @@ class ListManagementController extends Controller { $url = CaptureLogs::findOne($data['id'])->image; if ($listManagement) { $images = json_decode($listManagement->image, true); + if (count($images) >= \Yii::$app->params['maxPicture']) + return ["status" => false, "text" => "Mỗi đối tượng chỉ nhận tối đa " . \Yii::$app->params['maxPicture'] . " hình ảnh mẫu"]; $add = true; foreach ($images as $key => $value) { if ($value['url'] === $url) @@ -135,6 +137,7 @@ class ListManagementController extends Controller { file_get_contents("http://localhost:2305/update-feature"); return ["status" => true]; } + return ["status" => false, "text" => "Hình ảnh này đã được chọn làm mẫu cho đối tượng này!"]; } else { $features = json_decode(common::requestToEngine("/get-feature", [ "image_paths" => [ @@ -169,7 +172,8 @@ class ListManagementController extends Controller { "birthday" => date("d/m/Y", $ListManagement->birthday), "telephone" => $ListManagement->telephone, "address" => $ListManagement->address, - "image" => count($images) > 0 ? "/data/uploads/face/" . $images[0]['url'] : "/images/user2-160x160.jpg" + "image" => count($images) > 0 ? "/data/uploads/face/" . $images[0]['url'] : "/images/user2-160x160.jpg", + "canUpload" => count($images) >= Yii::$app->params['maxPicture'] ? false : true ]; } } @@ -372,21 +376,23 @@ class ListManagementController extends Controller { $targetPath = $RootFolder . "/face"; FileHelper::createDirectory($targetPath, 0777); foreach ($data['images'] as $key => $value) { - $key = common::generateRandomString(); - $fileName = "face_" . $key . "_" . time() . ".png"; - $img = file_get_contents($value); - if ($img !== "null") { - file_put_contents($targetPath . "/" . $fileName, $img); - $features = json_decode(common::requestToEngine("/get-feature", [ - "image_paths" => [ - ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] - ] - ]), true); - $ft[] = [ - "url" => $fileName, - "features" => $features['results'][0]['feature'], - "features512" => isset($features['results'][0]['feature512']) ? $features['results'][0]['feature512'] : [] - ]; + if ($key < Yii::$app->params['maxPicture']) { + $key = common::generateRandomString(); + $fileName = "face_" . $key . "_" . time() . ".png"; + $img = file_get_contents($value); + if ($img !== "null") { + file_put_contents($targetPath . "/" . $fileName, $img); + $features = json_decode(common::requestToEngine("/get-feature", [ + "image_paths" => [ + ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] + ] + ]), true); + $ft[] = [ + "url" => $fileName, + "features" => $features['results'][0]['feature'], + "features512" => isset($features['results'][0]['feature512']) ? $features['results'][0]['feature512'] : [] + ]; + } } } $model = ListManagement::findOne(['code' => $data['idStaff']]); @@ -427,13 +433,19 @@ class ListManagementController extends Controller { foreach ($results as $key => $value) { $images = []; foreach ($value['images'] as $k => $v) { - $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($v['url'])); - $images[] = ["url" => $fileName, "features" => $v['features']]; + if ($k < Yii::$app->params['maxPicture']) { + $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($v['url'])); + $images[] = [ + "url" => $fileName, + "features" => $v['features'], + "features512" => isset($v['features512']) ? $v['features512'] : [] + ]; + } } $model = ListManagement::findOne(['code' => $value['code']]); if ($model) { diff --git a/views/capture-logs/index.tpl b/views/capture-logs/index.tpl index 13d0a503..bf977664 100644 --- a/views/capture-logs/index.tpl +++ b/views/capture-logs/index.tpl @@ -58,11 +58,10 @@ 'value' => \app\helpers\CaptureLogsGrid::confidence() ], [ - 'attribute' => 'remark', + 'attribute' => 'sync_status', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center'] - ], - 'sync_status' + ] ] ])} diff --git a/web/js/capture-logs.js b/web/js/capture-logs.js index 06fa67cf..c413846c 100644 --- a/web/js/capture-logs.js +++ b/web/js/capture-logs.js @@ -55,8 +55,13 @@ function _save(e) { id: $("input[name='CaptureLogsID']").val() }, success: function (data) { - alert("Đã thêm dữ liệu thành công!"); - window.location.reload(true); + common.modalBlock(false); + if (data.status) { + alert("Đã thêm dữ liệu thành công!"); + window.location.reload(true); + } else { + alert(data.text); + } }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); diff --git a/web/js/list-management.js b/web/js/list-management.js index 3316f2f7..b18cd22d 100644 --- a/web/js/list-management.js +++ b/web/js/list-management.js @@ -60,6 +60,10 @@ function _formModified(e) { }, success: function (data) { common.modalBlock(false); + if (data.canUpload) + $("#upload-btn").removeClass("hidden"); + else + $("#upload-btn").addClass("hidden"); $("select[name='Type']").val(data.type); $("select[name='Code']").val(data.code); $("input[name='Name']").val(data.name); @@ -538,7 +542,7 @@ function syncFeatureToServer(id) { $("#progress").html(percent + "%"); if (percent >= 100) { common.modalBlock(false); - setTimeout(function () { + setTimeout(function () { alert("Đồng bộ dữ liệu hoàn thành"); window.location.reload(true); }, 2000)