提交 38a48221 编写于 作者: T Terry

产品url_key部分bug处理

上级 58a13893
<?php
use yii\db\Migration;
/**
* Class m190912_052057_fecshop_tables
*/
class m190912_052057_fecshop_tables extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$arr = [
"
ALTER TABLE product_flat DROP INDEX sku;
",
"
ALTER TABLE `product_flat` ADD UNIQUE (`sku`);
",
"
ALTER TABLE `product_flat` ADD UNIQUE (`url_key`);
",
];
foreach ($arr as $sql) {
$this->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;
}
*/
}
...@@ -24,7 +24,7 @@ use Yii; ...@@ -24,7 +24,7 @@ use Yii;
*/ */
class Url extends Service class Url extends Service
{ {
public $randomCount = 8; public $randomCount = 9;
public $showScriptName; public $showScriptName;
...@@ -62,14 +62,24 @@ class Url extends Service ...@@ -62,14 +62,24 @@ class Url extends Service
$str = trim($str); $str = trim($str);
$originUrl = $originUrl ? '/'.trim($originUrl, '/') : ''; $originUrl = $originUrl ? '/'.trim($originUrl, '/') : '';
$originUrlKey = $originUrlKey ? '/'.trim($originUrlKey, '/') : ''; $originUrlKey = $originUrlKey ? '/'.trim($originUrlKey, '/') : '';
if ($originUrlKey) { if ($originUrlKey && $originUrl) {
/** /**
* if originUrlKey and originUrl is exist in url rewrite collectons. * if originUrlKey and originUrl is exist in url rewrite collectons.
*/ */
$model = $this->find(); $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([ $data_one = $model->where([
'custom_url_key' => $originUrlKey, 'custom_url_key' => $originUrlKey,
//'origin_url' => $originUrl, 'origin_url' => $originUrl,
])->one(); ])->one();
if (isset($data_one['custom_url_key'])) { if (isset($data_one['custom_url_key'])) {
// 只要进行了查询,就要更新一下rewrite url表的updated_at. // 只要进行了查询,就要更新一下rewrite url表的updated_at.
...@@ -79,6 +89,7 @@ class Url extends Service ...@@ -79,6 +89,7 @@ class Url extends Service
return $originUrlKey; return $originUrlKey;
} }
*/
} }
if ($originUrlKey) { if ($originUrlKey) {
$urlKey = $this->generateUrlByName($originUrlKey); $urlKey = $this->generateUrlByName($originUrlKey);
...@@ -348,6 +359,23 @@ class Url extends Service ...@@ -348,6 +359,23 @@ class Url extends Service
return $str; 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. * if url key is exist in url_rewrite table ,Behind url add some random string.
...@@ -359,13 +387,33 @@ class Url extends Service ...@@ -359,13 +387,33 @@ class Url extends Service
$o_url = $this->_origin_url; $o_url = $this->_origin_url;
if (strstr($this->_origin_url, '.')) { if (strstr($this->_origin_url, '.')) {
list($o_url, $suffix) = explode('.', $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(); $randomStr = $this->getRandom();
return $o_url.'-'.$randomStr.'.'.$suffix; return $o_url.'-'.$randomStr.'.'.$suffix;
} } else {
$randomStr = $this->getRandom(); $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;
}
} }
} }
......
...@@ -406,6 +406,9 @@ class ProductMysqldb extends Service implements ProductInterface ...@@ -406,6 +406,9 @@ class ProductMysqldb extends Service implements ProductInterface
if (!$this->initSave($one)) { if (!$this->initSave($one)) {
return false; return false;
} }
$url_key = isset($one['url_key']) ? $one['url_key'] : '';
unset($one['url_key']);
$currentDateTime = \fec\helpers\CDate::getCurrentDateTime(); $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
$primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
// 得到group spu attr // 得到group spu attr
...@@ -513,7 +516,7 @@ class ProductMysqldb extends Service implements ProductInterface ...@@ -513,7 +516,7 @@ class ProductMysqldb extends Service implements ProductInterface
// 自定义url部分 // 自定义url部分
if ($originUrlKey) { if ($originUrlKey) {
$originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $product_id; $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'); $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name');
$urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey); $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey);
$model->url_key = $urlKey; $model->url_key = $urlKey;
...@@ -546,6 +549,8 @@ class ProductMysqldb extends Service implements ProductInterface ...@@ -546,6 +549,8 @@ class ProductMysqldb extends Service implements ProductInterface
if (!$this->initSave($one)) { if (!$this->initSave($one)) {
return false; return false;
} }
$url_key = isset($one['url_key']) ? $one['url_key'] : '';
unset($one['url_key']);
$defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name'); $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name');
$product_one = $this->_productModel->find()->where([ $product_one = $this->_productModel->find()->where([
'sku' => $one['sku'], 'sku' => $one['sku'],
...@@ -585,9 +590,8 @@ class ProductMysqldb extends Service implements ProductInterface ...@@ -585,9 +590,8 @@ class ProductMysqldb extends Service implements ProductInterface
$this->syncProductCategory($one['category'], $product_id); $this->syncProductCategory($one['category'], $product_id);
// 自定义url部分 // 自定义url部分
$originUrlKey = 'catalog/product/index'; $originUrl = 'catalog/product/index' . '?' . $this->getPrimaryKey() .'='. $product_id;
$originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $product_id; $originUrlKey = $url_key;
$originUrlKey = isset($one['url_key']) ? $one['url_key'] : '';
//var_dump([$defaultLangTitle, $originUrl, $originUrlKey]); //var_dump([$defaultLangTitle, $originUrl, $originUrlKey]);
//echo $defaultLangTitle; //echo $defaultLangTitle;
$urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey); $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey);
...@@ -1191,6 +1195,9 @@ class ProductMysqldb extends Service implements ProductInterface ...@@ -1191,6 +1195,9 @@ class ProductMysqldb extends Service implements ProductInterface
{ {
$sku = $one['sku']; $sku = $one['sku'];
// 查询出来主键。 // 查询出来主键。
$url_key = isset($one['url_key']) ? $one['url_key'] : '';
unset($one['url_key']);
$primaryKey = $this->getPrimaryKey(); $primaryKey = $this->getPrimaryKey();
$productModel = $this->getBySku($sku); $productModel = $this->getBySku($sku);
if (isset($productModel['sku']) && $productModel['sku']) { if (isset($productModel['sku']) && $productModel['sku']) {
...@@ -1311,7 +1318,7 @@ class ProductMysqldb extends Service implements ProductInterface ...@@ -1311,7 +1318,7 @@ class ProductMysqldb extends Service implements ProductInterface
// 自定义url部分 // 自定义url部分
if ($originUrlKey) { if ($originUrlKey) {
$originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $product_id; $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'); $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name');
$urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey); $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey);
$model->url_key = $urlKey; $model->url_key = $urlKey;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册