From 37a28dcce831e3165b29d360954d76fdbacac5bc Mon Sep 17 00:00:00 2001 From: xu Date: Tue, 5 Nov 2019 18:14:26 +0800 Subject: [PATCH] 1. F 授权接口调整,有重大的bug 2. F 版本管理代码被覆盖了 --- app-api/controllers/AuthDeviceController.php | 14 ++++---------- app-ht/modules/datas/views/device/device-list.php | 2 +- app-ht/modules/device/controllers/DeviceController.php | 477 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------ app-ht/modules/device/views/device/auth-fail-index.php | 46 +++++++++++++++++++++++++++++++++++++++++----- app-ht/modules/device/views/device/delete-index.php | 45 +++++++++++++++++++++++++++++++++++++++++++-- app-ht/modules/device/views/device/export.php | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app-ht/modules/device/views/device/index.php | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- app-ht/modules/upgrade/controllers/UpgradeController.php | 33 ++++++++++++++++++++++++++++++++- console/controllers/TestController.php | 16 ++++++++-------- domain/device/Device.php | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------- domain/device/DeviceAuthFailRepository.php | 13 +++++++++++++ domain/device/DeviceRepository.php | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 912 insertions(+), 113 deletions(-) create mode 100644 app-ht/modules/device/views/device/export.php diff --git a/app-api/controllers/AuthDeviceController.php b/app-api/controllers/AuthDeviceController.php index 6ac3070..c7bf66d 100644 --- a/app-api/controllers/AuthDeviceController.php +++ b/app-api/controllers/AuthDeviceController.php @@ -72,16 +72,7 @@ class AuthDeviceController extends BaseController $e->message = '签名有误'; return $e; } - // 检测是否授权过了的设备 - // 这里还得判读删除之后恢复的,状态是未授权 - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'is_delete' => 0]); - if ($deviceModel && DeviceStatus::HAS_AUTH == $deviceModel->status) { - $e->mac = $deviceModel->mac; - $e->serial_no = $deviceModel->serial_no; - $e->message = 'ok'; - $e->status = 5; - return $e; - } + $authResult = Device::authDevice($deviceId, $manufactureNo, $projectNo, $modelNo, $productionNo); if ($authResult->success) { @@ -93,6 +84,9 @@ class AuthDeviceController extends BaseController $e->status = 4; $e->message = $authResult->message; } + if ($authResult->status > 0) { + $e->status = $authResult->status; + } return $e; } diff --git a/app-ht/modules/datas/views/device/device-list.php b/app-ht/modules/datas/views/device/device-list.php index 0f4ba64..1b529a1 100644 --- a/app-ht/modules/datas/views/device/device-list.php +++ b/app-ht/modules/datas/views/device/device-list.php @@ -61,7 +61,7 @@ $this->params['breadcrumbs'][] = $this->title;
-
>厂商: 项目: 型号: 生产日期: +
厂商: 项目: 型号: 生产日期: 批次编号:
diff --git a/app-ht/modules/device/controllers/DeviceController.php b/app-ht/modules/device/controllers/DeviceController.php index cfcf568..b0de48c 100644 --- a/app-ht/modules/device/controllers/DeviceController.php +++ b/app-ht/modules/device/controllers/DeviceController.php @@ -28,6 +28,12 @@ class DeviceController extends BaseController */ public function actionIndex() { + $params = $this->dataList(1); + return $this->render('index', $params); + } + + private function dataList($type) + { $request = Yii::$app->request; $serialNo = $request->get('serial_no'); $mac = $request->get('mac'); @@ -95,11 +101,16 @@ class DeviceController extends BaseController if (0 >= $page) { $page = 1; } - $pageSize = 20; - $page = ($page -1) * $pageSize; - // DeviceRepository::getList($where, $pageSize, $page); - $deviceData = DeviceRepository::getList($where, $pageSize, $page); - $pages = new Pagination(['totalCount' => DeviceRepository::getListCount($where), 'pageSize' => $pageSize]); + if (1 == $type) { + $pageSize = 20; + $page = ($page -1) * $pageSize; + $deviceData = DeviceRepository::getList($where, $pageSize, $page); + $pages = new Pagination(['totalCount' => DeviceRepository::getListCount($where), 'pageSize' => $pageSize]); + } else { + $deviceData = DeviceRepository::getList($where, 0, 0); + $pages = null; + } + $statusList = DeviceStatus::statusLabels(); // $params['statusList'] = $statusList; @@ -121,7 +132,7 @@ class DeviceController extends BaseController 'status' => $status ]; - return $this->render('index', $params); + return $params; } /** @@ -319,16 +330,21 @@ class DeviceController extends BaseController return $this->renderJson($e); } + /** - * 导出订单数据 + * 导出数据 * @return string */ - public function actionExportDa() + public function actionExport() { - $request = Yii::$app->request; + $params = $this->dataList(0); + return $this->renderPartial('export', $params); } + /** + * @return string + */ public function actionAuthFailIndex() { $request = Yii::$app->request; @@ -429,6 +445,55 @@ class DeviceController extends BaseController return $this->renderJson($e); } + + public function actionBatchAuthDevice() + { + $req = Yii::$app->request; + $ids = $req->post('ids'); + $e = new stdClass(); + $e->success = false; + $e->message = 'fail'; + $ids = explode(',', $ids); + $deviceModels = DeviceRepository::findAll(['id' => $ids]); + if (empty($deviceModels)) { + $e->message = '找不到该设备'; + return $this->renderJson($e); + } + + $isAuthCount = 0; + $emptyDeviceIdCount = 0; + $updateIds = []; + foreach ($deviceModels as $k => $deviceModel ) { + if(DeviceStatus::HAS_AUTH == $deviceModel->status) { + $isAuthCount++; + } + if (empty($deviceModel->device_id)) { + $emptyDeviceIdCount++; + } + if (DeviceStatus::HAS_AUTH != $deviceModel->status && $deviceModel->device_id) { + $updateIds[] = $deviceModel->id; + } + } + + $tt = time(); + if ($updateIds) { + $attr = [ + 'auth_at' => $tt, + 'status' => DeviceStatus::HAS_AUTH + ]; + $condition = [ + 'id' => $updateIds + ]; + DeviceModel::updateAll($attr, $condition); + $e->success = true; + $e->message = '成功授权'.count($updateIds).'个设备'; + } else { + $e->message = '设备ID都为空或部分设备已经授权过,授权失败'; + } + + return $this->renderJson($e); + } + /** * @return string */ @@ -444,9 +509,11 @@ class DeviceController extends BaseController $e->message = '找不到该设备'; return $this->renderJson($e); } - + if (empty($deviceModel->device_id) || DeviceStatus::HAS_AUTH != $deviceModel->status) { + $e->message = '必须是授权成功且有设备ID的才能删除'; + return $this->renderJson($e); + } $tt = time(); - $deviceModel->is_delete = 1; $result = $deviceModel->save(); if ($result) { @@ -461,6 +528,50 @@ class DeviceController extends BaseController /** * @return string */ + public function actionBatchDelDevice() + { + $req = Yii::$app->request; + $ids = $req->post('ids'); + $e = new stdClass(); + $e->success = false; + $e->message = 'fail'; + $ids = explode(',', $ids); + $deviceModels = DeviceRepository::findAll(['id' => $ids]); + if (empty($deviceModels)) { + $e->message = '找不到该设备'; + return $this->renderJson($e); + } + + $tt = time(); + $delCount = 0; + $deleteIds = []; + foreach ($deviceModels as $k => $deviceModel) { + if ($deviceModel->device_id && DeviceStatus::HAS_AUTH == $deviceModel->status) { + $deleteIds[] = $deviceModel->id; + $delCount++; + } + } + $attr = [ + 'is_delete' => 1 + ]; + $condition = [ + 'id' => $deleteIds + ]; + DeviceModel::updateAll($attr, $condition); + + if ($delCount > 0) { + $e->message = '成功删除'.$delCount .'个设备'; + $e->success = true; + } else { + $e->message = '必须是授权成功的设备才能删除,删除失败'; + } + + return $this->renderJson($e); + } + + /** + * @return string + */ public function actionDoEdit() { $req = Yii::$app->request; @@ -564,11 +675,11 @@ class DeviceController extends BaseController $batchNo = Device::getBatchNo($deviceFailModel->manufacture_no, $deviceFailModel->project_no, $deviceFailModel->model_no, $deviceFailModel->production_no); $batchInfo = CreateBatchRepository::findOne(['batch_no' => $batchNo]); if (empty($batchInfo)) { - $e->message = '该厂商未生产过该批次的设备无法生成'; + $e->message = '该厂商未生产过该批次的设备无法处理'; return $this->renderJson($e); } $batchId = $batchInfo->id; - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceFailModel->device_id]); + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceFailModel->device_id , 'is_delete' => 0]); if ($deviceModel) { $e->message = '该设备已经授权过'; return $this->renderJson($e); @@ -576,71 +687,224 @@ class DeviceController extends BaseController $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]); if (($count *1) >= ($batchInfo->num * 1)) { - $exitDeviceModel = DeviceModel::find(); - $exitDeviceModel->where(['batch_id' => $batchId]); - $exitDeviceModel->orderBy('serial_no desc'); - $exitDevice = $exitDeviceModel->one(); - $serialNo = ''; - if ($exitDevice) { - $numNo = mb_substr($exitDevice->serial_no, -4); - $no1 = hexdec($numNo); - $no =($no1 * 1) + 1; - $newNo = sprintf('%04X', $no); - $serialNo = $batchNo.$newNo; + $trans = Yii::$app->getDb()->beginTransaction(); + try { + + Device::createWithMacSerialNo($batchId, $batchNo, $deviceFailModel->device_id, $deviceFailModel->apply_at, 1, DeviceStatus::HAS_AUTH); + // 设备改为删除了 + $deviceFailModel->is_delete = 1; + $deviceFailModel->save(); + $trans->commit(); + $e->success = true; + return $this->renderJson($e); + } catch (Exception $exception) { + $trans->rollBack(); + $e->message = '处理失败,请再尝试一次'; + return $this->renderJson($e); } - $macAddress = Utils::macGenerate(); - $deviceFind = DeviceModel::find(); - $deviceFind->where( - [ - 'or', - ['=', 'serial_no', $serialNo], - ['=', 'mac', $macAddress] - ] - ); - $checkDevice = $deviceFind->one(); - if ($checkDevice) { - $e->message = 'MAC地址或序列号已经存在,请再授权一次'; + } else { + $newDeviceModel = DeviceRepository::findOne(['device_id'=> null, 'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); + if (!$newDeviceModel) { + $trans = Yii::$app->getDb()->beginTransaction(); + try { + Device::createWithMacSerialNo($batchId, $batchNo, $deviceFailModel->device_id, $deviceFailModel->apply_at, 1, DeviceStatus::HAS_AUTH); + $deviceFailModel->is_delete = 1; + $deviceFailModel->save(); + $trans->commit(); + $e->success = true; + $e->message = '处理成功'; + } catch (Exception $exception) { + $trans->rollBack(); + $e->message = '处理失败, 生成的设备序列号用完,请再点击处理一次'; + } return $this->renderJson($e); } + $trans = Yii::$app->getDb()->beginTransaction(); try { - $newDevice = new DeviceModel(); - $newDevice->serial_no = $serialNo; - $newDevice->mac = $macAddress; - $newDevice->device_id = $deviceFailModel->device_id; - $newDevice->batch_id = $batchId; - $newDevice->status = DeviceStatus::HAS_AUTH; - $newDevice->has_re_auth = 1; - $newDevice->apply_at = time(); - $newDevice->save(); + $newDeviceModel->device_id = $deviceFailModel->device_id; + $newDeviceModel->status = DeviceStatus::HAS_AUTH; + $newDeviceModel->apply_at = $deviceFailModel->apply_at; + $newDeviceModel->auth_at = time(); + $newDeviceModel->status = DeviceStatus::HAS_AUTH; + $newDeviceModel->save(); + $deviceFailModel->is_delete = 1; $deviceFailModel->save(); $trans->commit(); $e->success = true; + $e->message = '处理成功'; return $this->renderJson($e); - } catch (Exception $exception){ - + } catch (Exception $exception) { $trans->rollBack(); - $e->message = '授权失败'; + $e->message = '处理失败,系统错误'; return $this->renderJson($e); } + } + } - } else { - $newDeviceModel = DeviceRepository::findOne(['device_id'=> null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); - $newDeviceModel->device_id = $deviceFailModel->device_id; - $newDeviceModel->status = DeviceStatus::HAS_AUTH; - $newDeviceModel->apply_at = time(); - $newDeviceModel->auth_at = time(); - $newDeviceModel->status = DeviceStatus::HAS_AUTH; - $saveResult = $newDeviceModel->save(); - if ($saveResult) { - $e->success = true; + /** + * @return string + * @throws \yii\db\Exception + */ + public function actionBatchReAuth() + { + $req = Yii::$app->request; + $ids = $req->post('ids'); + $e = new stdClass(); + $e->success = false; + $e->message = 'fail'; + $ids = explode(',', $ids); + $deviceFailModels = DeviceAuthFailRepository::findAll(['id' => $ids, 'is_delete' => 0]); + if (empty($deviceFailModels)) { + $e->message = '未找到记录'; + return $this->renderJson($e); + } + $exitCount = 0; + $successCount = 0; + $failCount = 0; + $notExitCount = 0; + $deviceExitCount = 0; + foreach ($deviceFailModels as $k => $deviceFailModel) { + $batchNo = Device::getBatchNo($deviceFailModel->manufacture_no, $deviceFailModel->project_no, $deviceFailModel->model_no, $deviceFailModel->production_no); + + $batchInfo = CreateBatchRepository::findOne(['batch_no' => $batchNo]); + if (empty($batchInfo)) { + //该厂商未生产过该批次的设备无法生成 + $notExitCount++; + continue; + } + $batchId = $batchInfo->id; + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceFailModel->device_id , 'is_delete' => 0]); + if ($deviceModel) { + //该设备已经授权过 + $deviceExitCount++; + continue; + } + $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]); + + if (($count *1) >= ($batchInfo->num * 1)) { + $exitDeviceModel = DeviceModel::find(); + $exitDeviceModel->where(['batch_id' => $batchId]); + $exitDeviceModel->orderBy('serial_no desc'); + $exitDevice = $exitDeviceModel->one(); + $serialNo = ''; + if ($exitDevice) { + $numNo = mb_substr($exitDevice->serial_no, -4); + $no1 = hexdec($numNo); + $no =($no1 * 1) + 1; + $newNo = sprintf('%04X', $no); + $serialNo = $batchNo.$newNo; + } + + $macAddress = Utils::macGenerate(); + $deviceFind = DeviceModel::find(); + $deviceFind->where( + [ + 'or', + ['=', 'serial_no', $serialNo], + ['=', 'mac', $macAddress] + ] + ); + $checkDevice = $deviceFind->one(); + if ($checkDevice) { + //'MAC地址或序列号已经存在,请再处理一次'; + $exitCount++; + continue; + } + + $trans = Yii::$app->getDb()->beginTransaction(); + try { + $item = [ + 'serial_no' => $serialNo, + 'mac' => $macAddress, + 'device_id' => $deviceFailModel->device_id, + 'batch_id' => $batchId, + 'status' => DeviceStatus::HAS_AUTH, + 'has_re_auth' => 1, + 'apply_at' => $deviceFailModel->apply_at, + 'auth_at' => time(), + ]; + Device::create($item); + // 设备改为删除了 + $deviceFailModel->is_delete = 1; + $deviceFailModel->save(); + $trans->commit(); + $successCount++; + + } catch (Exception $exception) { + $trans->rollBack(); + $failCount++; + } } else { - $e->message = '授权失败'; + + $newDeviceModel = DeviceRepository::findOne(['device_id'=> null, 'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); + if (!$newDeviceModel) { + + $trans = Yii::$app->getDb()->beginTransaction(); + try { + Device::createWithMacSerialNo($batchId, $batchNo, $deviceFailModel->device_id, $deviceFailModel->apply_at, 1, DeviceStatus::HAS_AUTH); + $deviceFailModel->is_delete = 1; + $deviceFailModel->save(); + $trans->commit(); + $successCount++; + } catch (Exception $exception) { + $trans->rollBack(); + $failCount++; + } + + } else { + $trans = Yii::$app->getDb()->beginTransaction(); + try { + $newDeviceModel->device_id = $deviceFailModel->device_id; + $newDeviceModel->status = DeviceStatus::HAS_AUTH; + $newDeviceModel->apply_at = $deviceFailModel->apply_at; + $newDeviceModel->auth_at = time(); + $newDeviceModel->status = DeviceStatus::HAS_AUTH; + $newDeviceModel->save(); + + $deviceFailModel->is_delete = 1; + $deviceFailModel->save(); + $trans->commit(); + $successCount++; + } catch (Exception $exception) { + $trans->rollBack(); + $failCount++; + } + } + } - return $this->renderJson($e); } + + if ($successCount > 0) { + $e->message = '成功处理了'.$successCount.'个设备。'; + if ($failCount > 0) { + $e->message = $e->message .'另外有'.$failCount.'个处理失败。'; + } + if ($exitCount > 0) { + $e->message = '有'.$exitCount.'个生成序列号失败,请重试'; + } + $e->success = true; + } else { + $e->message = '处理失败。'; + if ($failCount > 0) { + $e->message = $e->message .'共有'.$failCount.'个处理失败'; + } + if ($notExitCount > 0) { + $e->message = '发现有'.$notExitCount.'个没有厂商批次的设备。'; + } + if ($deviceExitCount > 0) { + $e->message = '发现有'.$deviceExitCount.'个设备已经授权过了'; + } + //$exitCount + if ($exitCount > 0) { + $e->message = '有'.$exitCount.'个生成序列号失败,请重试'; + } + + } + + return $this->renderJson($e); } /** @@ -655,7 +919,100 @@ class DeviceController extends BaseController $e->success = false; $e->message = 'fail'; $delDevice = DeviceRepository::findOne(['id' => $id, 'is_delete' => 1]); + if (!$delDevice) { + $e->message = '未找到该设备'; + return $this->renderJson($e); + } + // 检测当前批次是否已经满了,能否恢复 + $batchModel = CreateBatchRepository::findOne(['id' => $delDevice->batch_id]); + if (empty($batchModel)) { + $e->message = '未找到厂商对应的批次无法恢复'; + return $this->renderJson($e); + } + $count = DeviceRepository::rowsCount(['batch_id' => $batchModel->id, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]); + if (($count *1) >= ($batchModel->num * 1)) { + $e->message = '该厂商的批次已经满了无法恢复该设备'; + return $this->renderJson($e); + } + // 检测当前设备ID是否存在表里面 + $exitDevice = DeviceRepository::findOne(['device_id' => $delDevice->device_id, 'is_delete' => 0]); + if ($exitDevice) { + $e->message = '该设备ID已经存在序列号表里面,无法恢复'; + return $this->renderJson($e); + } + + $delDevice->is_delete = 0; + $delDevice->auth_at = null; + $delDevice->status = DeviceStatus::NO_AUTH; + $saveResult = $delDevice->save(); + if ($saveResult) { + $e->success = true; + $e->message = '恢复成功'; + } else { + $e->message = '恢复失败'; + } + + return $this->renderJson($e); + } + + public function actionBatchRestoreDevice() + { + $req = Yii::$app->request; + $ids = $req->post('ids'); + $e = new stdClass(); + $e->success = false; + $e->message = 'fail'; + $ids = explode(',', $ids); + $delDevices = DeviceRepository::findAll(['id' => $ids, 'is_delete' => 1]); + if (!$delDevices) { + $e->message = '未找到设备'; + return $this->renderJson($e); + } + $fullCount = 0; + $exitCount = 0; + $restoreArr = []; + // 检测当前批次是否已经满了,能否恢复 + foreach ($delDevices as $k => $delDevice) { + $batchModel = CreateBatchRepository::findOne(['id' => $delDevice->batch_id]); + if (empty($batchModel)) { + continue; + } + $count = DeviceRepository::rowsCount(['batch_id' => $batchModel->id, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]); + if (($count *1) >= ($batchModel->num * 1)) { + $fullCount++; + } + // 检测当前设备ID是否存在表里面 + $exitDevice = DeviceRepository::findOne(['device_id' => $delDevice->device_id, 'is_delete' => 0]); + if ($exitDevice) { + $exitCount++; + } + if (!$exitDevice && ($count *1) < ($batchModel->num * 1)) { + $restoreArr[] = $delDevice->id; + } + } + $restoreArr = array_unique($restoreArr); + if (!$restoreArr) { + $e->message = '设备不满足恢复条件,无法恢复'; + return $this->renderJson($e); + } + $attr = [ + 'is_delete' => 0, + 'auth_at' => null, + 'status' => DeviceStatus::NO_AUTH + ]; + + $condition = [ + 'id' => $restoreArr + ]; + $saveResult = DeviceModel::updateAll($attr, $condition); + + if ($saveResult) { + $e->success = true; + $e->message = '成功恢复'.count($restoreArr).'台设备'; + } else { + $e->message = '恢复失败,系统错误'; + } return $this->renderJson($e); } diff --git a/app-ht/modules/device/views/device/auth-fail-index.php b/app-ht/modules/device/views/device/auth-fail-index.php index 5f16a53..36f938f 100644 --- a/app-ht/modules/device/views/device/auth-fail-index.php +++ b/app-ht/modules/device/views/device/auth-fail-index.php @@ -1,7 +1,7 @@ title = '授权失败管理'; $this->params['breadcrumbs'][] = '失败序列号管理'; @@ -61,11 +61,12 @@ $this->params['breadcrumbs'][] = $this->title; ID - 厂商 + 批次编码 + 厂商 项目 设备型号 生产日期 - 设备ID + 设备ID 申请时间 操作 @@ -75,11 +76,13 @@ $this->params['breadcrumbs'][] = $this->title; - + - + + + @@ -132,6 +135,7 @@ $this->params['breadcrumbs'][] = $this->title; \ No newline at end of file diff --git a/app-ht/modules/device/views/device/delete-index.php b/app-ht/modules/device/views/device/delete-index.php index 37d2a8c..4515de2 100644 --- a/app-ht/modules/device/views/device/delete-index.php +++ b/app-ht/modules/device/views/device/delete-index.php @@ -148,7 +148,7 @@ $this->params['breadcrumbs'][] = $this->title; - +
暂无记录
@@ -174,6 +174,7 @@ $this->params['breadcrumbs'][] = $this->title; var authURL = ""; var delURL = ""; var restoreURL = ""; + var batchRestoreURL = ""; var doEditURL = ""; $(function () { @@ -223,10 +224,50 @@ $this->params['breadcrumbs'][] = $this->title; }) - $('.btn_restore').click(function(){ + $('.btn_restore').click(function() { + var id = $(this).attr('aid'); + $.post(restoreURL, {id:id} ,function(res) { + if (res.success) { + alert(res.message); + var _body = window.parent; + var _iframe1=_body.document.getElementById('x-iframe'); + _iframe1.contentWindow.location.reload(true); + } else { + alert(res.message); + } + } , 'json') + }) + $('.check-all').click(function(e) { + var checkVal = $(this).prop('checked'); + $('.check-item').prop('checked', checkVal) }) + $('.btn_batch_auth').click(function(){ + var checkItems = $('.check-item:checked'); + if ( 0 == checkItems.length) { + alert('请选择要操作的行'); + return false; + } + var ids = []; + $.each(checkItems, function(i,n){ + var id = $(n).val(); + if ('' != id && 0 != id) { + ids.push(id); + } + }) + ids = ids.join(','); + $.post(batchRestoreURL,{ids:ids},function(res){ + if (res.success) { + alert(res.message); + var _body = window.parent; + var _iframe1=_body.document.getElementById('x-iframe'); + _iframe1.contentWindow.location.reload(true); + } else { + alert(res.message); + } + }, 'json') + }) }); \ No newline at end of file diff --git a/app-ht/modules/device/views/device/export.php b/app-ht/modules/device/views/device/export.php new file mode 100644 index 0000000..51188fb --- /dev/null +++ b/app-ht/modules/device/views/device/export.php @@ -0,0 +1,91 @@ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + $item) : ?> + + + + + + + + + + + + + + + + +
ID序列号厂商项目设备型号生产日期MAC地址设备ID申请时间授权时间状态
+ + +
+
+ + + + + + + + + + + + + + + + + +
+
+ + \ No newline at end of file diff --git a/app-ht/modules/device/views/device/index.php b/app-ht/modules/device/views/device/index.php index fab5664..1b16600 100644 --- a/app-ht/modules/device/views/device/index.php +++ b/app-ht/modules/device/views/device/index.php @@ -80,6 +80,7 @@ $this->params['breadcrumbs'][] = $this->title;
@@ -186,7 +187,9 @@ $this->params['breadcrumbs'][] = $this->title; \ No newline at end of file diff --git a/app-ht/modules/upgrade/controllers/UpgradeController.php b/app-ht/modules/upgrade/controllers/UpgradeController.php index b783d08..59cb5a4 100644 --- a/app-ht/modules/upgrade/controllers/UpgradeController.php +++ b/app-ht/modules/upgrade/controllers/UpgradeController.php @@ -159,10 +159,25 @@ class UpgradeController extends BaseController $request = Yii::$app->request; $id = $request->post("id"); // $e = new stdClass(); + $e->success = false; + $e->message = 'fail'; if (empty($id)) { - + $e->message = 'ID为空'; + return $this->renderJson($e); + } + $upgradeModel = UpgradeRepository::findOne(['id' => $id]); + if (empty($upgradeModel)) { + $e->message = '未找到版本记录'; return $this->renderJson($e); } + $upgradeModel->status = UpgradeStatus::STATUS_WAIT; + if ($upgradeModel->save()) { + $e->success = true; + } else { + $e->message = '取消失败'; + } + + return $this->renderJson($e); } @@ -566,6 +581,14 @@ class UpgradeController extends BaseController if (isset($manufacture["name"])) { $info["manufacture_name"] = $manufacture["name"]; } + $project = ProjectRepository::selectOne($info["project_id"]); + if (isset($project["name"])) { + $info["project_name"] = $project["name"]; + } + $model = ModelRepository::selectOne($info["model_id"]); + if (isset($model["name"])) { + $info["model_name"] = $model["name"]; + } } return $this->render('push-app', ["info" => $info]); } @@ -584,6 +607,14 @@ class UpgradeController extends BaseController if (isset($manufacture["name"])) { $info["manufacture_name"] = $manufacture["name"]; } + $project = ProjectRepository::selectOne($info["project_id"]); + if (isset($project["name"])) { + $info["project_name"] = $project["name"]; + } + $model = ModelRepository::selectOne($info["model_id"]); + if (isset($model["name"])) { + $info["model_name"] = $model["name"]; + } } return $this->render('push-ota', ["info" => $info]); } diff --git a/console/controllers/TestController.php b/console/controllers/TestController.php index 917816e..2dcc3c2 100644 --- a/console/controllers/TestController.php +++ b/console/controllers/TestController.php @@ -35,15 +35,15 @@ class TestController extends Controller public function actionDeviceAuth() { $url = 'http://kingb:8012/app-api/web/authDevice'; - $manufactureNo = '0002'; - $device_id = 'SZDEVICE000037'; - $projectNo = '002'; - $modelNo = '002'; - $productionNo = '0001'; - + //$url = 'http://47.107.95.101/app-api/web/authDevice'; + $manufactureNo = '0003'; + $device_id = 'HZ5DEVICE00007'; + $projectNo = '0003'; + $modelNo = '0003'; + $productionNo = '0003'; $timestamp = time(); $salt = 13456; - $sign = md5($manufactureNo. $projectNo. $modelNo . $productionNo . $timestamp . $salt); + $sign = md5($manufactureNo. $projectNo. $modelNo . $productionNo . $timestamp .$device_id. $salt); $params = [ 'manufacture' => $manufactureNo, 'device_id' => $device_id, @@ -54,7 +54,7 @@ class TestController extends Controller 'sign' => $sign, ]; $params = json_encode($params); - $params = '{"manufacture":"0001","device_id":"4705BF36C376","project":"0001","model":"0001","production":"0001","timestamp":"1500000000","sign":"f16b3f969714a400e155e04e0ea29938"}'; + //$params = '{"manufacture":"0002","device_id":"6705BF36C37F","project":"0002","model":"0003","production":"0002","timestamp":"1500000000","sign":"2b67361ba972020496f1a1b3df388499"}'; $postResult = Http::POST($url, $params); echo $postResult; diff --git a/domain/device/Device.php b/domain/device/Device.php index 495efc9..9e2da7d 100644 --- a/domain/device/Device.php +++ b/domain/device/Device.php @@ -2,9 +2,73 @@ namespace domain\device; +use Yii; +use common\helpers\Utils; +use domain\device\models\Device as DeviceModel; use stdClass; + class Device { + + + /** + * @param $item + * @return DeviceModel + */ + static function create($item) + { + $newDevice = new DeviceModel(); + $newDevice->serial_no = $item['serial_no']; + $newDevice->mac = $item['mac']; + $newDevice->device_id = $item['device_id']; + $newDevice->batch_id = $item['batch_id']; + $newDevice->status = $item['status']; + $newDevice->has_re_auth = $item['has_re_auth']; + $newDevice->apply_at = $item['apply_at']; + $newDevice->auth_at = $item['auth_at']; + $newDevice->save(); + + return $newDevice; + } + + /** + * @param $batchId + * @param $batchNo + * @param $deviceId + * @param $applyAt + * @param $hasReAuth + * @return DeviceModel + */ + static function createWithMacSerialNo($batchId, $batchNo, $deviceId, $applyAt, $hasReAuth, $status) + { + $exitDeviceModel = DeviceModel::find(); + $exitDeviceModel->where(['batch_id' => $batchId]); + $exitDeviceModel->orderBy('serial_no desc'); + $exitDevice = $exitDeviceModel->one(); + $serialNo = ''; + if ($exitDevice) { + $numNo = mb_substr($exitDevice->serial_no, -4); + $no1 = hexdec($numNo); + $no =($no1 * 1) + 1; + $newNo = sprintf('%04X', $no); + $serialNo = $batchNo.$newNo; + } + + $macAddress = Utils::macGenerate(); + $item = [ + 'serial_no' => $serialNo, + 'mac' => $macAddress, + 'device_id' => $deviceId, + 'batch_id' => $batchId, + 'status' => $status, + 'has_re_auth' => $hasReAuth, + 'apply_at' => $applyAt, + 'auth_at' => time(), + ]; + + return self::create($item); + } + /** * 序列号前缀,也是batchNo * @param $manufacture @@ -49,6 +113,7 @@ class Device $e->message = ''; $e->serial_no = ''; $e->mac = ''; + $e->status = 0; $tt = time(); $batchNo = self::getBatchNo($manufactureNo, $projectNo, $modelNo, $productionNo); $batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]); @@ -57,11 +122,34 @@ class Device return $e; } $batchId = $batchModel->id; - $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]); - if (($count *1) >= ($batchModel->num * 1)) { + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'batch_id' => $batchId, 'is_delete' => 0]); + if ($deviceModel) { + if (DeviceStatus::HAS_AUTH == $deviceModel->status) { + $e->mac = $deviceModel->mac; + $e->serial_no = $deviceModel->serial_no; + $e->message = '授权成功'; + $e->success = true; + $e->status = 5; + } elseif(DeviceStatus::NO_AUTH == $deviceModel->status) { + $deviceModel->status = DeviceStatus::HAS_AUTH; + $deviceModel->auth_at = time(); + $deviceModel->save(); + $e->mac = $deviceModel->mac; + $e->serial_no = $deviceModel->serial_no; + $e->message = '授权成功'; + $e->success = true; + $e->status = 5; + } else { + $e->message = '授权失败'; + } + + return $e; + } + $needRecord = DeviceRepository::checkNeedRecordFailRecord($batchId, $batchModel->num); + + if ($needRecord) { // 超过了限制数,记录到另外一个表里面 - // to do 记录到表里面 $failRecord = DeviceAuthFailRepository::findOne(['device_id' => $deviceId, 'is_delete' => 0]); if (!$failRecord) { $item = [ @@ -73,14 +161,32 @@ class Device ]; DeviceAuthFail::create($item); } + $e->message = '授权失败'; return $e; } - - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'batch_id' => $batchId, 'is_delete' => 0]); if (empty($deviceModel)) { - $newDeviceModel = DeviceRepository::findOne(['device_id'=> null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); + $needGen = DeviceRepository::checkNeedAutoGen($batchId, $batchModel->num); + if ($needGen) { + $genDeviceModel = Device::createWithMacSerialNo($batchId, $batchNo, $deviceId, $tt, 0, DeviceStatus::HAS_AUTH); + if ($genDeviceModel) { + $e->message = '授权成功'; + $e->success = true; + $e->serial_no = $genDeviceModel->serial_no; + $e->mac = $genDeviceModel->mac; + } else { + $e->message = '授权失败, 下次重试授权!'; + $e->success = false; + } + return $e; + } + // 找到未空白未绑定的设备序列号 + $newDeviceModel = DeviceRepository::findOne(['device_id' => null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); + if (empty($newDeviceModel)) { + $e->message = '授权失败,系统参数有误'; + return $e; + } $newDeviceModel->device_id = $deviceId; $newDeviceModel->status = DeviceStatus::HAS_AUTH; $newDeviceModel->apply_at = $tt ; @@ -93,26 +199,9 @@ class Device return $e; } - if (DeviceStatus::HAS_AUTH == $deviceModel->status) { - $e->message = '授权成功'; - $e->success = true; - $e->serial_no = $deviceModel->serial_no; - $e->mac = $deviceModel->mac; - } elseif (DeviceStatus::FAIL_AUTH == $deviceModel->status) { - $e->message = '授权失败'; - } elseif (DeviceStatus::NO_AUTH == $deviceModel->status) { - // 删除之后恢复的数据 - $deviceModel->status = DeviceStatus::HAS_AUTH; - $deviceModel->auth_at = $tt; - $deviceModel->save(); - $e->message = '授权成功'; - $e->success = true; - $e->serial_no = $deviceModel->serial_no; - $e->mac = $deviceModel->mac; - } else { - $e->message = '授权失败!'; - } return $e; } + + } \ No newline at end of file diff --git a/domain/device/DeviceAuthFailRepository.php b/domain/device/DeviceAuthFailRepository.php index 16c50c6..60fb82b 100644 --- a/domain/device/DeviceAuthFailRepository.php +++ b/domain/device/DeviceAuthFailRepository.php @@ -62,4 +62,17 @@ class DeviceAuthFailRepository return $all; } + + /** + * @param $condition + * @return array|\yii\db\ActiveRecord[] + */ + static function findAll($condition) + { + $deviceModel = DeviceAuthFailModel::find(); + $deviceModel->where($condition); + $list = $deviceModel->all(); + + return $list; + } } \ No newline at end of file diff --git a/domain/device/DeviceRepository.php b/domain/device/DeviceRepository.php index 2bf8a05..5c03100 100644 --- a/domain/device/DeviceRepository.php +++ b/domain/device/DeviceRepository.php @@ -2,11 +2,21 @@ namespace domain\device; +use common\helpers\Log as AppLog; use domain\device\models\CreateBatch as CreateBatchModel; use domain\device\models\Device as DeviceModel; +use domain\device\models\DeviceStats; class DeviceRepository { + const DEBUG = true; + private static function myLog($str) + { + if (false == self::DEBUG) { + return ''; + } + AppLog::DEBUG($str); + } /** * @param $where * @return array|\yii\db\ActiveRecord[] @@ -85,6 +95,62 @@ class DeviceRepository $deviceModel = DeviceModel::find(); $deviceModel->where($condition); $count = $deviceModel->count(); + self::myLog('SQL:'.$deviceModel->createCommand()->rawSql); return $count; } + + /** + * 判断是否要登记到授权失败表 + * @param $batchId + * @param $batchNum + * @return bool + */ + static function checkNeedRecordFailRecord($batchId, $batchNum) + { + // 第1个情况,全部授权满了 + $authCount = self::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]); + + if (($authCount *1) >= ($batchNum * 1)) { + return true; + } + + // 第2个情况,授权数目 + 删除后恢复数满了 + $where = ['and', + ['=', 'batch_id', $batchId], + ['is not', 'device_id', null], + ['in', 'status', [DeviceStatus::NO_AUTH, DeviceStatus::FAIL_AUTH]], + ['=', 'is_delete', 0], + ]; + $restoreCount = self::rowsCount($where); + if (($restoreCount *1 + $authCount) >= ($batchNum * 1)) { + return true; + } + + return false; + } + + /** + * 判断是否要自动产生序列号 + * @param $batchId + * @param $batchNum + * @return bool + */ + static function checkNeedAutoGen($batchId, $batchNum) + { + $where = [ + 'and', + ['=', 'batch_id' ,$batchId], + ['=', 'is_delete', 0], + ['=', 'status', DeviceStatus::NO_AUTH], + ['is', 'device_id', null] + ]; + + $authCount = self::rowsCount($where); + self::myLog('checkNeedAutoGen data:'.$authCount.' __'. $batchNum); + if (empty($authCount)) { + return true; + } + + return false; + } } \ No newline at end of file -- libgit2 0.21.0