From 38a48221e8cbab5fb0fae6023b659862234f5116 Mon Sep 17 00:00:00 2001 From: Terry <2358269014@qq.com> Date: Thu, 12 Sep 2019 17:56:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A7=E5=93=81url=5Fkey=E9=83=A8=E5=88=86bu?= =?UTF-8?q?g=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mysqldb/m190912_052057_fecshop_tables.php | 56 +++++++++++++++++ services/Url.php | 60 +++++++++++++++++-- services/product/ProductMysqldb.php | 17 ++++-- 3 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 migrations/mysqldb/m190912_052057_fecshop_tables.php diff --git a/migrations/mysqldb/m190912_052057_fecshop_tables.php b/migrations/mysqldb/m190912_052057_fecshop_tables.php new file mode 100644 index 00000000..c14abb51 --- /dev/null +++ b/migrations/mysqldb/m190912_052057_fecshop_tables.php @@ -0,0 +1,56 @@ +execute($sql); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + echo "m190912_052057_fecshop_tables cannot be reverted.\n"; + + return false; + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m190912_052057_fecshop_tables cannot be reverted.\n"; + + return false; + } + */ +} diff --git a/services/Url.php b/services/Url.php index 0a944dd5..f3e7c58e 100644 --- a/services/Url.php +++ b/services/Url.php @@ -24,7 +24,7 @@ use Yii; */ class Url extends Service { - public $randomCount = 8; + public $randomCount = 9; public $showScriptName; @@ -62,14 +62,24 @@ class Url extends Service $str = trim($str); $originUrl = $originUrl ? '/'.trim($originUrl, '/') : ''; $originUrlKey = $originUrlKey ? '/'.trim($originUrlKey, '/') : ''; - if ($originUrlKey) { + if ($originUrlKey && $originUrl) { /** * if originUrlKey and originUrl is exist in url rewrite collectons. */ $model = $this->find(); + // 如果已经存在,那么直接返回 + $data_one = $model->where([ + 'custom_url_key' => $originUrlKey, + 'origin_url' => $originUrl, + ])->one(); + if (isset($data_one['custom_url_key'])) { + + return $originUrlKey; + } + /* $data_one = $model->where([ 'custom_url_key' => $originUrlKey, - //'origin_url' => $originUrl, + 'origin_url' => $originUrl, ])->one(); if (isset($data_one['custom_url_key'])) { // 只要进行了查询,就要更新一下rewrite url表的updated_at. @@ -79,6 +89,7 @@ class Url extends Service return $originUrlKey; } + */ } if ($originUrlKey) { $urlKey = $this->generateUrlByName($originUrlKey); @@ -348,6 +359,23 @@ class Url extends Service return $str; } + + protected function isRandomStr($randomStr) + { + $f = substr($randomStr, 0, 1); + if ($f !== '-') { + return false; + } + $s = substr($randomStr, 1); + if (strlen($s) != $this->randomCount) { + return false; + } + if(!is_numeric($s)){ + return false; + } + + return true; + } /** * if url key is exist in url_rewrite table ,Behind url add some random string. @@ -359,13 +387,33 @@ class Url extends Service $o_url = $this->_origin_url; if (strstr($this->_origin_url, '.')) { list($o_url, $suffix) = explode('.', $this->_origin_url); + $l = $this->randomCount +1; + if (strlen($o_url) > $l) { + $randomStrSub = substr($o_url, strlen($o_url) - $l , $l ); + $orignUrlK = substr($o_url, 0, strlen($o_url) - $l ); + if ($this->isRandomStr($randomStrSub)) { + $o_url = $orignUrlK; + } + } + $randomStr = $this->getRandom(); return $o_url.'-'.$randomStr.'.'.$suffix; - } - $randomStr = $this->getRandom(); + } else { + $l = $this->randomCount +1; + + if (strlen($o_url) > $l) { + $randomStr = substr($o_url, strlen($o_url) - $l , $l ); + $orignUrlK = substr($o_url, 0, strlen($o_url) - $l ); + if ($this->isRandomStr($randomStr)) { + $o_url = $orignUrlK; + } + } + $randomStr = $this->getRandom(); - return $this->_origin_url.'-'.$randomStr; + return $o_url.'-'.$randomStr; + } + } } diff --git a/services/product/ProductMysqldb.php b/services/product/ProductMysqldb.php index 1ddb526d..0403030f 100644 --- a/services/product/ProductMysqldb.php +++ b/services/product/ProductMysqldb.php @@ -406,6 +406,9 @@ class ProductMysqldb extends Service implements ProductInterface if (!$this->initSave($one)) { return false; } + $url_key = isset($one['url_key']) ? $one['url_key'] : ''; + unset($one['url_key']); + $currentDateTime = \fec\helpers\CDate::getCurrentDateTime(); $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; // 得到group spu attr @@ -513,7 +516,7 @@ class ProductMysqldb extends Service implements ProductInterface // 自定义url部分 if ($originUrlKey) { $originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $product_id; - $originUrlKey = isset($one['url_key']) ? $one['url_key'] : ''; + $originUrlKey = $url_key; $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name'); $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey); $model->url_key = $urlKey; @@ -546,6 +549,8 @@ class ProductMysqldb extends Service implements ProductInterface if (!$this->initSave($one)) { return false; } + $url_key = isset($one['url_key']) ? $one['url_key'] : ''; + unset($one['url_key']); $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name'); $product_one = $this->_productModel->find()->where([ 'sku' => $one['sku'], @@ -585,9 +590,8 @@ class ProductMysqldb extends Service implements ProductInterface $this->syncProductCategory($one['category'], $product_id); // 自定义url部分 - $originUrlKey = 'catalog/product/index'; - $originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $product_id; - $originUrlKey = isset($one['url_key']) ? $one['url_key'] : ''; + $originUrl = 'catalog/product/index' . '?' . $this->getPrimaryKey() .'='. $product_id; + $originUrlKey = $url_key; //var_dump([$defaultLangTitle, $originUrl, $originUrlKey]); //echo $defaultLangTitle; $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey); @@ -1191,6 +1195,9 @@ class ProductMysqldb extends Service implements ProductInterface { $sku = $one['sku']; // 查询出来主键。 + $url_key = isset($one['url_key']) ? $one['url_key'] : ''; + unset($one['url_key']); + $primaryKey = $this->getPrimaryKey(); $productModel = $this->getBySku($sku); if (isset($productModel['sku']) && $productModel['sku']) { @@ -1311,7 +1318,7 @@ class ProductMysqldb extends Service implements ProductInterface // 自定义url部分 if ($originUrlKey) { $originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $product_id; - $originUrlKey = isset($one['url_key']) ? $one['url_key'] : ''; + $originUrlKey = $url_key; $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name'); $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey); $model->url_key = $urlKey; -- GitLab