ipcheck(); // Handle different form types if ($formType === 'inquiry') { $result = $this->handleInquiryForm($data); } if ($formType === 'lecture') { $result = $this->handleLectureForm($data); } if ($formType === 'ebook'){ $result = $this->handleEBookForm($data, $formType); } if ($formType === 'livedemo') { $result = $this->handleLivedemoForm($data, $formType); } // Default handling - save to log file if ($formType === 'default') { $result = $this->handleDefaultData($data); } } catch (\Exception $e) { $result = ['error' => "Error collecting data: " . $e->getMessage()]; } return $this->webapiResponse::json($result); } private function handleEbookForm(string $data, string $formType){ try { // Decode JSON data $decodedData = $this->jsonSerializer->unserialize($data); // Validate data $this->eBookModel->setFormCode($formType)->validateData($decodedData); // Process data return $this->eBookModel->processData($decodedData); //return $result['message'] . ' (Record ID: ' . $result['record_id'] . ')'; } catch (\Exception $e) { return ['error' => "Error processing inquiry form: " . $e->getMessage()]; } } /** * Handle livedemo form data */ private function handleLivedemoForm(string $data, string $formType) { try { $decodedData = $this->jsonSerializer->unserialize($data); $this->liveDemoForm->setFormCode($formType)->validateData($decodedData); return $this->liveDemoForm->processData($decodedData); //return $result['message'] . ' (Record ID: ' . $result['record_id'] . ')'; } catch (\Exception $e) { return ['error' => "Error processing livedemo form: " . $e->getMessage()]; } } /** * Handle inquiry form data * * @param string $data * @return array */ private function handleInquiryForm($data) { try { // Decode JSON data $decodedData = $this->jsonSerializer->unserialize($data); // Validate data $this->inquiryModel->validateData($decodedData); // Process data return $this->inquiryModel->processData($decodedData); //return $result['message'] . ' (Record ID: ' . $result['record_id'] . ')'; } catch (\Exception $e) { return ['error' => "Error processing inquiry form: " . $e->getMessage()]; } } /** * Handle lecture form data * * @param string $data * @return array */ private function handleLectureForm($data) { try { // Decode JSON data $decodedData = $this->jsonSerializer->unserialize($data); // Validate data $this->lectureModel->validateData($decodedData); // Process data return $this->lectureModel->processData($decodedData); //return $result['message'] . ' (Record ID: ' . $result['record_id'] . ')'; } catch (\Exception $e) { return ['error' => "Error processing lecture form: " . $e->getMessage()]; } } /** * Handle default data (save to log file) * * @param string $data * @return array */ private function handleDefaultData($data) { $filename = $this->log($data); return ['success' => "Data collected successfully. File: " . $filename]; } private function log($data) { // Get var directory path $varDir = $this->filesystem->getDirectoryWrite(DirectoryList::LOG); $logPath = $varDir->getAbsolutePath('datacollection'); // Create directory if it doesn't exist if (!is_dir($logPath)) { $this->ioFile->mkdir($logPath, 0755); } // Create log file with timestamp $filename = 'data_' . date('Y-m-d') . '.log'; $filePath = $logPath . '/' . $filename; // Prepare log content $logContent = "Timestamp: " . date('Y-m-d H:i:s') . "\n"; $logContent .= "Data: " . $data . "\n"; $logContent .= "----------------------------------------\n"; // Write to file $this->ioFile->write($filePath, $logContent); return $filename; } private function ipcheck() { $ip = $this->remoteAddress->getRemoteAddress(); $cacheKey = 'datacollection_rate_limit_' . $ip; $count = $this->cache->load($cacheKey) ?: 0; if ($count >= 5) { $this->log('IP ' . $ip . ' has made 5 requests in the last 5 minutes.'); throw new \Magento\Framework\Exception\LocalizedException(__('Too many requests. Please try again later.')); } // 增加计数并设置5分钟过期 $this->cache->save((string)($count + 1), $cacheKey, [], 300); } /** * {@inheritdoc} */ public function getMyInquiryRecords($pageSize = 10, $currentPage = 1) { try { // Delegate to Inquiry model $result = $this->inquiryModel->getMyInquiryRecords($pageSize, $currentPage); } catch (\Exception $e) { $result = [ 'error' => $e->getMessage()]; } return $this->webapiResponse::json($result); } }