提交 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;
*/
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;
}
}
}
......
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册