作者 赵彬吉

seo tdk

... ... @@ -13,6 +13,7 @@ use App\Models\Domain\DomainInfo;
use App\Models\Mail\Mail;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\Project\KeywordPrefix;
use App\Models\Project\Project;
use App\Models\Project\ProjectKeyword;
use App\Models\Project\ProjectUpdateTdk;
... ... @@ -373,39 +374,9 @@ class UpdateSeoTdk extends Command
$seo_title = $v[$this->topic_fields[$table]];;
//只有推广项目 且未标记特殊前后缀 才加 前后缀
if($project->type == Project::TYPE_TWO && !in_array(8, explode(',', $project->deploy_optimize->special))) {
$prefix = $this->getPrefixKeyword($project_id, 'prefix', 1);
$suffix = $this->getPrefixKeyword($project_id, 'suffix', 2);
if (empty($prefix) || empty($suffix)) {
continue;
}
$title = $seo_title;
$prefix = $this->getPrefixKeyword($project_id, 'prefix', 1, $title);
//in,for,with,to,near,from 这些介词 只拼前缀,不拼后缀
$is_contains_jieci = false;
$words = explode(' ', $title);
foreach ($words as $word){
$word = Str::replace([',', '!', '?'], '', $word);
if(in_array(strtolower($word), ['in', 'for', 'with', 'to', 'near','from'])){
$is_contains_jieci = true;
}
}
$suffix = '';
if(!$is_contains_jieci){
// 某些后缀不能并存的情况
$ban_suffix = [];
//services/service 结尾的词,后缀不拼manufacturer,factory
if (Str::endsWith($title, ['services', 'service', 'Services', 'Service'])) {
$ban_suffix = ['manufacturer', 'factory', 'Manufacturer', 'Factory', 'Factories', 'Manufacturers'];
}
//前缀有wholesale或cheap的词,后缀不拼 manufacturer,factory,exporter,company
if (Str::startsWith($title, ['wholesale', 'cheap', 'Wholesale', 'Cheap'])) {
$ban_suffix = array_merge($ban_suffix, ['manufacturer', 'factory', 'exporter', 'company', 'Manufacturer', 'Factory', 'Exporter', 'Company', 'Factories', 'Manufacturers', 'Exporters', 'Companies']);
}
$suffix = $this->getPrefixKeyword($project_id, 'suffix', 2, $title, $ban_suffix);
}
$seo_title = $prefix . ' ' . $title . ' ' . $suffix;
$prefix = $this->getPrefixKeyword($project_id, 'prefix', 1, $project->main_lang_id, $seo_title);
$suffix = $this->getPrefixKeyword($project_id, 'suffix', 2, $project->main_lang_id, $seo_title);
$seo_title = $prefix . ' ' . $seo_title . ' ' . $suffix;
}
$data[$field] = trim($seo_title);
... ... @@ -576,62 +547,111 @@ class UpdateSeoTdk extends Command
* @param $project_id
* @param $type
* @param $num
* @param int $main_lang_id
* @param string $topic
* @param array $ban 被禁用的前后缀
* @return string
*/
public function getPrefixKeyword($project_id, $type, $num, $topic='', $ban = [])
public function getPrefixKeyword($project_id, $type, $num, $main_lang_id = 0, $topic='')
{
$str = '';
$ban = []; //被禁用的前后缀
$info = $this->getDeployOptimize($project_id);
if (!empty($info['keyword_' . $type])) {
$fix_keyword = explode(",", $info['keyword_' . $type]);
$fix_keyword = array_filter($fix_keyword);
//去掉标题存在的词
if ($topic) {
foreach ($fix_keyword as $k=>$keyword) {
// 被禁用的关键词
if (in_array($keyword, $ban)) {
unset($fix_keyword[$k]);
}
// 前后缀如果已经存在, 就不在拼接当前类型
if (FALSE !== strpos($topic, $keyword))
return $str;
//复数转单数
$keyword = Str::singular($keyword);
$topic_words = explode(" ", $topic);
if($type == 'prefix' && Str::startsWith($topic_words[0], $keyword)){
unset($fix_keyword[$k]);
}
if($type == 'suffix' && Str::startsWith($topic_words[count($topic_words)-1], $keyword)){
unset($fix_keyword[$k]);
}
//没有勾选前后缀
if (empty($info['keyword_' . $type])) {
return $str;
}
//前后缀(小语种包括自定义前后缀)如果已经存在,就不在拼接当前类型 后缀只包含了一个,要再拼一个(需去重)
$all_prefixes = $this->getAllPrefix($type == 'prefix' ? 1 : 2, $main_lang_id == 1 ? 0 : $project_id);
//加上选中的自定义前后缀
$all_prefixes = array_merge($all_prefixes, explode(",", $info['keyword_' . $type]));
$all_prefixes = array_filter($all_prefixes);
$all_prefixes = array_map('strtolower', $all_prefixes);
//in,for,with,to,near,from 这些介词 只拼前缀,不拼后缀
$preposition = ['in', 'for', 'with', 'to', 'near','from'];
//标题拆成词
$topic_words = explode(' ', strtolower($topic));
$i= 0;
foreach ($topic_words as $topic_word){
//包含了前后缀
if(in_array($topic_word, $all_prefixes)){
//前缀包含一个就不拼了 后缀包含两个才不再拼
if($i == $num - 1){
return $str;
}
$ban[] = $topic_word;
$i++;
$num--;
}
//in,for,with,to,near,from 这些介词 只拼前缀,不拼后缀
if(in_array($topic_word, $preposition) && $type == 'suffix'){
return $str;
}
}
//services/service 结尾的词,后缀不拼manufacturer,factory
if (Str::endsWith(strtolower($topic), ['services', 'service']) && $type == 'suffix') {
$ban = array_merge($ban, ['manufacturer', 'manufacturers', 'factory', 'factories']);
}
//前缀有wholesale或cheap的词,后缀不拼 manufacturer,factory,exporter,company
if (Str::startsWith(strtolower($topic), ['wholesale', 'cheap']) && $type == 'prefix') {
$ban = array_merge($ban, ['manufacturer', 'manufacturers', 'factory', 'factories', 'exporter', 'exporters', 'company', 'companies', 'supplier', 'suppliers']);
}
//关键词是否包含 品牌词
$brand_keywords = explode("\r\n", $info['brand_keyword']);
$is_contains_brand = false;
foreach ($brand_keywords as $brand_keyword) {
if (Str::contains(strtolower($topic), strtolower(trim($brand_keyword)))) {
$is_contains_brand = true;
break;
}
}
//包含品牌词 排除自定义前后缀
if ($is_contains_brand) {
$customer_keywords = KeywordPrefix::where('project_id', $project_id)->pluck('keyword')->toArray();
$ban = array_merge($ban, $customer_keywords);
}
//勾选的前后缀
$fix_keyword = explode(",", $info['keyword_' . $type]);
$fix_keyword = array_filter($fix_keyword);
foreach ($fix_keyword as $k => $keyword) {
// 被禁用的关键词
if (in_array(strtolower(Str::plural($keyword)), $ban)) {
unset($fix_keyword[$k]);
}
if (in_array(strtolower(Str::singular($keyword)), $ban)) {
unset($fix_keyword[$k]);
}
//随机取 并单复数去重
shuffle($fix_keyword);
$keywords = [];
foreach ($fix_keyword as $v){
if($num == 0){
}
//随机取 并单复数去重
shuffle($fix_keyword);
$keywords = [];
foreach ($fix_keyword as $v) {
if ($num == 0) {
break;
}
$is_repeat = false;
foreach ($keywords as $keyword) {
if (Str::singular($keyword) == Str::singular($v)) {
$is_repeat = true;
break;
}
$is_repeat = false;
foreach ($keywords as $keyword){
if(Str::singular($keyword) == Str::singular($v)){
$is_repeat = true;
break;
}
}
if($is_repeat){
continue;
}
$keywords[] = $v;
$num--;
}
$str = implode(', ', $keywords);
if ($is_repeat) {
continue;
}
$keywords[] = $v;
$num--;
}
return $str;
return implode(', ', $keywords);
}
... ... @@ -653,6 +673,24 @@ class UpdateSeoTdk extends Command
}
/**
* 所有前后缀 和 当期项目的自定义前后缀
* @param $type
* @param int $project_id
* @return mixed
* @author zbj
* @date 2025/4/15
*/
public function getAllPrefix($type, int $project_id = 0){
$cache_key = 'AllPrefix_' . $type . '_' . $project_id;
$data = Cache::get($cache_key);
if(!$data){
$data = KeywordPrefix::whereIn('project_id', [0, $project_id])->where('type', $type)->pluck('keyword')->toArray();
Cache::put($cache_key, $data, 600);
}
return $data;
}
/**
* @remark :获取公司英文名称
* @name :companyName
* @author :lyh
... ...