237 lines
7.3 KiB
PHP
237 lines
7.3 KiB
PHP
<?php
|
|
/**
|
|
* 2012-2025 D1m Group
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Open Software License (OSL 3.0) that is available
|
|
* through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
|
|
* If you are unable to obtain it through the world-wide-web, please send an email
|
|
* to sales@d1m.cn so we can send you a copy immediately.
|
|
*
|
|
* @copyright 2012-2025 D1m Group
|
|
* @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
|
*/
|
|
|
|
namespace Stemcell\Datacollection\Model;
|
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList;
|
|
use Magento\Framework\Filesystem;
|
|
use Magento\Framework\Filesystem\Io\File;
|
|
use Magento\Framework\Serialize\Serializer\Json;
|
|
use Stemcell\Commerce\Model\WebapiResponse;
|
|
use Stemcell\Datacollection\Api\DataCollectionInterface;
|
|
use Stemcell\Datacollection\Model\Type\EBook;
|
|
use Stemcell\Datacollection\Model\Type\Inquiry;
|
|
use Stemcell\Datacollection\Model\Type\Lecture;
|
|
use Stemcell\Datacollection\Model\Type\Livedemo;
|
|
use Stemcell\Datacollection\Model\Type\LiveDemoForm;
|
|
|
|
/**
|
|
* Data Collection Model
|
|
*/
|
|
class DataCollection implements DataCollectionInterface
|
|
{
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct(
|
|
private Filesystem $filesystem,
|
|
private File $ioFile,
|
|
private Inquiry $inquiryModel,
|
|
private Lecture $lectureModel,
|
|
private Livedemo $livedemoModel,
|
|
private LiveDemoForm $liveDemoForm,
|
|
private EBook $eBookModel,
|
|
private Json $jsonSerializer,
|
|
private \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
|
|
private \Magento\Framework\App\CacheInterface $cache,
|
|
private WebapiResponse $webapiResponse
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function submitData($data, $formType = 'default')
|
|
{
|
|
try {
|
|
$this->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);
|
|
}
|
|
|
|
|
|
}
|