From 02b39e7e6a022743f18b761ae8e0b7805a0fca76 Mon Sep 17 00:00:00 2001 From: xu Date: Wed, 13 Nov 2019 10:13:49 +0800 Subject: [PATCH] app-ht 1. F 添加序列号和追加序列号可以导入mac地址 --- app-ht/modules/device/controllers/DeviceController.php | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- app-ht/modules/device/views/device/createDevice.php | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 287 insertions(+), 43 deletions(-) diff --git a/app-ht/modules/device/controllers/DeviceController.php b/app-ht/modules/device/controllers/DeviceController.php index 3318d7a..7a2ac66 100644 --- a/app-ht/modules/device/controllers/DeviceController.php +++ b/app-ht/modules/device/controllers/DeviceController.php @@ -7,6 +7,7 @@ use yii\base\Exception; use yii\data\Pagination; use app\ht\controllers\BaseController; use common\helpers\Utils; +use common\helpers\FileUtil; use common\models\AdminLog as AdminLogModel; use domain\device\DeviceAuthFailRepository; use domain\device\models\DeviceAuthFail as DeviceAuthFailModel; @@ -182,21 +183,41 @@ class DeviceController extends BaseController $model = $req->post('model'); $production = $req->post('production'); $num = $req->post('num'); + $uploadPath = $req->post('upload_path'); + $type = $req->post('type'); $e = new stdClass(); $e->success = false; $e->message = 'fail'; - if (empty($num)) { - $e->message = '数量不能为空'; - return $this->renderJson($e); - } - if ($num *1 <= 0) { - $e->message = '数量不能小于等于0'; - return $this->renderJson($e); + + if (1 == $type) { + if (empty($num)) { + $e->message = '数量不能为空'; + return $this->renderJson($e); + } + if ($num *1 <= 0) { + $e->message = '数量不能小于等于0'; + return $this->renderJson($e); + } + if (1 * $num > 10000) { + $e->message = '数量不能超过1万'; + return $this->renderJson($e); + } } - if (1 * $num > 10000) { - $e->message = '数量不能超过1万'; - return $this->renderJson($e); + $importMacs = []; + if (2 == $type) { + if (empty($uploadPath)) { + $e->message = '请上传文件'; + return $this->renderJson($e); + } + + $importMacs = $this->getMacAddress($uploadPath); + if (empty($importMacs)) { + $e->message = '上传文件不合格'; + return $this->renderJson($e); + } + $num = count($importMacs); } + if (empty($manufacture) || empty($project) || empty($model) || empty($production)) { $e->message = '厂商,项目,型号,生产日期必填'; return $this->renderJson($e); @@ -247,9 +268,15 @@ class DeviceController extends BaseController $saveData = []; $tt = time(); for ($i = 1 ; $i <= $num; $i++) { + if (1 == $type) { + $macAddress = Utils::macGenerate(); + } else { + $index = $i -1; + $macAddress = isset($importMacs[$index])? $importMacs[$index]: Utils::macGenerate(); + } $saveData[] = [ strtoupper($batchNo.sprintf('%04x', $i)), - Utils::macGenerate(), + $macAddress, 0, $newBatchModel->id, 0, @@ -266,6 +293,7 @@ class DeviceController extends BaseController $saveData)->execute();//执行批量添加 $transaction->commit(); $e->success = true; + $e->message = '成功生成'. $num.'个序列号'; } catch (Exception $exception) { $transaction->rollBack(); $e->message = '创建失败'; @@ -1029,14 +1057,42 @@ class DeviceController extends BaseController $req = Yii::$app->request; $batchId = $req->post('batch_id'); $appendNum = $req->post('append_num'); + $type = $req->post('type'); + $uploadPath = $req->post('upload_path'); $e = new stdClass(); $e->success = false; $e->message = 'fail'; - if (empty($appendNum)) { - $e->message = '追加数量不能为0'; - return $this->renderJson($e); + + if (1 == $type) { + if (empty($appendNum)) { + $e->message = '数量不能为空'; + return $this->renderJson($e); + } + if ($appendNum *1 <= 0) { + $e->message = '数量不能小于等于0'; + return $this->renderJson($e); + } + if (1 * $appendNum > 10000) { + $e->message = '数量不能超过1万'; + return $this->renderJson($e); + } + } + $importMacs = []; + if (2 == $type) { + if (empty($uploadPath)) { + $e->message = '请上传文件'; + return $this->renderJson($e); + } + + $importMacs = $this->getMacAddress($uploadPath); + if (empty($importMacs)) { + $e->message = '上传文件不合格'; + return $this->renderJson($e); + } + $appendNum = count($importMacs); } + $batchModel = CreateBatchRepository::findOne(['id' => $batchId]); if (empty($batchModel)) { $e->message = '未找到批次'; @@ -1063,9 +1119,15 @@ class DeviceController extends BaseController } for ($i = 1 ; $i <= $appendNum; $i++) { + if (1 == $type) { + $macAddress = Utils::macGenerate(); + } else { + $index = $i -1; + $macAddress = isset($importMacs[$index])? $importMacs[$index]: Utils::macGenerate(); + } $saveData[] = [ strtoupper($batchNo.sprintf('%04X', ($no1 * 1) + $i)), - Utils::macGenerate(), + $macAddress, 0, $batchId, 0, @@ -1082,7 +1144,7 @@ class DeviceController extends BaseController $saveData)->execute();//执行批量添加 $trans->commit(); $e->success = true; - $e->message = 'ok'; + $e->message = '成功追加了'.$appendNum.'个序列号'; return $this->renderJson($e); } catch (Exception $exception) { @@ -1107,4 +1169,74 @@ class DeviceController extends BaseController return $this->renderJson($e); } + + /** + * @return string + */ + public function actionUploadMac() + { + $e = new stdClass(); + $e->success = false; + $e->path = ''; + + if (empty($_FILES['file']['name'])) { + $e->message = "请选择文件进行上传"; + return $this->renderJson($e); + } + $file_size = $_FILES['file']['size']; + if ($file_size > 5 * 1024 * 1024) { + $e->message = "文件过大,不能上传大于5M的文件"; + return $this->renderJson($e); + } + $file_name = strtolower($_FILES['file']['name']); + $file_type = substr($file_name, strrpos($file_name, '.') + 1); + if ($file_type != "txt") { + $e->message = "文件类型只能为" . $file_type; + return $this->renderJson($e); + } + + $temp_name = $_FILES['file']['tmp_name']; + $fileHandle = fopen($temp_name, "r"); + $contents = fread($fileHandle, filesize($temp_name)); + fclose($fileHandle); + + if (empty($contents)) { + $e->message = '文件为空'; + } else { + $dir = Yii::getAlias('@app/ht') . "/web/tmp"; + FileUtil::createDir($dir); + $saveFilePath = $dir.'/mac_'.time().mt_rand(1000, 9999).'_.txt'; + $saveFileHandle = fopen($saveFilePath, "a"); + fwrite($saveFileHandle, $contents); + fclose($saveFileHandle); + + $e->success = true; + $e->message = 'ok'; + $e->path = $saveFilePath; + } + + return $this->renderJson($e); + } + + /** + * @param $pathFile + * @return array|null + */ + private function getMacAddress($pathFile) + { + $fileHandle = fopen($pathFile, "r"); + $macList = []; + while(!feof($fileHandle)) { + $macList [] = fgets($fileHandle); + } + fclose($fileHandle); + $returnMac = []; + foreach ($macList as $k => $v) { + if (preg_match('/^[0-9a-fA-F:]{12,18}$/', $v)) { + $returnMac[] = strtoupper(trim($v)); + } + } + + return $returnMac; + } } \ No newline at end of file diff --git a/app-ht/modules/device/views/device/createDevice.php b/app-ht/modules/device/views/device/createDevice.php index 7be6ef4..30e79dc 100644 --- a/app-ht/modules/device/views/device/createDevice.php +++ b/app-ht/modules/device/views/device/createDevice.php @@ -15,6 +15,12 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); .create-div .form-group{padding:1rem 0} .create-div .full-width{width:100%;} .combo-select{max-width:100% !important;} + .source-cls{padding:1rem 0.5rem; box-sizing: border-box;} + .source-cls .add_way, + .source-cls .append_way{margin-left:1rem;} + .source-cls .upload-cls{position: relative;width:100px;display: inline-block;cursor: pointer} + .source-cls .upload-cls-txt{position: absolute;top:0;left:0;width: 100%;display: inline; padding: 0.5rem 0;text-align: center;} + .source-cls .file-upload-cls{background-color:rgba(0,0,0,0);opacity: 0;max-width: 100px;position: absolute;top: 0;left: 0;z-index: 222;padding: 0.5rem;}
@@ -73,9 +79,20 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css');
- +
- +
+ 自动生成 + + +
+ +
+ 导入文件 + 点击上传 + (格式是txt,每行一个mac地址,5M以内) +
+
@@ -86,7 +103,6 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); -
@@ -96,9 +112,21 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css');
- +
- + +
+ 追加生成 + + +
+ +
+ 导入文件 + 点击上传 (格式是txt,每行一个mac地址,5M以内) + +
+
@@ -115,7 +143,7 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); - + " /> -- libgit2 0.21.0