CollectTask.php 2.4 KB
<?php

namespace App\Models\Collect;

use App\Models\Base;

class CollectTask extends Base
{
    //设置关联表名
    protected $table = 'gl_collect_task';

    //连接数据库
    protected $connection = 'custom_mysql';

    const STATUS_UN = 0;
    const STATUS_ING = 1;
    const STATUS_COM = 2;
    const STATUS_FAIL = 3;

    public static function _insert($url, $project_id, $source, $source_id, $domain, $link_type = 0, $language_list = [], $page_list = [])
    {
        if (!$url) {
            return;
        }

        $url_arr = parse_url($url);

        $where = [
            'project_id' => $project_id,
            'source' => $source,
            'source_id' => $source_id,
            'language' => ''
        ];

        $task = self::where($where)->first();
        $data = [];
        $now = date('Y-m-d H:i:s');
        if (!$task) {
            $data[] = [
                'project_id' => $project_id,
                'source' => $source,
                'source_id' => $source_id,
                'domain' => $url_arr['host'] ?? $domain,
                'route' => substr($url_arr['path'], 0, 1) == '/' ? $url_arr['path'] : '/' . $url_arr['path'],
                'language' => '',
                'created_at' => $now,
                'updated_at' => $now,
            ];

            if ($link_type > 0 && $language_list && in_array($url_arr['path'], $page_list)) {
                $domain_arr = explode('.', $url_arr['host'] ?? $domain);
                foreach ($language_list as $v_lan) {
                    if ($link_type == 1) {
                        //二级域名
                        $domain_arr[0] = $v_lan;
                        $new_domain = implode('.', $domain_arr);
                    } else {
                        //二级目录
                        $new_domain = ($url_arr['host'] ?? $domain) . '/' . $v_lan;
                    }

                    $data[] = [
                        'project_id' => $project_id,
                        'source' => $source,
                        'source_id' => $source_id,
                        'domain' => $new_domain,
                        'route' => $url_arr['path'],
                        'language' => $v_lan,
                        'created_at' => $now,
                        'updated_at' => $now,
                    ];
                }
            }

            self::insert($data);
        }
    }
}