<?php

namespace app\extra\tools;

use yzh52521\EasyHttp\Http;

class XcJob
{



    /**
     * 默认header
     * @var string[]
     */
    protected array $header = [
        "content-type"  => "application/json"
    ];

    public function token(): static
    {
        $this->header = [
            "cookie" => $this->getCookie()
        ];
        return $this;
    }

    /**
     * 获取达人详情信息
     * @param string $uid
     * @return array
     */
    public function getStarDetail(string $uid = ""): array
    {
        $param = [
            "o_author_id"   => $uid,
            "platform_source"   => 1,
            "platform_channel"  => 1,
            "recommend" => true,
            "need_sec_uid"  => true,
            "need_linkage_info" => true
        ];
        $resp = Http::withHeaders($this->header)->asJson()->get("https://www.xingtu.cn/gw/api/author/get_author_base_info",$param)->array();
        if (empty($resp['sec_uid'])) return [0,'获取数据失败'];
        return [1,$resp];
    }

    /**
     * 获取达人信息
     * {"scene_param":{"platform_source":1,"search_scene":1,"display_scene":1,"marketing_target":1,"task_category":1,"first_industry_id":0,"task_status":3},"search_param":{"keyword":"11_0306","seach_type":2,"is_new_nickname_query":true},"sort_param":{"sort_type":2,"sort_field":{"field_name":"score"}},"page_param":{"page":1,"limit":20},"attribute_filter":[{"field":{"field_name":"price_by_video_type__ge","rel_id":"2"},"field_value":"0"}]}
     * @param string $uid
     * @return array
     */
    public function getStarData(string $uid = ""): array
    {
        $param = [
            "scene_param"   => [
                "platform_source" => 1,
                "search_scene" => 1,
                "display_scene" => 1,
                "marketing_target" => 1,
                "task_category" => 1,
                "first_industry_id" => 0,
                "task_status" => 3
            ],
            "search_param"  => [
                "keyword"   => $uid,
                "seach_type" => 2,
                "is_new_nickname_query" => true
            ],
            "sort_param"    => [
                "sort_type" => 2,
                "sort_field" => [
                    "field_name" => "score"
                ]
            ],
            "page_param"    => [
                "page" => 1,
                "limit" => 20
            ]
        ];
        $resp = Http::withHeaders($this->header)->asJson()->post("https://www.xingtu.cn/gw/api/gsearch/search_for_author_square",$param)->array();
        if ($resp['base_resp']['status_code'] <> 0) return [0,'获取数据失败'];
        return [1,['total' => $resp['pagination']['total_count'],'has_more' => $resp['pagination']['has_more'],'list' => $resp['authors']]];
    }

    /**
     * 获取星川任务列表
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getTaskList(int $page = 1,int $size = 10): array
    {
        // {"query":{"task_category_list":[137],"order_status":[],"qc_metrics_time_range_param":{"start_time":"1767628800000","end_time":"1783353599000"}},"page":1,"limit":10}
        $param = [
            "query" => [
                "task_category_list" => [137],
                "order_status"       => [],
                "qc_metrics_time_range_param"   => [
                    "start_time"    => strtotime(date("Y-m-d 00:00:00",strtotime("-180 day"))) * 1000,
                    "end_time"      => strtotime(date("Y-m-d 23:59:59")) * 1000,
                ]
            ],
            "page"  => $page,
            "limit" => $size
        ];
        $resp = Http::withHeaders($this->header)->asJson()->post("https://www.xingtu.cn/gw/api/task/provider_get_task_order_list",$param)->array();
        if ($resp['base_resp']['status_code'] <> 0) return [0,'获取数据失败'];
        return [1,['total' => $resp['pagination']['total_count'],'has_more' => $resp['pagination']['has_more'],'page' => $resp['pagination']['page'],'list' => $resp['provider_task_order_list']]];
    }

    /**
     * 投稿达人
     * @param string $jobId
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getTaskAuthor(string $jobId = "",int $page = 1,int $size = 10): array
    {
        // {"task_id":"7650432488166899755","qc_metrics_time_range_param":{"start_time":"1767628800000","end_time":"1783353599000"},"page":2,"limit":10,"scene":1}
        $param = [
            "task_id"       => $jobId,
            "qc_metrics_time_range_param"   => [
                "start_time"    => strtotime(date("Y-m-d 00:00:00",strtotime("-180 day"))) * 1000,
                "end_time"      => strtotime(date("Y-m-d 23:59:59")) * 1000,
            ],
            "page"  => $page,
            "limit" => $size,
            "scene" => 1
        ];
        $resp = Http::withHeaders($this->header)->post("https://www.xingtu.cn/gw/api/task/provider_get_task_author_performance_list",$param)->array();
        if ($resp['base_resp']['status_code'] <> 0) return [0,'获取数据失败'];
        if ($resp['pagination']['total_count'] == 0) return [0,'没有数据'];
        return [1,['total' => $resp['pagination']['total_count'],'has_more' => $resp['pagination']['has_more'],'page' => $resp['pagination']['page'],'list' => $resp['author_list']]];
    }

    /**
     * 获取达人投稿视频消耗数据
     * https://www.xingtu.cn/gw/api/task/provider_get_star_task_item_list
     * POST
     * {"page":1,"limit":10,"query":{"provider_confirmed":false,"task_search_key":"7628487095040311348","qc_metrics_time_range_param":{"start_time":"1762963200000","end_time":"1778687999000"}}}
     * @param string $jobId
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getAuthorVideo(string $jobId = "",int $page = 1,int $size = 10): array
    {
        $param = [
            "page"  => $page,
            "limit" => $size,
            "query" => [
                "provider_confirmed"    => false,
                "task_search_key"       => $jobId,
                "qc_metrics_time_range_param"   => [
                    "start_time"    => strtotime(date("Y-m-d 00:00:00",strtotime("-180 day"))) * 1000,
                    "end_time"      => strtotime(date("Y-m-d 23:59:59")) * 1000,
                ]
            ]
        ];
        $resp = Http::withHeaders($this->header)->asJson()->post("https://www.xingtu.cn/gw/api/task/provider_get_star_task_item_list",$param)->array();
        if ($resp['base_resp']['status_code'] <> 0) return [0,'获取数据失败'];
        return [1,['total' => $resp['pagination']['total_count'],'has_more' => $resp['pagination']['has_more'],'page' => $resp['pagination']['page'],'list' => $resp['item_list']]];
    }

    /**
     * 获取单个任务中指定达人的投稿视频
     * @param string $jobId
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getAuthorVide2Task(string $jobId = "",string $starId = "",int $page = 1,int $size = 10): array
    {
        $param = [
            "page"  => $page,
            "limit" => $size,
            "query" => [
                "provider_confirmed"    => false,
                "task_search_key"       => $jobId,
                "author_search_key"     => $starId,
                "qc_metrics_time_range_param"   => [
                    "start_time"    => strtotime(date("Y-m-d 00:00:00",strtotime("-180 day"))) * 1000,
                    "end_time"      => strtotime(date("Y-m-d 23:59:59")) * 1000,
                ]
            ]
        ];
        $resp = Http::withHeaders($this->header)->asJson()->post("https://www.xingtu.cn/gw/api/task/provider_get_star_task_item_list",$param)->array();
        if ($resp['base_resp']['status_code'] <> 0) return [0,'获取数据失败'];
        return [1,['total' => $resp['pagination']['total_count'],'has_more' => $resp['pagination']['has_more'],'page' => $resp['pagination']['page'],'list' => $resp['item_list']]];
    }

    /**
     * 获取视频播放链接
     * @param string $videoId
     * @return array
     */
    public function getVideoDetail(string $videoId = ""): array
    {
        $param = [
            "video_id" => $videoId
        ];
        $resp = Http::withHeaders($this->header)->get("https://www.xingtu.cn/gw/api/ggeneric/get_video_info",$param)->array();
        if ($resp['base_resp']['status_code'] <> 0) return [0,'获取数据失败'];
        return [1,['cover' => $resp['cover'],'duration' => $resp['duration'],'play_url' => $resp['play_url'],'width' => $resp['width'],'height' => $resp['width']]];
    }

    /**
     * 生成邀约达人参与任务二维码
     * @param string $jobId
     * @return array
     */
    public function createJobQrcode(string $jobId = ""): array
    {
        $param = [
            "template_id"   => "7550880075256709158",
            "placeholder_params"    => [
                "challenge_id"  => $jobId,
                "provider_id"   => "1829709687645195",
                "utm_medium"    => "xingtu_provider_star_qc_unite_invite"
            ],
            "expired_time"  => strtotime("+1 year"), // 有效期1年
            "event_id"      => $jobId,
            "share_type"    => 4
        ];
        $resp = Http::withHeaders($this->header)->asJson()->post("https://www.xingtu.cn/gw/api/fe_common_service/share/info",$param)->array();
        if ($resp['base_resp']['status_code'] <> 0) return [0,'生成失败'];
        return [1,"https://www.xingtu.cn/share/{$resp['share_id']}?hide_nav_bar=1&hide_status_bar=1&should_full_screen=1&utm_source=share_4&session_track_id={$resp['share_id']}"];
    }

    /**
     * 星川任务详情
     * @param string $jobId
     * @return array
     */
    public function getJobDetail(string $jobId = ""): array
    {
        return Http::withHeaders($this->header)->get("https://www.xingtu.cn/gw/api/task/provider_get_project_detail",['project_id' => $jobId])->array();
    }


    protected function getCookie(): string
    {
        return "csrftoken=DiDhfVo0pz9HnEsJ3zGKA39ixm9AxA8t; tt_webid=7637854168573183526; passport_csrf_token=9510b4581fea0015ec5e3293fefb6194; passport_csrf_token_default=9510b4581fea0015ec5e3293fefb6194; is_staff_user=false; has_biz_token=false; s_v_web_id=verify_mqhwxxgj_rimh5qRs_m3VS_4oAv_8yXX_EMfuKVBYkpVW; passport_auth_status=39cd9f76ea3c8ac5c9beab7dfee4825f%2C56147e39ce55d737468b0c00fe003bc2; passport_auth_status_ss=39cd9f76ea3c8ac5c9beab7dfee4825f%2C56147e39ce55d737468b0c00fe003bc2; uid_tt=32a10b0de605d5e5e09080d5531eb77e; uid_tt_ss=32a10b0de605d5e5e09080d5531eb77e; sid_tt=528106ce020f1db0e4ca4ff7895135ef; sessionid=528106ce020f1db0e4ca4ff7895135ef; sessionid_ss=528106ce020f1db0e4ca4ff7895135ef; Hm_lvt_5d77c979053345c4bd8db63329f818ec=1781690725,1782806402; HMACCOUNT=7B24288D273CC431; csrf_session_id=7b04abc1fd6e463b4e7e191681f733da; Hm_lpvt_5d77c979053345c4bd8db63329f818ec=1783489162; sid_guard=528106ce020f1db0e4ca4ff7895135ef%7C1783489186%7C5184000%7CSun%2C+06-Sep-2026+05%3A39%3A46+GMT; session_tlb_tag=sttt%7C16%7CUoEGzgIPHbDkyk_3iVE17_________-2m7Gf0ljknGiOP6NwunArIr0uRP8Cc8jMGlCcp1XhGac%3D; sid_ucp_v1=1.0.0-KDFkMTdlODNhYTQ5NTZkMTg4YWYzZWE0OTA2OTQzOTFiMzc0MjNjNGMKFwiUv5Cvv8yyBxCixbfSBhimDDgCQPEHGgJsZiIgNTI4MTA2Y2UwMjBmMWRiMGU0Y2E0ZmY3ODk1MTM1ZWY; ssid_ucp_v1=1.0.0-KDFkMTdlODNhYTQ5NTZkMTg4YWYzZWE0OTA2OTQzOTFiMzc0MjNjNGMKFwiUv5Cvv8yyBxCixbfSBhimDDgCQPEHGgJsZiIgNTI4MTA2Y2UwMjBmMWRiMGU0Y2E0ZmY3ODk1MTM1ZWY; star_sessionid=85b9f01f4fb6e21eaaeadeb62d7b4b6c";
    }
}