update đồng bộ dữ liệu giữa các thiết bị

This commit is contained in:
dongpd 2020-12-09 10:40:35 +07:00
parent f694e9fe71
commit 4cb2f77598
6 changed files with 239 additions and 0 deletions

View File

@ -96,4 +96,36 @@ class ApiController extends Controller {
return $allFeatures; return $allFeatures;
} }
public function actionData() {
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", "id", 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,
"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;
}
}
} }

View File

@ -233,4 +233,59 @@ class ListManagementController extends Controller {
} }
} }
public function actionFormSync() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$results = file_get_contents("http://" . $data['ip'] . "/api/data");
Yii::$app->response->format = "json";
return [
"title" => $data['ip'],
"form" => $this->renderPartial("list", [
"results" => json_decode($results, true),
"typeArray" => ListManagement::$typeArray,
"ip" => $data['ip']
])
];
}
}
public function actionSync($ip) {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$results = json_decode(file_get_contents("http://" . $ip . "/api/full-data", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode($data['lists'])
]
])), true);
foreach ($results as $key => $value) {
$images = [];
foreach ($value['image'] 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']];
}
$model = new ListManagement();
$model->create([
'type' => $value['type'],
'name' => $value['name'],
'image' => json_encode($images),
'gender' => $value['gender'],
'birthday' => date("d/m/Y", $value['birthday']),
'telephone' => $value['telephone'],
'address' => $value['address'],
'time' => $value['time']
]);
}
file_get_contents("http://localhost:2305/update-feature");
Yii::$app->response->format = "json";
return $results;
}
}
} }

View File

@ -144,6 +144,19 @@
<i class="fa fa-plus-circle"></i> Thêm mới <i class="fa fa-plus-circle"></i> Thêm mới
</button> </button>
</div> </div>
<hr style="border-top: 1px solid #000;">
<h4>Đồng bộ từ thiết bị khác</h4>
<div class="form-group" id="ip">
<div class="input-group">
<input type="text" value="" class="form-control" name="SyncIP" placeholder="Nhập ip thiết bị">
<div class="input-group-btn">
<button class="btn btn-info" onclick="_syncForm(this);" data-href="{Url::to(['form-sync'])}">
<i class="fa fa-search"></i>
</button>
</div>
</div>
<span class="help-block hidden"></span>
</div>
</div> </div>
</div> </div>
<div style="width: 300px;position: absolute;" id="menu" class="hidden"> <div style="width: 300px;position: absolute;" id="menu" class="hidden">

View File

@ -0,0 +1,57 @@
<div style="max-height: 750px;overflow-y: auto;">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Type</th>
<th>Name</th>
<th>Gender</th>
<th>Registration Image</th>
<th>Registration time</th>
<th>Birthday</th>
<th>Telephone</th>
<th>Address</th>
</tr>
</thead>
<tbody id="sync-lists">
{foreach from=$results item=arr}
<tr onclick="choooseToSync(this);" style="cursor: pointer;" data-stt="false" data-id="{$arr.id}">
<td>{$arr.id}</td>
<td>{$typeArray[$arr.type]}</td>
<td>{$arr.name}</td>
<td>{$arr.gender}</td>
<td>
{$images = json_decode($arr.image, true)}
{foreach from=$images item=img}
<div class='feature-img'>
<img src="http://{$ip}/data/uploads/face/{$img.url}" class="img-thumbnail" style="width: 100px;height:100px;">
</div>
{/foreach}
</td>
<td>{$arr.time|date_format:"H:i:s d/m/Y"}</td>
<td>{$arr.birthday|date_format:"d/m/Y"}</td>
<td>{$arr.telephone}</td>
<td>{$arr.address}</td>
</tr>
{/foreach}
</tbody>
</table>
</div>
<br>
<div class="row">
<div class="col-md-6">
<i class="fa fa-info-circle"></i> Đã lựa chọn <b class="text-red" id="totals-choose">0</b> đối tượng.
</div>
<div class="col-md-6 text-right">
<button class="btn btn-default" onclick="checkAllSync(true);">
Chọn tất cả
</button>
<button class="btn btn-default" onclick="checkAllSync(false);">
Bỏ chọn tất cả
</button>
<button class="btn btn-primary" onclick="_sync(this);" data-href="{yii\helpers\Url::to(['sync','ip'=>$ip])}">
<i class="fa fa-refresh"></i> Đồng bộ
</button>
<button type="button" class="btn btn-default" data-dismiss="modal"><span class="fa fa-remove"></span> Đóng lại</button>
</div>
</div>

View File

@ -303,6 +303,12 @@ common.reset = function (e) {
}); });
} }
}; };
common.validateIp = function (Ip) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(Ip)) {
return true;
}
return false;
}
/** /**
* ========================= * =========================
*/ */

View File

@ -258,3 +258,79 @@ function _create(e) {
} }
}); });
} }
function _syncForm(e) {
var ip = $("input[name='SyncIP']").val();
if (ip === "") {
common.error("ip", "Hãy nhập địa chỉ ip thiết bị muốn đồng bộ");
return;
}
if (!common.validateIp(ip)) {
common.error("ip", "Sai định dạng ip");
return;
}
common.success("ip");
common.modalBlock(true);
$.ajax({
url: $(e).attr("data-href"),
type: 'POST',
data: {
ip: ip
},
success: function (data) {
common.modalBlock(false);
common.modalOpenFullScreen(data.form, data.title);
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
alert("Không có kết nối tới thiết bị");
}
});
}
function choooseToSync(e) {
var stt = $(e).attr("data-stt");
if (stt === "false") {
$(e).addClass("success");
$(e).attr("data-stt", "true");
$("#totals-choose").html(parseInt($("#totals-choose").html()) + 1);
} else {
$(e).removeClass("success");
$(e).attr("data-stt", "false");
$("#totals-choose").html(parseInt($("#totals-choose").html()) - 1);
}
}
function checkAllSync(stt) {
if (stt) {
$("#sync-lists").find("tr").addClass("success").attr("data-stt", "true");
$("#totals-choose").html($("#sync-lists").find("tr").length);
} else {
$("#sync-lists").find("tr").removeClass("success").attr("data-stt", "false");
$("#totals-choose").html("0");
}
}
function _sync(e) {
var lists = [];
$.each($("#sync-lists").find(".success"), function () {
lists.push($(this).attr("data-id"));
});
common.modalBlock(true);
$.ajax({
url: $(e).attr("data-href"),
type: 'POST',
data: {
lists: lists
},
success: function (data) {
common.modalBlock(false);
alert("Đồng bộ dữ liệu thành công!");
window.location.reload(true);
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
alert("Mất kết nối tới thiết bị");
}
});
}