diff --git a/app-api/config/url-rules.php b/app-api/config/url-rules.php index 15e4259..adc98ed 100644 --- a/app-api/config/url-rules.php +++ b/app-api/config/url-rules.php @@ -5,11 +5,13 @@ return [ 'POST authDevice' => 'auth-device/index', + 'POST authDeviceT' => 'auth-device/indext', 'POST checkOtaVersion' => 'upgrade/check-version', 'POST reportOtaUpgradeEvent' => 'upgrade/report-upgrade-event', 'POST checkAppVersion' => 'upgrade/check-app-version', 'POST reportAppUpgradeEvent' => 'upgrade/report-app-upgrade-event', 'POST reportDeviceVersion' => 'upgrade/report-device-version', + 'POST CryptTxt' => 'auth-device/crypt-txt', 'GET errorPage' => 'site/error-page-info', 'GET minaQuery' => 'site/mina-query', ]; \ No newline at end of file diff --git a/app-api/controllers/AuthDeviceController.php b/app-api/controllers/AuthDeviceController.php index 95e3d02..49e0a99 100644 --- a/app-api/controllers/AuthDeviceController.php +++ b/app-api/controllers/AuthDeviceController.php @@ -2,14 +2,13 @@ namespace app\api\controllers; - use Yii; - +use common\exts\RSACrypt; +use common\exts\Aes; use common\helpers\Utils; use common\helpers\Log as AppLog; -use domain\device\DeviceRepository; use domain\device\Device; -use domain\device\DeviceStatus; + use stdClass; use function date; @@ -37,66 +36,135 @@ class AuthDeviceController extends BaseController { AppLog::DEBUG($str); } + /** * 设备授权接口 * @return stdClass */ - public function actionIndex() - { - $e = new stdClass(); - $e->status = 1; - $e->message = 'message'; - $e->serial_no = '';; - $e->mac = ''; - - $getPostData = file_get_contents('php://input', 'r'); - self::myLog('actionIndex postData:'.$getPostData); - if (!$getPostData) { - $e->status = 1; - $e->message = '传入的数据为空'; - return $e; - } - $getPostData = json_decode($getPostData, true); - $manufactureNo = isset($getPostData['manufacture'])?$getPostData['manufacture']:''; - $deviceId = isset($getPostData['device_id'])?$getPostData['device_id']:''; - $projectNo = isset($getPostData['project'])?$getPostData['project']:''; - $modelNo = isset($getPostData['model'])?$getPostData['model']:''; - $productionNo = isset($getPostData['production'])?$getPostData['production']:''; - $timestamp = isset($getPostData['timestamp'])?$getPostData['timestamp']:''; - $sign = isset($getPostData['sign'])?$getPostData['sign']:''; - if (empty($deviceId) || empty($manufactureNo) || empty($projectNo) || empty($modelNo) || empty($productionNo)) { - $e->message = '传入的数据部分为空'; - return $e; - } - $pattern = "/^[a-zA-Z0-9]+$/"; - $dexPattern = "/^[0-9a-fA-F]+$/"; - if (!preg_match($pattern, $deviceId) || !preg_match($dexPattern, $manufactureNo) || !preg_match($dexPattern, $modelNo) || !preg_match($dexPattern, $productionNo)) { - $e->status = 9; - $e->message = '传入的数据字段格式不对'; - return $e; - } - if (isset(Yii::$app->params['secretKey']) && !empty(Yii::$app->params['secretKey'])) { - $salt = Yii::$app->params['secretKey']; - } else { - $salt = isset(Yii::$app->params['secretKey'])? Yii::$app->params['secretKey']: self::$SIGN_SALT; - } - - $makeSign = md5($manufactureNo . $projectNo. $modelNo . $productionNo . $timestamp . $deviceId. $salt); - if ($sign != $makeSign || empty($sign)) { - $e->status = 2; - $e->message = '签名出错'; - return $e; - } - - $authResult = Device::authDevice($deviceId, $manufactureNo, $projectNo, $modelNo, $productionNo); - - $e->status = $authResult->status; - $e->message = $authResult->message; - if ($authResult->success) { - $e->mac = $authResult->mac; - $e->serial_no = $authResult->serial_no; - } - - return $e; - } + public function actionIndex() + { + $getPostData = file_get_contents('php://input', 'r'); + self::myLog('actionIndex postData:'.$getPostData); + + return $this->authDevice($getPostData); + } + + /** + * @param $getPostData + * @return stdClass + */ + private function authDevice($getPostData) + { + $e = new stdClass(); + $e->status = 1; + $e->message = 'message'; + $e->serial_no = '';; + $e->mac = ''; + + $getPostData = json_decode($getPostData, true); + if (empty($getPostData)) { + $e->status = 1; + $e->message = '传入的数据为空'; + return $e; + } + $manufactureNo = isset($getPostData['manufacture'])?$getPostData['manufacture']:''; + $deviceId = isset($getPostData['device_id'])?$getPostData['device_id']:''; + $projectNo = isset($getPostData['project'])?$getPostData['project']:''; + $modelNo = isset($getPostData['model'])?$getPostData['model']:''; + $productionNo = isset($getPostData['production'])?$getPostData['production']:''; + $timestamp = isset($getPostData['timestamp'])?$getPostData['timestamp']:''; + $sign = isset($getPostData['sign'])?$getPostData['sign']:''; + if (empty($deviceId) || empty($manufactureNo) || empty($projectNo) || empty($modelNo) || empty($productionNo)) { + $e->message = '传入的数据部分为空'; + return $e; + } + $pattern = "/^[a-zA-Z0-9]+$/"; + $dexPattern = "/^[0-9a-fA-F]+$/"; + if (!preg_match($pattern, $deviceId) || !preg_match($dexPattern, $manufactureNo) || !preg_match($dexPattern, $modelNo) || !preg_match($dexPattern, $productionNo)) { + $e->status = 9; + $e->message = '传入的数据字段格式不对'; + return $e; + } + if (isset(Yii::$app->params['secretKey']) && !empty(Yii::$app->params['secretKey'])) { + $salt = Yii::$app->params['secretKey']; + } else { + $salt = isset(Yii::$app->params['secretKey'])? Yii::$app->params['secretKey']: self::$SIGN_SALT; + } + + $makeSign = md5($manufactureNo . $projectNo. $modelNo . $productionNo . $timestamp . $deviceId. $salt); + if ($sign != $makeSign || empty($sign)) { + $e->status = 2; + $e->message = '签名出错'; + return $e; + } + + $authResult = Device::authDevice($deviceId, $manufactureNo, $projectNo, $modelNo, $productionNo); + + $e->status = $authResult->status; + $e->message = $authResult->message; + if ($authResult->success) { + $e->mac = $authResult->mac; + $e->serial_no = $authResult->serial_no; + } + + return $e; + } + + /** + * + */ + public function actionCryptTxt() + { + $e = new stdClass(); + + $getPostDataTxt = file_get_contents('php://input', 'r'); + $getPostData = json_decode($getPostDataTxt, true); + $randomKey = $getPostData['randomKey']; + $content = $getPostData['content']; + $rsa = new RSACrypt(); + $randKey = $rsa->decrypt($randomKey); + $aes = new Aes($randKey); + $contentStr = $aes->decrypt($content); + + $returnContent = [ + "mac" => Utils::macGenerate(), + "serial_no" => Utils::rand(16), + 'random_key' =>$randKey, + ]; + + $e->content = $aes->encrypt(json_encode($returnContent)); + + return $e; + } + + /** + * @return stdClass + */ + public function actionIndext() + { + $e = new stdClass(); + $e->content = ''; + + $getPostData = file_get_contents('php://input', 'r'); + self::myLog('actionIndext postData:'.$getPostData); + $getPostData = json_decode($getPostData, true); + $randomKey = $getPostData['randomKey']; + $content = $getPostData['content']; + $rsa = new RSACrypt(); + $randKey = $rsa->decrypt($randomKey); + if (16 != strlen($randKey)) { + // 检查randKey,当前只是做长度判断 + $randKey = null; + } + $aes = new Aes($randKey); + $contentStr = $aes->decrypt($content); + $authResult = $this->authDevice($contentStr); + + $returnStr = json_encode($authResult, JSON_UNESCAPED_UNICODE); + + $e->content = $aes->encrypt($returnStr); + + return $e; + } + } \ No newline at end of file diff --git a/app-api/helpers/Aes.php b/app-api/helpers/Aes.php deleted file mode 100644 index 557aeb8..0000000 --- a/app-api/helpers/Aes.php +++ /dev/null @@ -1,73 +0,0 @@ -secret_key = isset($key) ? $key : 'king_board_key_01'; - - $this->method = $method; - - $this->iv = $iv; - - $this->options = $options; - } - - /** - * 加密方法,对数据进行加密,返回加密后的数据 - * - * @param string $data 要加密的数据 - * - * @return string - * - */ - public function encrypt($data) - { - return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); - } - - /** - * 解密方法,对数据进行解密,返回解密后的数据 - * - * @param string $data 要解密的数据 - * - * @return string - * - */ - public function decrypt($data) - { - return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); - } -} \ No newline at end of file diff --git a/app-ht/modules/device/controllers/DeviceController.php b/app-ht/modules/device/controllers/DeviceController.php index 9c0ca66..5acc9b2 100644 --- a/app-ht/modules/device/controllers/DeviceController.php +++ b/app-ht/modules/device/controllers/DeviceController.php @@ -32,7 +32,7 @@ class DeviceController extends BaseController return $this->render('index', $params); } - private function dataList($type) + private function dataList($type, $delete = 0) { $request = Yii::$app->request; $serialNo = $request->get('serial_no'); @@ -48,13 +48,15 @@ class DeviceController extends BaseController $endApplyAt = $request->get('end_apply_at'); $startAuthAt = $request->get('start_auth_at'); $endAuthAt = $request->get('end_auth_at'); + $startCreatedAt = $request->get('start_created_at'); + $endCreatedAt = $request->get('end_created_at'); $has_re_auth = $request->get('has_re_auth'); $page = $request->get('page'); $where = [ 'and', - ['=','a.is_delete', 0] + ['=','a.is_delete', $delete] ]; if (!empty($serialNo)) { $where[] = ['like', 'a.serial_no', $serialNo]; @@ -87,11 +89,17 @@ class DeviceController extends BaseController $where[] = ['>=', 'a.auth_at', strtotime($startAuthAt)]; } if ($endAuthAt) { - $where[] = ['>=', 'a.auth_at', strtotime($endAuthAt) + 86400]; + $where[] = ['<=', 'a.auth_at', strtotime($endAuthAt) + 86400]; } if ($has_re_auth) { $where[] = ['=', 'a.has_re_auth', $has_re_auth]; } + if ($startCreatedAt) { + $where[] = ['>=', 'a.created_at', strtotime($startCreatedAt)]; + } + if ($endCreatedAt) { + $where[] = ['<=', 'a.created_at', strtotime($endCreatedAt) + 86400]; + } if (isset($_GET['status']) && -1 != $status) { $where[] = ['=', 'a.status', $status]; } else { @@ -129,6 +137,8 @@ class DeviceController extends BaseController 'start_auth_at' => $startAuthAt, 'end_auth_at' => $endAuthAt, 'has_re_auth' => $has_re_auth, + 'start_created_at' => $startCreatedAt, + 'end_created_at' => $endCreatedAt, 'status' => $status ]; @@ -140,92 +150,7 @@ class DeviceController extends BaseController */ public function actionDeleteIndex() { - $request = Yii::$app->request; - $serialNo = $request->get('serial_no'); - $mac = $request->get('mac'); - $project = $request->get('project'); - $model = $request->get('model'); - $production = $request->get('production'); - $manufacture = $request->get('manufacture'); - $deviceId = $request->get('device_id'); - $status = $request->get('status'); - - $startApplyAt = $request->get('start_apply_at'); - $endApplyAt = $request->get('end_apply_at'); - $startAuthAt = $request->get('start_auth_at'); - $endAuthAt = $request->get('end_auth_at'); - $page = $request->get('page'); - $where = [ - 'and', - ['=','a.is_delete', 1] - ]; - if (!empty($serialNo)) { - $where[] = ['like', 'a.serial_no', $serialNo]; - } - if (!empty($project)) { - $where[] = ['like', 'p.name', $project]; - } - if (!empty($model)) { - $where[] = ['like', 'mo.name', $model]; - } - if (!empty($production)) { - $where[] = ['like', 'pd.name', $production]; - } - if (!empty($mac)) { - $where[] = ['like', 'a.mac', $mac]; - } - if (!empty($manufacture)) { - $where[] = ['like', 'm.name', $manufacture]; - } - if (!empty($deviceId)) { - $where[] = ['like', 'a.device_id', $deviceId]; - } - - if ($startApplyAt) { - $where[] = ['>=', 'a.apply_at', strtotime($startApplyAt)]; - } - if ($endApplyAt) { - $where[] = ['<=', 'a.apply_at', strtotime($endApplyAt) + 86400]; - } - if ($startAuthAt) { - $where[] = ['>=', 'a.auth_at', strtotime($startAuthAt)]; - } - if ($endAuthAt) { - $where[] = ['<=', 'a.auth_at', strtotime($endAuthAt) + 86400]; - } - if (isset($_GET['status']) && -1 != $status) { - $where[] = ['=', 'a.status', $status]; - } else { - $status = -1; - } - - 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]); - $statusList = DeviceStatus::statusLabels(); // - - $params['statusList'] = $statusList; - $params['deviceList'] = $deviceData; - $params['pages'] = $pages; - $params["gets"] = [ - 'serial_no' => $serialNo, - 'mac' => $mac, - 'project' => $project, - 'model' => $model, - 'device_id' => $deviceId, - 'production' => $production, - 'manufacture' => $manufacture, - 'start_apply_at' => $startApplyAt, - 'end_apply_at' => $endApplyAt, - 'start_auth_at' => $startAuthAt, - 'end_auth_at' => $endAuthAt, - 'status' => $status - ]; + $params = $this->dataList(1, 1); return $this->render('delete-index', $params); } @@ -245,16 +170,10 @@ class DeviceController extends BaseController public function actionDoCreateDevice() { $req = Yii::$app->request; - $manufactureId = $req->post('manufactureId'); - $projectId = $req->post('projectId'); - $modelId = $req->post('modelId'); - $productionId = $req->post('productionId'); - - $manufactureNo = $req->post('manufactureNo'); - $projectNo = $req->post('projectNo'); - $modelNo = $req->post('modelNo'); - $productionNo = $req->post('productionNo'); - + $manufacture = $req->post('manufacture'); + $project = $req->post('project'); + $model = $req->post('model'); + $production = $req->post('production'); $num = $req->post('num'); $e = new stdClass(); $e->success = false; @@ -271,6 +190,34 @@ class DeviceController extends BaseController $e->message = '数量不能超过1万'; return $this->renderJson($e); } + if (empty($manufacture) || empty($project) || empty($model) || empty($production)) { + $e->message = '厂商,项目,型号,生产日期必填'; + return $this->renderJson($e); + } + $manufactureArr = explode('_', $manufacture); + $projectArr = explode('_', $project); + $modelArr = explode('_', $model); + $productionArr = explode('_', $production); + + $manufactureId = isset($manufactureArr[0])? $manufactureArr[0] :0; + $manufactureNo = isset($manufactureArr[1])? $manufactureArr[1] :''; + + $projectId = isset($projectArr[0])? $projectArr[0] :0; + $projectNo = isset($projectArr[1])? $projectArr[1] :''; + + $modelId = isset($modelArr[0])? $modelArr[0] :0; + $modelNo = isset($modelArr[1])? $modelArr[1] :''; + + $productionId = isset($productionArr[0])? $productionArr[0] :0; + $productionNo = isset($productionArr[1])? $productionArr[1] :0; + if (empty($manufactureId) || empty($projectId) || empty($modelId) || empty($productionId)) { + $e->message = '找不到对应的厂商,项目,型号,生产日期'; + return $this->renderJson($e); + } + if (empty($manufactureNo) || empty($projectNo) || empty($modelNo) || empty($productionNo)) { + $e->message = '找不到对应的厂商,项目,型号,生产日期!'; + return $this->renderJson($e); + } $batchNo = strtoupper(Device::getBatchNo($manufactureNo, $projectNo, $modelNo, $productionNo)); $batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]); @@ -725,7 +672,7 @@ class DeviceController extends BaseController if ($needGen) { $trans = Yii::$app->getDb()->beginTransaction(); try { - $genDeviceModel = Device::createWithMacSerialNo($batchId, $batchNo, $deviceId, $tt, 1, DeviceStatus::HAS_AUTH); + Device::createWithMacSerialNo($batchId, $batchNo, $deviceId, $tt, 1, DeviceStatus::NO_AUTH); $deviceFailModel->is_delete = 1; $deviceFailModel->save(); $trans->commit(); @@ -748,7 +695,7 @@ class DeviceController extends BaseController $trans = Yii::$app->getDb()->beginTransaction(); try { $newDeviceModel->device_id = $deviceId; - $newDeviceModel->status = DeviceStatus::HAS_AUTH; + $newDeviceModel->status = DeviceStatus::NO_AUTH; $newDeviceModel->apply_at = $tt ; $newDeviceModel->auth_at = $tt; $newDeviceModel->save(); diff --git a/app-ht/modules/device/views/device/createDevice.php b/app-ht/modules/device/views/device/createDevice.php index 0c3fe46..7be6ef4 100644 --- a/app-ht/modules/device/views/device/createDevice.php +++ b/app-ht/modules/device/views/device/createDevice.php @@ -201,28 +201,28 @@ $(function() { alert('请选择厂商'); return false; } - manufacture = manufacture.split('_'); + //manufacture = manufacture.split('_'); var project = $('#project').val(); if ('' == project) { alert('请选择项目'); return false; } - project = project.split('_'); + //project = project.split('_'); var model = $('#model').val(); if ('' == model) { alert('请选择型号'); return false; } - model = model.split('_'); + //model = model.split('_'); var production = $('#production').val(); if ('' == production) { alert('请选生产日期'); return false; } - production = production.split('_'); + //production = production.split('_'); var num = $('#num').val(); var par = /^[0-9]+$/; if (par.test(num) && (num > 0)) { @@ -233,17 +233,10 @@ $(function() { } var params = { - manufactureId: manufacture[0], - manufactureNo: manufacture[1], - - projectId: project[0], - projectNo: project[1], - - modelId: model[0], - modelNo: model[1], - - productionId: production[0], - productionNo: production[1], + manufacture: manufacture, + project: project, + model: model, + production: production, num: num } $.post(saveUrl, params, function(res) { @@ -271,14 +264,13 @@ $(function() { return false; } - return false; if (append_num*1 > 3000) { alert('追加数量不要超过3000'); return false; } $.post(appendSerialUrl,{batch_id:batch_id, append_num:append_num}, function(ajaxRes){ if (ajaxRes.success) { - alert('成功追加'); + alert('成功追加'+append_num+'个序列号'); window.location.href = '=Url::toRoute('/datas/device/index')?>' } else { alert(ajaxRes.message); diff --git a/app-ht/modules/device/views/device/delete-index.php b/app-ht/modules/device/views/device/delete-index.php index 4515de2..9f986a5 100644 --- a/app-ht/modules/device/views/device/delete-index.php +++ b/app-ht/modules/device/views/device/delete-index.php @@ -48,13 +48,6 @@ $this->params['breadcrumbs'][] = $this->title;