<?php
namespace  liuniu;
use dh2y\qrcode\QRcode;
use think\Request;

class UtilService
{
    /**
     * 获取POST请求的数据
     * @param $params
     * @param null $request
     * @param bool $suffix
     * @return array
     */
    public static function postMore($params, $request = null, $suffix = false)
    {
        if ($request === null) $request = Request::instance();
        $p = [];
        $i = 0;
        foreach ($params as $param) {
            if (!is_array($param)) {
                $p[$suffix == true ? $i++ : $param] = $request->param($param);
            } else {
                if (!isset($param[1])) $param[1] = null;
                if (!isset($param[2])) $param[2] = '';
                if (is_array($param[0])) {
                    $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
                    $keyName = $param[0][0];
                } else {
                    $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
                    $keyName = $param[0];
                }
                $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
            }
        }
        return $p;
    }

    /**
     * 获取请求的数据
     * @param $params
     * @param null $request
     * @param bool $suffix
     * @return array
     */
    public static function getMore($params, $request = null, $suffix = false)
    {
        if ($request === null) $request = Request::instance();
        $p = [];
        $i = 0;
        foreach ($params as $param) {
            if (!is_array($param)) {
                $p[$suffix == true ? $i++ : $param] = $request->param($param);
            } else {
                if (!isset($param[1])) $param[1] = null;
                if (!isset($param[2])) $param[2] = '';
                if (is_array($param[0])) {
                    $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
                    $keyName = $param[0][0];
                } else {
                    $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
                    $keyName = $param[0];
                }
                $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
            }
        }
        return $p;
    }
    /**
     * TODO 获取小程序二维码是否生成
     * @param $url
     * @return array
     */
    public static function remoteImage($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($curl);
        $result = json_decode($result, true);
        if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
        return ['status' => true];
    }

    /**
     * TODO 修改 https 和 http 移动到common
     * @param $url $url 域名
     * @param int $type 0 返回https 1 返回 http
     * @return string
     */
    public static function setHttpType($url, $type = 0)
    {
        $domainTop = substr($url, 0, 5);
        if ($type) {
            if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
        } else {
            if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
        }
        return $url;
    }
    /**
     * 获取二维码
     * @param $url
     * @param $name
     * @return array|bool|string
     */
    public static function getQRCodePath($url, $name)
    {
        if (!strlen(trim($url)) || !strlen(trim($name))) return false;
        try {
            $uploadType = 1;
            //TODO 没有选择默认使用本地上传
            if (!$uploadType) $uploadType = 1;
            $uploadType = (int)$uploadType;
            $siteUrl = Request::instance()->domain();
            if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为：http://域名';
            $info = [];
            $outfile = "./uploads/code";
            $code = new QRcode();
            $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
            $content = file_get_contents('.' . $wapCodePath);
            if ($uploadType === 1) {
                $info["code"] = 200;
                $info["name"] = $name;
                $info["dir"] = $wapCodePath;
                $info["time"] = time();
                $info['size'] = 0;
                $info['type'] = 'image/png';
                $info["image_type"] = 1;
                $info['thumb_path'] = $wapCodePath;
                return $info;
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * 获取小程序外连
     * @param $cid
     * @return mixed
     */
    public static function getMiniScheme($cid,$param)
    {
        if (!cache('Scheme_' . $cid)) {
            $access = MiniProgramService::getToken($cid);
            $url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token=' . $access;
            $data = [];
            if (isset($param['path']) && $param['path'] != '') $data['jump_wxa']['path'] = $param['path'];
            if (isset($param['query']) && $param['query'] != '') $data['jump_wxa']['query'] = $param['query'];
            $data['is_expire'] = true;
            $data['expire_type'] = 1;
            $data['expire_interval'] = 15;
            $res = HttpService::postRequest($url, $data);
            cache('Scheme_' . $cid,json_decode($res,true));
        }
        return cache('Scheme_' . $cid);
    }
    /**
     * 获取小程序外连
     * @param $cid
     * @return mixed
     */
    public static function getMiniLink($cid,$param)
    {

        if (!cache('link_' . $cid) || true) {

            $access = MiniProgramService::getToken($cid);
            $url = 'https://api.weixin.qq.com/wxa/generate_urllink?access_token=' . $access['access_token'];
            $data = [];
            $data['path'] = $param['path'];
            $data['query'] = $param['query'];
            $data['is_expire'] = false;
            $res = do_request($url,$data,null,true,true);
            cache('link_' . $cid,json_decode($res,true));
        }
        return cache('link_' . $cid);
    }


}
