diff --git a/controllers/ApiController.php b/controllers/ApiController.php index d5238506..271bce1d 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -96,4 +96,36 @@ class ApiController extends Controller { 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; + } + } + } diff --git a/controllers/ListManagementController.php b/controllers/ListManagementController.php index ee9ee86b..8d86140c 100644 --- a/controllers/ListManagementController.php +++ b/controllers/ListManagementController.php @@ -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; + } + } + } diff --git a/views/list-management/index.tpl b/views/list-management/index.tpl index cd84e01a..0b8a704c 100644 --- a/views/list-management/index.tpl +++ b/views/list-management/index.tpl @@ -144,6 +144,19 @@ Thêm mới +
+

Đồng bộ từ thiết bị khác

+
+
+ +
+ +
+
+ +