<?php


namespace app\controller\api\wx;


use think\facade\Cache;

class WxSdk
{
    private $appid = 'wx9082e5ac2fdb513f';
    private $secret = '88980d4455cd48e258c6bdff28029383';
    public function mp_link()
    {
        $userId = request()->param('uid');
        $common = app()->make(\app\controller\api\Common::class);
        $token = $common->get_mp_access_token();

        $url = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=" . $token;

        $body = [
            'path' => '/pagesC/goods_details/index_detail',
            'query' => 'uid='.$userId . '&type=qrcode',
            'env_version' => 'release'
        ];
        $data = curlPost($url, $body);
        if(isset($data['url_link']) && !empty($data['url_link'])) {
            return app('json')->success(['url' => $data['url_link']]);
        }
        return app('json')->fail('服务繁忙，请稍后再试');
    }

    public function get_tdyl_mp_link()
    {
        $path = request()->param('path');
        $query = request()->param('query');
        $env_version = request()->param('env_version');
        if (empty($path) || empty($query) || empty($env_version)) return app('json')->fail("参数错误");
        $common = app()->make(\app\controller\api\Common::class);
        $token = $common->get_mp_access_token("WECHAT_TDYL");

        $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $token;

        $body = [
            'jump_wxa' => [
                'path' => $path,
                'query' => $query,
                'env_version' => $env_version
            ]
        ];
        $data = curlPost($url, $body);
        if(isset($data['openlink']) && !empty($data['openlink'])) {
            return app('json')->success(['url' => $data['openlink']]);
        }
        return app('json')->fail('服务繁忙，请稍后再试');
    }

    public function get_gx_link(){

        $common = app()->make(\app\controller\api\Common::class);
        $token = $common->get_mp_access_token();
        $url = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token=".$token;
        $body = [
            'page_url' => "pages/Home/index?uid=4239"
        ];
        $data = curlPost($url, $body);
        if(isset($data['link']) && !empty($data['link'])) {
//            return "<script>alert(111)</script>";
//            return "<script>document.location.href = 'https://www.baidu.com'</script>";
            return "<script>document.location.href = '{$data['link']}'</script>";
        }
    }

}