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

This commit is contained in:
dongpd 2020-12-10 16:36:24 +07:00
parent fbc48fca53
commit 94215e4e80
6 changed files with 100 additions and 58 deletions

View File

@ -100,14 +100,14 @@ class ApiController extends Controller {
public function actionData() {
if (Yii::$app->request->post()) {
Yii::$app->response->format = "json";
return ListManagement::find()->andWhere(["NOT IN", "name", Yii::$app->request->post()])->orderBy(['id' => SORT_DESC])->all();
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();
$lists = ListManagement::find()->andWhere(["IN", "code", Yii::$app->request->post()])->orderBy(['id' => SORT_DESC])->all();
$res = [];
foreach ($lists as $key => $value) {
$f = [];
@ -117,6 +117,7 @@ class ApiController extends Controller {
}
$res[] = [
"id" => $value->id,
"code" => $value->code,
"type" => $value->type,
"name" => $value->name,
"gender" => $value->gender,

View File

@ -268,45 +268,6 @@ class ListManagementController extends Controller {
}
}
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;
}
}
public function actionSyncFromServer() {
if (Yii::$app->request->isAjax) {
$datas = json_decode(file_get_contents("https://dev-dc.beetai.com/api/oem/get_all_image", false, stream_context_create([
@ -382,6 +343,57 @@ class ListManagementController extends Controller {
}
}
public function actionSyncFeatureFromDevice() {
if (Yii::$app->request->post()) {
Yii::$app->response->format = "json";
$data = Yii::$app->request->post();
$results = json_decode(file_get_contents("http://" . $data['ip'] . "/api/full-data", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([$data['id']])
]
])), 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 = ListManagement::findOne(['code' => $value['code']]);
if ($model) {
$model->type = $value['type'];
$model->name = $value['name'];
$model->image = json_encode($images);
$model->gender = $value['gender'];
$model->birthday = date("d/m/Y", $value['birthday']);
$model->telephone = $value['telephone'];
$model->address = $value['address'];
$model->save();
} else {
$model = new ListManagement();
$model->create([
'code' => $value['code'],
'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']
]);
}
}
return ["status" => true];
}
}
public function actionUpdateFeature() {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = "json";

View File

@ -29,6 +29,7 @@
<input type="hidden" value="{Url::to(["/list-management/add"])}" name="create_url">
<input type="hidden" value="{Url::to(["/list-management/update"])}" name="update_url">
<input type="hidden" value="{Url::to(["/list-management/sync-feature"])}" name="sync_feature_url">
<input type="hidden" value="{Url::to(["/list-management/sync-feature-from-device"])}" name="sync_feature_from_device_url">
<input type="hidden" value="{Url::to(["/list-management/update-feature"])}" name="update_feature_url">
<div class="row">
<div class="col-md-10" style="max-height: 850px;overflow-y: auto;">

View File

@ -52,7 +52,7 @@
<button class="btn btn-default" onclick="checkAllSync(false);">
Bỏ chọn tất cả
</button>
<button class="btn btn-primary" onclick="_syncFromServer(this);" data-href="{yii\helpers\Url::to(['sync-from-server'])}">
<button class="btn btn-primary" onclick="_syncFromServer();">
<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>

View File

@ -15,8 +15,8 @@
</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>
<tr onclick="choooseToSync(this);" style="cursor: pointer;" data-stt="false" data-id="{$arr.code}">
<td>{$arr.code}</td>
<td>{$typeArray[$arr.type]}</td>
<td>{$arr.name}</td>
<td>{$arr.gender}</td>
@ -39,17 +39,27 @@
</div>
<br>
<div class="row">
<div class="col-md-6">
<div class="col-md-2">
<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">
<div class="col-md-6">
<div class="hidden" id="progress-form">
Đồng bộ <b id="progress-current" class="text-green">0</b>/<b id="progress-totals" class="text-red">0</b> đối tượng.
<div class="progress">
<div class="progress-bar progress-bar-primary progress-bar-striped" id='progress' role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
0%
</div>
</div>
</div>
</div>
<div class="col-md-4 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])}">
<button class="btn btn-primary" onclick="_sync();">
<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>

View File

@ -332,7 +332,9 @@ function checkAllSync(stt) {
}
}
function _sync(e) {
var progress = 0;
var totals = 0;
function _sync() {
var lists = [];
$.each($("#sync-lists").find(".success"), function () {
lists.push($(this).attr("data-id"));
@ -342,16 +344,34 @@ function _sync(e) {
return;
}
common.modalBlock(true);
$("#progress-totals").html(lists.length);
$("#progress-form").removeClass("hidden");
totals = lists.length;
for (var i = 0; i < lists.length; i++) {
syncFeatureFromDevice(lists[i]);
}
}
function syncFeatureFromDevice(id) {
$.ajax({
url: $(e).attr("data-href"),
url: $("input[name='sync_feature_from_device_url']").val(),
type: 'POST',
data: {
lists: lists
id: id,
ip: $("input[name='SyncIP']").val()
},
success: function (data) {
common.modalBlock(false);
alert("Đồng bộ dữ liệu thành công!");
window.location.reload(true);
progress++;
$("#progress-current").html(parseInt($("#progress-current").html()) + 1);
var percent = parseInt(progress / totals * 100);
$("#progress").attr("aria-valuenow", percent);
$("#progress").attr("style", "width: " + percent + "%");
$("#progress").html(percent + "%");
if (percent >= 100) {
common.modalBlock(false);
updateFeature();
progress = 0;
}
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
@ -375,9 +395,8 @@ function _syncFromServerForm(e) {
}
});
}
var progress = 0;
var totals = 0;
function _syncFromServer(e) {
function _syncFromServer() {
var lists = [];
$.each($("#sync-lists").find(".success"), function () {
lists.push($(this).attr("data-id"));
@ -403,7 +422,6 @@ function syncFeature(id) {
id: id
},
success: function (data) {
console.log(data);
progress++;
$("#progress-current").html(parseInt($("#progress-current").html()) + 1);
var percent = parseInt(progress / totals * 100);