Commit 02b39e7e6a022743f18b761ae8e0b7805a0fca76
1 parent
41682b63
Exists in
master
app-ht
1. F 添加序列号和追加序列号可以导入mac地址
Showing
2 changed files
with
287 additions
and
43 deletions
Show diff stats
app-ht/modules/device/controllers/DeviceController.php
... | ... | @@ -7,6 +7,7 @@ use yii\base\Exception; |
7 | 7 | use yii\data\Pagination; |
8 | 8 | use app\ht\controllers\BaseController; |
9 | 9 | use common\helpers\Utils; |
10 | +use common\helpers\FileUtil; | |
10 | 11 | use common\models\AdminLog as AdminLogModel; |
11 | 12 | use domain\device\DeviceAuthFailRepository; |
12 | 13 | use domain\device\models\DeviceAuthFail as DeviceAuthFailModel; |
... | ... | @@ -182,21 +183,41 @@ class DeviceController extends BaseController |
182 | 183 | $model = $req->post('model'); |
183 | 184 | $production = $req->post('production'); |
184 | 185 | $num = $req->post('num'); |
186 | + $uploadPath = $req->post('upload_path'); | |
187 | + $type = $req->post('type'); | |
185 | 188 | $e = new stdClass(); |
186 | 189 | $e->success = false; |
187 | 190 | $e->message = 'fail'; |
188 | - if (empty($num)) { | |
189 | - $e->message = '数量不能为空'; | |
190 | - return $this->renderJson($e); | |
191 | - } | |
192 | - if ($num *1 <= 0) { | |
193 | - $e->message = '数量不能小于等于0'; | |
194 | - return $this->renderJson($e); | |
191 | + | |
192 | + if (1 == $type) { | |
193 | + if (empty($num)) { | |
194 | + $e->message = '数量不能为空'; | |
195 | + return $this->renderJson($e); | |
196 | + } | |
197 | + if ($num *1 <= 0) { | |
198 | + $e->message = '数量不能小于等于0'; | |
199 | + return $this->renderJson($e); | |
200 | + } | |
201 | + if (1 * $num > 10000) { | |
202 | + $e->message = '数量不能超过1万'; | |
203 | + return $this->renderJson($e); | |
204 | + } | |
195 | 205 | } |
196 | - if (1 * $num > 10000) { | |
197 | - $e->message = '数量不能超过1万'; | |
198 | - return $this->renderJson($e); | |
206 | + $importMacs = []; | |
207 | + if (2 == $type) { | |
208 | + if (empty($uploadPath)) { | |
209 | + $e->message = '请上传文件'; | |
210 | + return $this->renderJson($e); | |
211 | + } | |
212 | + | |
213 | + $importMacs = $this->getMacAddress($uploadPath); | |
214 | + if (empty($importMacs)) { | |
215 | + $e->message = '上传文件不合格'; | |
216 | + return $this->renderJson($e); | |
217 | + } | |
218 | + $num = count($importMacs); | |
199 | 219 | } |
220 | + | |
200 | 221 | if (empty($manufacture) || empty($project) || empty($model) || empty($production)) { |
201 | 222 | $e->message = '厂商,项目,型号,生产日期必填'; |
202 | 223 | return $this->renderJson($e); |
... | ... | @@ -247,9 +268,15 @@ class DeviceController extends BaseController |
247 | 268 | $saveData = []; |
248 | 269 | $tt = time(); |
249 | 270 | for ($i = 1 ; $i <= $num; $i++) { |
271 | + if (1 == $type) { | |
272 | + $macAddress = Utils::macGenerate(); | |
273 | + } else { | |
274 | + $index = $i -1; | |
275 | + $macAddress = isset($importMacs[$index])? $importMacs[$index]: Utils::macGenerate(); | |
276 | + } | |
250 | 277 | $saveData[] = [ |
251 | 278 | strtoupper($batchNo.sprintf('%04x', $i)), |
252 | - Utils::macGenerate(), | |
279 | + $macAddress, | |
253 | 280 | 0, |
254 | 281 | $newBatchModel->id, |
255 | 282 | 0, |
... | ... | @@ -266,6 +293,7 @@ class DeviceController extends BaseController |
266 | 293 | $saveData)->execute();//执行批量添加 |
267 | 294 | $transaction->commit(); |
268 | 295 | $e->success = true; |
296 | + $e->message = '成功生成'. $num.'个序列号'; | |
269 | 297 | } catch (Exception $exception) { |
270 | 298 | $transaction->rollBack(); |
271 | 299 | $e->message = '创建失败'; |
... | ... | @@ -1029,14 +1057,42 @@ class DeviceController extends BaseController |
1029 | 1057 | $req = Yii::$app->request; |
1030 | 1058 | $batchId = $req->post('batch_id'); |
1031 | 1059 | $appendNum = $req->post('append_num'); |
1060 | + $type = $req->post('type'); | |
1061 | + $uploadPath = $req->post('upload_path'); | |
1032 | 1062 | $e = new stdClass(); |
1033 | 1063 | $e->success = false; |
1034 | 1064 | $e->message = 'fail'; |
1035 | - if (empty($appendNum)) { | |
1036 | - $e->message = '追加数量不能为0'; | |
1037 | - return $this->renderJson($e); | |
1065 | + | |
1066 | + if (1 == $type) { | |
1067 | + if (empty($appendNum)) { | |
1068 | + $e->message = '数量不能为空'; | |
1069 | + return $this->renderJson($e); | |
1070 | + } | |
1071 | + if ($appendNum *1 <= 0) { | |
1072 | + $e->message = '数量不能小于等于0'; | |
1073 | + return $this->renderJson($e); | |
1074 | + } | |
1075 | + if (1 * $appendNum > 10000) { | |
1076 | + $e->message = '数量不能超过1万'; | |
1077 | + return $this->renderJson($e); | |
1078 | + } | |
1079 | + } | |
1080 | + $importMacs = []; | |
1081 | + if (2 == $type) { | |
1082 | + if (empty($uploadPath)) { | |
1083 | + $e->message = '请上传文件'; | |
1084 | + return $this->renderJson($e); | |
1085 | + } | |
1086 | + | |
1087 | + $importMacs = $this->getMacAddress($uploadPath); | |
1088 | + if (empty($importMacs)) { | |
1089 | + $e->message = '上传文件不合格'; | |
1090 | + return $this->renderJson($e); | |
1091 | + } | |
1092 | + $appendNum = count($importMacs); | |
1038 | 1093 | } |
1039 | 1094 | |
1095 | + | |
1040 | 1096 | $batchModel = CreateBatchRepository::findOne(['id' => $batchId]); |
1041 | 1097 | if (empty($batchModel)) { |
1042 | 1098 | $e->message = '未找到批次'; |
... | ... | @@ -1063,9 +1119,15 @@ class DeviceController extends BaseController |
1063 | 1119 | } |
1064 | 1120 | |
1065 | 1121 | for ($i = 1 ; $i <= $appendNum; $i++) { |
1122 | + if (1 == $type) { | |
1123 | + $macAddress = Utils::macGenerate(); | |
1124 | + } else { | |
1125 | + $index = $i -1; | |
1126 | + $macAddress = isset($importMacs[$index])? $importMacs[$index]: Utils::macGenerate(); | |
1127 | + } | |
1066 | 1128 | $saveData[] = [ |
1067 | 1129 | strtoupper($batchNo.sprintf('%04X', ($no1 * 1) + $i)), |
1068 | - Utils::macGenerate(), | |
1130 | + $macAddress, | |
1069 | 1131 | 0, |
1070 | 1132 | $batchId, |
1071 | 1133 | 0, |
... | ... | @@ -1082,7 +1144,7 @@ class DeviceController extends BaseController |
1082 | 1144 | $saveData)->execute();//执行批量添加 |
1083 | 1145 | $trans->commit(); |
1084 | 1146 | $e->success = true; |
1085 | - $e->message = 'ok'; | |
1147 | + $e->message = '成功追加了'.$appendNum.'个序列号'; | |
1086 | 1148 | |
1087 | 1149 | return $this->renderJson($e); |
1088 | 1150 | } catch (Exception $exception) { |
... | ... | @@ -1107,4 +1169,74 @@ class DeviceController extends BaseController |
1107 | 1169 | |
1108 | 1170 | return $this->renderJson($e); |
1109 | 1171 | } |
1172 | + | |
1173 | + /** | |
1174 | + * @return string | |
1175 | + */ | |
1176 | + public function actionUploadMac() | |
1177 | + { | |
1178 | + $e = new stdClass(); | |
1179 | + $e->success = false; | |
1180 | + $e->path = ''; | |
1181 | + | |
1182 | + if (empty($_FILES['file']['name'])) { | |
1183 | + $e->message = "请选择文件进行上传"; | |
1184 | + return $this->renderJson($e); | |
1185 | + } | |
1186 | + $file_size = $_FILES['file']['size']; | |
1187 | + if ($file_size > 5 * 1024 * 1024) { | |
1188 | + $e->message = "文件过大,不能上传大于5M的文件"; | |
1189 | + return $this->renderJson($e); | |
1190 | + } | |
1191 | + $file_name = strtolower($_FILES['file']['name']); | |
1192 | + $file_type = substr($file_name, strrpos($file_name, '.') + 1); | |
1193 | + if ($file_type != "txt") { | |
1194 | + $e->message = "文件类型只能为" . $file_type; | |
1195 | + return $this->renderJson($e); | |
1196 | + } | |
1197 | + | |
1198 | + $temp_name = $_FILES['file']['tmp_name']; | |
1199 | + $fileHandle = fopen($temp_name, "r"); | |
1200 | + $contents = fread($fileHandle, filesize($temp_name)); | |
1201 | + fclose($fileHandle); | |
1202 | + | |
1203 | + if (empty($contents)) { | |
1204 | + $e->message = '文件为空'; | |
1205 | + } else { | |
1206 | + $dir = Yii::getAlias('@app/ht') . "/web/tmp"; | |
1207 | + FileUtil::createDir($dir); | |
1208 | + $saveFilePath = $dir.'/mac_'.time().mt_rand(1000, 9999).'_.txt'; | |
1209 | + $saveFileHandle = fopen($saveFilePath, "a"); | |
1210 | + fwrite($saveFileHandle, $contents); | |
1211 | + fclose($saveFileHandle); | |
1212 | + | |
1213 | + $e->success = true; | |
1214 | + $e->message = 'ok'; | |
1215 | + $e->path = $saveFilePath; | |
1216 | + } | |
1217 | + | |
1218 | + return $this->renderJson($e); | |
1219 | + } | |
1220 | + | |
1221 | + /** | |
1222 | + * @param $pathFile | |
1223 | + * @return array|null | |
1224 | + */ | |
1225 | + private function getMacAddress($pathFile) | |
1226 | + { | |
1227 | + $fileHandle = fopen($pathFile, "r"); | |
1228 | + $macList = []; | |
1229 | + while(!feof($fileHandle)) { | |
1230 | + $macList [] = fgets($fileHandle); | |
1231 | + } | |
1232 | + fclose($fileHandle); | |
1233 | + $returnMac = []; | |
1234 | + foreach ($macList as $k => $v) { | |
1235 | + if (preg_match('/^[0-9a-fA-F:]{12,18}$/', $v)) { | |
1236 | + $returnMac[] = strtoupper(trim($v)); | |
1237 | + } | |
1238 | + } | |
1239 | + | |
1240 | + return $returnMac; | |
1241 | + } | |
1110 | 1242 | } |
1111 | 1243 | \ No newline at end of file | ... | ... |
app-ht/modules/device/views/device/createDevice.php
... | ... | @@ -15,6 +15,12 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); |
15 | 15 | .create-div .form-group{padding:1rem 0} |
16 | 16 | .create-div .full-width{width:100%;} |
17 | 17 | .combo-select{max-width:100% !important;} |
18 | + .source-cls{padding:1rem 0.5rem; box-sizing: border-box;} | |
19 | + .source-cls .add_way, | |
20 | + .source-cls .append_way{margin-left:1rem;} | |
21 | + .source-cls .upload-cls{position: relative;width:100px;display: inline-block;cursor: pointer} | |
22 | + .source-cls .upload-cls-txt{position: absolute;top:0;left:0;width: 100%;display: inline; padding: 0.5rem 0;text-align: center;} | |
23 | + .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;} | |
18 | 24 | </style> |
19 | 25 | <div class="panel panel-default"> |
20 | 26 | <div class="panel-body form-inline create-div"> |
... | ... | @@ -73,9 +79,20 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); |
73 | 79 | </div> |
74 | 80 | </div> |
75 | 81 | <div class="form-group col-sm-12"> |
76 | - <label for="production" class="col-sm-4 control-label text-right">数量:</label> | |
82 | + <label for="production" class="col-sm-4 control-label text-right">来源:</label> | |
77 | 83 | <div class="col-sm-4 form-inline"> |
78 | - <input type="number" class="form-control full-width" id="num" name="num" value="" autocomplete="off"> | |
84 | + <div class="source-cls" style="padding-top: 0;"> | |
85 | + 自动生成 | |
86 | + <input type="number" class="form-control" style="max-width: 100px;" id="num" name="num" value="" placeholder="例如:20" autocomplete="off"> | |
87 | + <input type="radio" class="form-control add_way" name="add_way" value="1" checked/> | |
88 | + </div> | |
89 | + | |
90 | + <div class="source-cls"> | |
91 | + 导入文件 | |
92 | + <span class="upload-cls form-control" ><input type="file" style="max-width: 100px;" class="file-upload-cls" id="file_num" name="file" disabled><span class="upload-cls-txt">点击上传</span></span><input type="radio" class="form-control add_way" name="add_way" value="2" /> | |
93 | + <input type="hidden" value="" id="upload_path"> <span style="color:#cc0000">(格式是txt,每行一个mac地址,5M以内)</span> | |
94 | + </div> | |
95 | + | |
79 | 96 | </div> |
80 | 97 | </div> |
81 | 98 | |
... | ... | @@ -86,7 +103,6 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); |
86 | 103 | |
87 | 104 | </div> |
88 | 105 | |
89 | - | |
90 | 106 | <div role="tabpanel" class="tab-pane" id="append"> |
91 | 107 | <form action="" id="append-form" class="filter-form"> |
92 | 108 | <div class="form-group col-sm-12"> |
... | ... | @@ -96,9 +112,21 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); |
96 | 112 | </div> |
97 | 113 | </div> |
98 | 114 | <div class="form-group col-sm-12"> |
99 | - <label for="append_num" class="col-sm-4 control-label text-right">追加数量:</label> | |
115 | + <label for="append_num" class="col-sm-4 control-label text-right">来源:</label> | |
100 | 116 | <div class="col-sm-4 form-inline"> |
101 | - <input type="number" class="form-control full-width" id="append_num" name="append_num" value="" autocomplete="off"> | |
117 | + | |
118 | + <div class="source-cls" style="padding-top: 0;"> | |
119 | + 追加生成 | |
120 | + <input type="number" class="form-control" style="max-width: 100px;" id="append_num" name="append_num" value="" placeholder="例如:20" autocomplete="off"> | |
121 | + <input type="radio" class="form-control append_way" name="append_way" value="1" checked/> | |
122 | + </div> | |
123 | + | |
124 | + <div class="source-cls"> | |
125 | + 导入文件 | |
126 | + <span class="upload-cls form-control" ><input type="file" style="max-width: 100px;" class="file-upload-cls" id="append_file_num" name="file" disabled><span class="upload-cls-txt">点击上传</span></span><input type="radio" class="form-control append_way" name="append_way" value="2" /> <span style="color:#cc0000">(格式是txt,每行一个mac地址,5M以内)</span> | |
127 | + <input type="hidden" value="" id="append_upload_path"> | |
128 | + </div> | |
129 | + | |
102 | 130 | </div> |
103 | 131 | </div> |
104 | 132 | |
... | ... | @@ -115,7 +143,7 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); |
115 | 143 | </div> |
116 | 144 | |
117 | 145 | </div> |
118 | - | |
146 | +<script src="<?= Yii::$app->request->baseUrl . "/exts/base/1.0.0/ui/ajaxfileupload/ajaxfileupload-1.0.0.js"?>"></script> | |
119 | 147 | <script src="<?= Yii::$app->request->baseUrl . "/exts/combo-select/js/jquery.combo.select.js"?>"></script> |
120 | 148 | <link rel="stylesheet" type="text/css" href="<?= Yii::$app->request->baseUrl . "/exts/combo-select/css/combo.select.css"?>" /> |
121 | 149 | <script type="text/javascript"> |
... | ... | @@ -123,6 +151,7 @@ CssFiles::register($this, 'exts/showimg/css/showimg.css'); |
123 | 151 | var saveUrl = "<?=Url::toRoute('/device/device/do-create-device')?>"; |
124 | 152 | var getBatchSelectUrl = "<?=Url::toRoute('/device/device/get-batch-select')?>"; |
125 | 153 | var appendSerialUrl = "<?=Url::toRoute('/device/device/append-serial-no')?>"; |
154 | + var upLoadURL = "<?=Url::toRoute('/device/device/upload-mac')?>"; | |
126 | 155 | $(function() { |
127 | 156 | |
128 | 157 | function selectAll(id) { |
... | ... | @@ -222,14 +251,24 @@ $(function() { |
222 | 251 | alert('请选生产日期'); |
223 | 252 | return false; |
224 | 253 | } |
225 | - //production = production.split('_'); | |
254 | + | |
255 | + var type = $('.add_way:checked').val(); | |
226 | 256 | var num = $('#num').val(); |
227 | - var par = /^[0-9]+$/; | |
228 | - if (par.test(num) && (num > 0)) { | |
229 | - // 合法的 | |
230 | - } else { | |
231 | - alert('请录入大于0的整数'); | |
232 | - return false; | |
257 | + if (1 == type) { | |
258 | + var par = /^[0-9]+$/; | |
259 | + if (par.test(num) && (num > 0)) { | |
260 | + // 合法的 | |
261 | + } else { | |
262 | + alert('请录入大于0的整数'); | |
263 | + return false; | |
264 | + } | |
265 | + } | |
266 | + var uploadPath = $('#upload_path').val(); | |
267 | + if (2 == type) { | |
268 | + if ("" == uploadPath || null == uploadPath) { | |
269 | + alert('请上传文件'); | |
270 | + return false; | |
271 | + } | |
233 | 272 | } |
234 | 273 | |
235 | 274 | var params = { |
... | ... | @@ -237,14 +276,16 @@ $(function() { |
237 | 276 | project: project, |
238 | 277 | model: model, |
239 | 278 | production: production, |
240 | - num: num | |
279 | + num: num, | |
280 | + type:type, | |
281 | + upload_path:uploadPath | |
241 | 282 | } |
242 | 283 | $.post(saveUrl, params, function(res) { |
243 | 284 | if (!res.success) { |
244 | 285 | alert(res.message); |
245 | 286 | return false; |
246 | 287 | } |
247 | - alert('成功生成'+params.num +'个序列号'); | |
288 | + alert(res.message); | |
248 | 289 | window.location.href = '<?=Url::toRoute('/datas/device/index')?>' |
249 | 290 | }, 'json') |
250 | 291 | }) |
... | ... | @@ -256,21 +297,37 @@ $(function() { |
256 | 297 | alert('请选择批次'); |
257 | 298 | return false; |
258 | 299 | } |
259 | - var par = /^[0-9]+$/; | |
260 | - if (par.test(append_num) && (append_num > 0)) { | |
261 | - // 合法的 | |
262 | - } else { | |
263 | - alert('请录入大于0的整数'); | |
264 | - return false; | |
265 | - } | |
266 | 300 | |
267 | - if (append_num*1 > 3000) { | |
268 | - alert('追加数量不要超过3000'); | |
269 | - return false; | |
301 | + var type = $('.append_way:checked').val(); | |
302 | + if (1 == type) { | |
303 | + var par = /^[0-9]+$/; | |
304 | + if (par.test(append_num) && (append_num > 0)) { | |
305 | + // 合法的 | |
306 | + } else { | |
307 | + alert('请录入大于0的整数'); | |
308 | + return false; | |
309 | + } | |
310 | + if (append_num*1 > 3000) { | |
311 | + alert('追加数量不要超过3000'); | |
312 | + return false; | |
313 | + } | |
314 | + } | |
315 | + var uploadPath = $('#append_upload_path').val(); | |
316 | + if (2 == type) { | |
317 | + if ("" == uploadPath || null == uploadPath) { | |
318 | + alert('请上传文件'); | |
319 | + return false; | |
320 | + } | |
270 | 321 | } |
271 | - $.post(appendSerialUrl,{batch_id:batch_id, append_num:append_num}, function(ajaxRes){ | |
322 | + | |
323 | + $.post(appendSerialUrl,{ | |
324 | + batch_id:batch_id, | |
325 | + append_num:append_num, | |
326 | + type:type, | |
327 | + upload_path:uploadPath | |
328 | + }, function(ajaxRes){ | |
272 | 329 | if (ajaxRes.success) { |
273 | - alert('成功追加'+append_num+'个序列号'); | |
330 | + alert(ajaxRes.message); | |
274 | 331 | window.location.href = '<?=Url::toRoute('/datas/device/index')?>' |
275 | 332 | } else { |
276 | 333 | alert(ajaxRes.message); |
... | ... | @@ -278,5 +335,60 @@ $(function() { |
278 | 335 | }, 'json') |
279 | 336 | }) |
280 | 337 | |
338 | + $('.add_way').click(function(e){ | |
339 | + var type = $(this).val(); | |
340 | + if (1 == type) { | |
341 | + $('#num').removeAttr('disabled'); | |
342 | + $('#file_num').prop('disabled', true); | |
343 | + } else { | |
344 | + $('#file_num').removeAttr('disabled'); | |
345 | + $('#num').prop('disabled', true); | |
346 | + } | |
347 | + }) | |
348 | + | |
349 | + $('#file_num').change(function(){ | |
350 | + $.ajaxFileUpload({ | |
351 | + url: upLoadURL, | |
352 | + fileElementId: "file_num", | |
353 | + dataType: "json", | |
354 | + success: function (res) { | |
355 | + if (res.success) { | |
356 | + $('#upload_path').val(res.path); | |
357 | + alert('成功上传'); | |
358 | + } else { | |
359 | + $('#upload_path').val(''); | |
360 | + alert(res.message); | |
361 | + } | |
362 | + }, | |
363 | + }); | |
364 | + }) | |
365 | + | |
366 | + $('.append_way').click(function(e){ | |
367 | + var type = $(this).val(); | |
368 | + if (1 == type) { | |
369 | + $('#append_num').removeAttr('disabled'); | |
370 | + $('#append_file_num').prop('disabled', true); | |
371 | + } else { | |
372 | + $('#append_file_num').removeAttr('disabled'); | |
373 | + $('#append_num').prop('disabled', true); | |
374 | + } | |
375 | + }) | |
376 | + | |
377 | + $('#append_file_num').change(function(){ | |
378 | + $.ajaxFileUpload({ | |
379 | + url: upLoadURL, | |
380 | + fileElementId: "append_file_num", | |
381 | + dataType: "json", | |
382 | + success: function (res) { | |
383 | + if (res.success) { | |
384 | + $('#append_upload_path').val(res.path); | |
385 | + alert('成功上传'); | |
386 | + } else { | |
387 | + $('#append_upload_path').val(''); | |
388 | + alert(res.message); | |
389 | + } | |
390 | + }, | |
391 | + }); | |
392 | + }) | |
281 | 393 | }) |
282 | 394 | </script> | ... | ... |