<?php

namespace app\extra\weMini;

use yzh52521\EasyHttp\Http;

class Link extends BasicWeChat
{



    public function createQrcodeWx(string $path = "",string $query = "", string $filePath = "")
    {
        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$this->getAccessToken()}";
        $data = [
            "path"  => $path."?".$query,
            'width' => 430
        ];

        // 初始化cURL
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // 执行请求
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        // 检查请求是否成功
        if ($httpCode != 200) {
            return false;
        }

        // 检查返回的是否是JSON（错误情况）
        $decode = json_decode($response, true);
        if ($decode && isset($decode['errcode'])) {
            error_log("微信接口返回错误: " . $decode['errmsg']);
            return false;
        }

        // 保存为JPG文件
        $filePath = public_path().$filePath;
        $result = file_put_contents($filePath, $response);

        if ($result === false) {
            error_log("文件保存失败: " . $filePath);
            return false;
        }
        return $filePath;
//        echo $url;
//        print_r($param);
//        return Http::asJson()->post($url,$param);
    }

    public function createQrcode(string $path = "",string $query = "", string $filePath = "")
    {
        $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$this->getAccessToken()}";
        $data = [
            "path"  => $path."?".$query,
            'width' => 430
        ];

        // 初始化cURL
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // 执行请求
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        // 检查请求是否成功
        if ($httpCode != 200) {
            return false;
        }

        // 检查返回的是否是JSON（错误情况）
        $decode = json_decode($response, true);
        if ($decode && isset($decode['errcode'])) {
            error_log("微信接口返回错误: " . $decode['errmsg']);
            return false;
        }

        // 保存为JPG文件
        $filePath = public_path().$filePath;
        $result = file_put_contents($filePath, $response);

        if ($result === false) {
            error_log("文件保存失败: " . $filePath);
            return false;
        }
        return $filePath;
//        echo $url;
//        print_r($param);
//        return Http::asJson()->post($url,$param);
    }


    public function createLink(string $path = "",string $query = "",string $version = "release")
    {
        $url = "https://api.weixin.qq.com/wxa/generate_urllink?access_token={$this->getAccessToken()}";
        $param = [
            "path"          => $path,
            "query"         => $query,
            "env_version"   => $version
        ];
        return Http::asJson()->post($url,$param)->array();
    }


    public function createLinkJump(string $path = "",string $query = "",string $version = "release")
    {
        $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token={$this->getAccessToken()}";
        $param = [
            "jump_wxa"  => [
                "path"          => $path,
                "query"         => $query,
                "env_version"   => $version
            ],
        ];
        print_r($param);
        return Http::asJson()->post($url,$param)->array();
    }


    public function createShortLink(string $path = "",string $query = "",string $version = "release")
    {
        $url = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token={$this->getAccessToken()}";
        $param = [
            "page_url"   => $path."?".$query,
        ];
        return Http::asJson()->post($url,$param)->array();
    }

}