<?php namespace Collect\Api;
use Dever;
Dever::apply('autoload', 'puppeteer', 'vendor');
use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;
class Test
{
    public function chat()
    {
        $url = 'http://192.168.10.242:8000';
        $param['prompt'] = 'hello';
        $result = Dever::curl($url, $param, 'post', true)->result();
        print_r($result);die;
    }

    public function api()
    {
        
    }

    public function web()
    {
        $id = Dever::input('id');
        $web_path = Dever::db('web_path', 'collect')->find($id);
        return Dever::load('web', 'collect')->run($web_path['web_id'], $web_path['id']);
    }

    public function pup()
    {
        //https://blog.csdn.net/weixin_44151650/article/details/116591529
        $puppeteer = new Puppeteer;
        //$config['browserURL'] = 'ws://127.0.0.1:3000/';
        $config['args'] = ['--no-sandbox', '--disable-setuid-sandbox'];
        $config['executablePath'] = '/usr/bin/chromium-browser';
        $config['headless'] = 'new';
        $browser = $puppeteer->launch($config);
        $page = $browser->newPage();

        // 将webdriver字段删除，防止反爬虫
        //$page->evaluateOnNewDocument();

        /*
        await page.evaluateOnNewDocument(() => {
            const newProto = navigator.__proto__;
            delete newProto.webdriver;
            navigator.__proto__ = newProto;
        })*/
        //$page->setJavaScriptEnabled();
        $source = 'b-user-id=76a50748-e5c7-1b13-0a23-cd394bc9de02; _UP_A4A_11_=wb9631628de446ecbae00fa36ed69c9c; _UP_30C_6A_=st96362019ci60vqluum3ufsb23vhkal; _UP_TS_=sg11d29a6577618bce6a13bbb41c560183f; _UP_E37_B7_=sg11d29a6577618bce6a13bbb41c560183f; _UP_TG_=st96362019ci60vqluum3ufsb23vhkal; _UP_335_2B_=1; tfstk=efNWAes3n3x500pkcg_45kGKblcC7u1NPegLSydyJbhJ9kan04JPEvSQhkEq27Wo4oiQ7yAE4zlRRkZTVvrQ-a0KvkEIY8WV_z4otXIZA15arNqwiiSZKWcAgXcdbi5u_aDrtlwaGCsFunCXpnaCcIdXtVZJSxdNJwFjkM0TPZOIu73SIcU8NBddB4MSXznc4QAZfWlMdEMM9qiNlZ9HKFHpgkIud5KZeq04_Z_XVpD-oqiNlZ9HKY3muA7fl39h.; __wpkreporterwid_=6d876116-ad0f-4b71-3a65-5242f7fcc4a8; __itrace_wid=8acfdbe4-a7f7-4df7-034d-cb1548254b45; __pus=cea94375368cd2e6924e8cc35b78e81aAARhLk7aeFqfm1OFLRytmPAIyAmZd22cLuwDG3knUW5NHC9W0vIZeJGDEF5w+Jx2bl4XNm6lMDuk4spx0H9iZ9Nw; __kp=ee235c80-eb2b-11ee-9e69-13aff00f16a0; __kps=AAQbM2POsLOgR8lqIGbPVZcB; __ktd=UG6pZdQkcCZLZBEVFk3xdQ==; __uid=AAQbM2POsLOgR8lqIGbPVZcB; __chkey=; ctoken=MOMW0SpcqR1NherO8fwVWOGc; grey-id=5601708c-3c04-fcf2-30e1-c7292e3bab6b; grey-id.sig=CyiYkKM1OQii7hr9BhHG5HiaVq6DnROFKOetTHzs0NQ; isQuark=false; isQuark.sig=DWPHMZYiiwQ-v58AbcP-rBdSIpzO8ZnrD67BdJuPatU; Video-Auth=T+k6taB6pO7zggrHvs63JRKx0JK6dtwCdq64veJigx2pqnZwNhD/XZ13G5c4bOvUwK/hLJvVOqzLp07woYP34g==; __puus=9e55607d1ab452b03967a99f9c860e58AAQCQNhATdqiwS8g65i1NCBiGm/UaFOeUjkzM91/rM5Su7FfpXrlU0XI6CaPEauqp08tfhrgI4XAfLOhvv4sgIsu78OKVX87+f0j3k8+n8pjJTWCYTlrZCrKZcE3bsKdXf6lGbZNawhyZzZVtBEeGF4xx0Fjff6oz5HWLMQDFOKZ4mfg2lHBPrUJMq39pMR3btLL2WfPZ8GL30HWAifPS3Lo';


        $source = 'ctoken=Q0d-vNz1fd47hrh8noLiFGZa; b-user-id=9cba243e-3dc4-151a-9449-432c417d7c7a; grey-id=98ed2b0e-0ecc-992f-704d-e05147e340f1; grey-id.sig=Ic6pg2hoT6DxtsnqkAjTIRPNtaRzq4lI4g5m0fjIzcM; isQuark=false; isQuark.sig=DWPHMZYiiwQ-v58AbcP-rBdSIpzO8ZnrD67BdJuPatU; _UP_A4A_11_=wb9641d4489c40df98c93aa213af5437; _UP_D_=pc; __wpkreporterwid_=4817d716-ae87-4710-ba79-6ef1de3bc1d0; _UP_F7E_8D_=9Gx8C05DJ1ZACZC0UxPrcEsem8D%2BGJzLUdRO7YsGUI%2BibipNZ4wWoKA3nkVDHeUjK77U6xYgqMbXDik4ZZxmO%2B6jibaMeh%2F15GPrrCl5M87ivs0EP%2FjAQTQimMgEdat62Byd22%2BZGM4yUy0PBELSeMkiJEldlgGDvOTidzNw8s%2FWtKAIxWbnCzZn4%2FJMBUub1SIMcW89g55kCmkq%2FOiGdAEWNX%2Fc5H93FxZfP%2FJ18Vl7%2F40zo8vu0BU4wR0Pq7NklczEGdRq2nIAcu7v22Uw2o%2FxMY0xBdeC9Korm5%2FNHnxl6K%2Bd6FXSoT9a3XIMQO359auZPiZWzrNlZe%2BqnOahXcx7KAhQIRqSOapSmL4ygJor4r5isJhRuDoXy7vJAVuH%2FRDtEJJ8rZTq0BdC23Bz%2B0MrsdgbK%2BiW; _UP_30C_6A_=st964620199l2xw9j38680l99zuv85a7; _UP_TS_=sg18ab38720f49d2e10092cd38590c2ae91; _UP_E37_B7_=sg18ab38720f49d2e10092cd38590c2ae91; _UP_TG_=st964620199l2xw9j38680l99zuv85a7; _UP_335_2B_=1; __pus=3bde6ec9c76b8707e96a89d6b80ff840AARawU+KXrnD0Iodio2/8PqhK9hEfdSWRd9M1eGXBXLZXU33zNJwbLRE1OwmU51NvJeJa/UoIgkhjWixesMEixe1; __kp=2f8ed350-f650-11ee-8424-858497c4193d; __kps=AAStaauII6hAM8snP+s0xv+G; __ktd=GqbNIc8dW2i9aFX7GwO/kg==; __uid=AAStaauII6hAM8snP+s0xv+G; tfstk=fzF-wS1Flijo9xB3Nz60LAklV7_cI7UyMuz6tDmkdoEYSoondY0o9vZSm7DnNglKAmZeEQmnZWEY0oruryVL3BUa87brtalzzXlCjGfi9zzrT6abrIV-GKgal2OSFv8qG8cCjGfca_QrpXwHeGftMriqJ2OSdkGjGmuEAXiSdn9j5miIOXZIGngZSQtIVQOXhjIwyDYSvBFVgD_K_dTD9BFxlMozygdKo73-eca7wBOC54H-fziVjNpJdxaEpW8e9Aa7Qku_V3sKYSUbNRZ1mLm7B2Ugpz1WGbVzPRMQ1MJQXbEKC7MvRCaramlxl5_HQmVxqkN-hNAtKjNiCbwc3gDnwVEQauLpOla0S7k49GKjY80ZG2FF5pnKdgRYs5pCIhmt-LQAkpJEFqA9mRBHzDUTTqnGoKveLx0qkcbAkpJEFquxjZfDLpkmu; __itrace_wid=39956d96-26ea-4bc6-9db1-a19ac6bb3bd7; __puus=4527bded350c9fcb7ed73c444243e71eAASAH2r76ACUYloQDax86wyaS9G7hZI9qvqH37Abc0X7OxONbfotuMOPW6ReAcz0WXIuq7XAwc74NiMm+JVsZ9Na4h0TJg0t/W2DlGnHVXnUhcUA9smGBsSPUcoyfHmgswdFL2nFg0oo+3l+W9rSV0NnsEMtGB9kDeMWQb20bsgd748/16wSKpg9qrxiC3WGhAKToqdAdO32sDdcdOKbzZ9u';
        $source1 = '[
{
    "domain": ".quark.cn",
    "expirationDate": 1713774266.298931,
    "hostOnly": false,
    "httpOnly": false,
    "name": "__kp",
    "path": "/",
    "sameSite": "unspecified",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "6a80b430-f581-11ee-bf97-27350aa7faf8",
    "id": 1
},

{
    "domain": ".quark.cn",
    "expirationDate": 1713774266.298867,
    "hostOnly": false,
    "httpOnly": true,
    "name": "__pus",
    "path": "/",
    "sameSite": "unspecified",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "5938ec7bf221e3d20f01fa439de38109AAQFVqfGKLPeh07IF9Bwie5ZJHmWMeJhi8DW6Snnk2LmRNEVZ7vOTyDt20G9p44n6fplG9sQ75GZ7DbuTJ/iAaOk",
    "id": 4
},

{
    "domain": "pan.quark.cn",
    "expirationDate": 1744095332,
    "hostOnly": true,
    "httpOnly": false,
    "name": "b-user-id",
    "path": "/",
    "sameSite": "unspecified",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "151a1466-8437-433f-2846-c35670e93f5a",
    "id": 18
}
]';

        if (strstr($source, '{')) {
            $cookie = Dever::json_decode($source);
        } else {
            $cookie = str_replace(' ', '', $source);
            $array = explode(';', $cookie);
            $cookie = array();
            foreach ($array as $k => $v) {
                $temp = explode('=', $v);
                $domain = '';
                $host = false;
                if ($temp[0] == '__kp' || $temp[0] == '__pus') {
                    $domain = '.quark.cn';
                } elseif ($temp[0] == 'b-user-id') {
                    $domain = 'pan.quark.cn';
                    $host = true;
                }
                if ($domain) {
                    $cookie[] = array
                    (
                        'domain' => $domain,
                        'expirationDate' => 0,
                        'hostOnly' => $host,
                        'httpOnly' => false,
                        'name' => $temp[0],
                        'path' => '/',
                        'value' => $temp[1],
                        'sameSite' => 'unspecified',
                        'secure' => false,
                        'session' => false,
                        'storeId' => "0",
                        'id' => $k,
                    );
                }
            }
        }
        //print_r($cookie);die;
        foreach ($cookie as $k => $v) {
            $v['expirationDate'] = time() + 3600;
            $page->setCookie($v);
        }
        $header['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.41';
        $header['Accept-Language'] = 'zh-CN,zh;q=0.9';
        //$page->setExtraHTTPHeaders($header);
        /*
        $page->waitForSelector('.ant-table-selection-column');
        $page->click('.ant-table-selection-column');
        */
        $page->goto('https://pan.quark.cn/list#/list/all/8e65d470a9fd4e46b3e1863e454ececf-%E8%BD%BB%E6%9D%BE%E5%AD%A6%E5%90%89%E4%BB%96%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B');

        /* 这个只能生成一个
        # 点击全选
        $page->querySelector('.ant-table-selection-column')->click();
        # 点击全选后出现的分享
        $page->querySelector('#ice-container > section > section > main > div > div.section-main > div.section-header.list-header > div.btn-operate > div.btn-group > div > button:nth-child(2)')->click(array('delay' => 500));
        # 点击分享弹窗里的永久有效
        $page->querySelector('div.ant-modal-body > div > div:nth-child(2) > span.oper-area > div > label:nth-child(4)')->click(array('delay' => 1000));
        # 点击分享按钮
        $page->querySelector('div.ant-modal-content > div.ant-modal-footer > div > div > button')->click(array('delay' => 5000));
        */
        $page->waitForSelector('.ant-table-selection-column');
        $list = $page->evaluate(JsFunction::createWithBody('
            var elements = document.querySelectorAll(".ant-table-tbody tr");
            elements = Array.from(elements);
            elements = elements.map(function(element, index) {
                var result = {}
                result.id = element.getAttribute("data-row-key");
                if (result.id) {
                    var name = element.querySelector(".filename-text").innerHTML;
                    result.name = name
                }
                return result
            });
            return elements;
        '));

        foreach ($list as $k => $v) {
            $param = array();
            $param['expired_type'] = 1;
            $param['fid_list'] = array($v['id']);
            $param['title'] = $v['name'];
            $param['url_type'] = 1;//2是有提取码，1是无提取码
            //$param['passcode'] = '';

            $header['accept'] = 'application/json, text/plain, */*';
            $header['content-type'] = 'application/json';
            $header['sec-ch-ua'] = '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"';
            $header['ec-ch-ua-mobile'] = '?0';
            $header['user-agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
            $header['sec-ch-ua-platform'] = '"Windows"';
            $header['origin'] = 'https://pan.quark.cn';
            $header['sec-fetch-site'] = 'same-site';
            $header['sec-fetch-mode'] = 'cors';
            $header['sec-fetch-dest'] = 'empty';
            $header['accept-encoding'] = 'gzip, deflate, br';
            $header['accept-language'] = 'zh-CN,zh;q=0.9';
            $header['referer'] = 'https://pan.quark.cn/';
            $header['cookie'] = $source;
            $result = Dever::curl('https://drive-pc.quark.cn/1/clouddrive/share?pr=ucpro&fr=pc&uc_param_str=', $param, 'post', true, $header)->result();
            //$result = Dever::curl('https://drive-pc.quark.cn/1/clouddrive/file/sort?pr=ucpro&fr=pc&uc_param_str=&pdir_fid=8e65d470a9fd4e46b3e1863e454ececf&_page=1&_size=50&_fetch_total=1&_fetch_sub_dirs=0&_sort=file_type:asc,updated_at:desc', array(), 'get', false, $header)->result();
            print_r($result);die;
        }
        
        print_r($list);die;
        
        # 点击第一个
        $data = $page->querySelectorXPath('//td[@class="ant-table-selection-column"]');
        foreach ($data as $k => $v) {
            if ($k == 0) {
                $v->click();
                # 点击分享
                $page->querySelector('#ice-container > section > section > main > div > div.section-main > div.section-header.list-header > div.btn-operate > div.btn-group > div > button:nth-child(2)')->click();

                # 点击分享弹窗里的永久有效
                $page->querySelector('div.ant-modal-body > div > div:nth-child(2) > span.oper-area > div > label:nth-child(4)')->click();
                # 点击分享按钮
                $page->querySelector('div.ant-modal-content > div.ant-modal-footer > div > div > button')->click();
                $page->querySelector('.ant-modal-close')->click();
                $v->click();
            }
        }

        /*
        # 点击全选后出现的分享
        $page->querySelector('#ice-container > section > section > main > div > div.section-main > div.section-header.list-header > div.btn-operate > div.btn-group > div > button:nth-child(2)')->click(array('delay' => 500));
        # 点击分享弹窗里的永久有效
        $page->querySelector('div.ant-modal-body > div > div:nth-child(2) > span.oper-area > div > label:nth-child(4)')->click(array('delay' => 1000));
        # 点击分享按钮
        $page->querySelector('div.ant-modal-content > div.ant-modal-footer > div > div > button')->click(array('delay' => 5000));
        */

        //$page->querySelector('.ant-btn-group:ant-btn(2)')->click();
        //print_r($page->content());
        //$element = $page->querySelector('.ant-table-selection-column');
        //$element->click();

        $page->screenshot(['path' => Dever::data() . 'kk.png']);

        $browser->close();
    }
}