diff --git a/code/app/common.php b/code/app/common.php
index 36731f2..3c70743 100644
--- a/code/app/common.php
+++ b/code/app/common.php
@@ -1,4 +1,5 @@
getFomartSubject('NV_SHENG_URL');
-
- // exit;
-
$TemplatesModel = TemplatesModel::getTemplateOnCacheByTId($DomainModel->t_id);
if (empty($DomainModel)) {
diff --git a/code/app/model/GanRaoMaModel.php b/code/app/model/GanRaoMaModel.php
index 32db24e..940c757 100644
--- a/code/app/model/GanRaoMaModel.php
+++ b/code/app/model/GanRaoMaModel.php
@@ -22,6 +22,60 @@ class GanRaoMaModel extends MongoModel
*/
protected $strCollection = 'gan_rao_ma';
+
+
+ /**
+ * Undocumented variable
+ *
+ * @var array
+ */
+ static public $arrSelfClosing = [
+ '',
+ '',
+ '',
+ '',
+ '',
+ '
',
+ '',
+ '%s',
+ '%s
',
+ '%s
',
+ '%s',
+ '%s',
+ ];
+
+ const LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+
+ const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
+
+ public static function generateRandomStrings(int $count): array
+ {
+ static $lettersLength = null;
+ static $charsLength = null;
+
+ if ($lettersLength === null) {
+ $lettersLength = strlen(self::LETTERS) - 1;
+ $charsLength = strlen(self::CHARS) - 1;
+ }
+
+
+ $result = array_fill(0, $count, '');
+
+ array_walk($result, function (&$value) use ($lettersLength, $charsLength) {
+ $length = random_int(5, 10);
+ $value = self::LETTERS[random_int(0, $lettersLength)];
+ $i = 1;
+ while ($i < $length) {
+ $value .= self::CHARS[random_int(0, $charsLength)];
+ $i++;
+ }
+ });
+
+ return $result;
+ }
+
+
+
/**
* Undocumented function
*
@@ -33,9 +87,33 @@ class GanRaoMaModel extends MongoModel
* @param int $intEndNumber 生成标签,随机结束位置 随机生成(10-20)个标签 20
* @return string
*/
- static public function getGrmCode($strDomain, $strPageCode, $intLine, $strDifference,$intStartNumber,$intEndNumber): string
+ static public function getGrmCode($strDomain, $strPageCode, $intLine, $strDifference, $intStartNumber, $intEndNumber): string
{
- // TODO 干扰码功能实现
- return (string)mt_rand(1, 100);
+ $arrFilter = [
+ 'grm_domain' => (string) $strDomain,
+ 'grm_page_code' => (string) $strPageCode,
+ 'grm_page_line' => (string)$intLine,
+ 'grm_page_diff' => (string) $strDifference,
+ ];
+ $arrGrm = self::getInstance()->getOne($arrFilter);
+
+ if (empty($arrGrm)) {
+ $intNum = mt_rand($intStartNumber, $intEndNumber);
+ $arrGrmCode = self::$arrSelfClosing;
+ shuffle($arrGrmCode);
+ $arrGrmCode = array_slice($arrGrmCode, 0, $intNum);
+ $strGrmCode = implode('', $arrGrmCode);
+ $intRandNum = substr_count($strGrmCode, '%s');
+ $arrRandStr = self::generateRandomStrings($intRandNum);
+ $strGrmCode = sprintf($strGrmCode, ...$arrRandStr);
+
+ $arrData = $arrFilter;
+ $arrData['grm_code'] = $strGrmCode;
+ self::getInstance()->insert($arrData);
+ } else {
+ $strGrmCode = $arrGrm['grm_code'];
+ }
+
+ return $strGrmCode;
}
}
diff --git a/code/app/model/MongoModel.php b/code/app/model/MongoModel.php
index cd3980d..a5c0f17 100644
--- a/code/app/model/MongoModel.php
+++ b/code/app/model/MongoModel.php
@@ -14,6 +14,22 @@ use think\facade\Cache;
*/
abstract class MongoModel
{
+
+ /**
+ * Undocumented variable
+ *
+ * @var GanRaoMaModel
+ */
+ static public $obj;
+
+ static public function getInstance()
+ {
+ if (static::$obj == NULL) {
+ static::$obj = new static;
+ }
+ return static::$obj;
+ }
+
/**
* collection name
*
@@ -115,4 +131,16 @@ abstract class MongoModel
Cache::set($strKey, $mixedData, $intLifeTime);
}
}
+
+ /**
+ * Undocumented function
+ *
+ * @param array $arrData
+ * @return mixed
+ */
+ public function insert(array $arrData, array $arrOptions = []): mixed
+ {
+ $Result = $this->getCol()->insertOne($arrData);
+ return $Result->getInsertedId();
+ }
}