EJiAoFei.php 5.2 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
<?php

namespace DtApp\ThinkLibrary\service\decent;

use DtApp\ThinkLibrary\exception\DtaException;
use DtApp\ThinkLibrary\facade\Xmls;
use DtApp\ThinkLibrary\Service;
use think\exception\HttpException;

/**
 * 缴费平台
 * Class EJiAoFei
 * @package DtApp\ThinkLibrary\service\decent
 */
class EJiAoFei extends Service
{
    /**
     * 待请求的链接
     * @var string
     */
    private $api, $method = '';

    /**
     * 由鼎信商务提供
     * @var
     */
    private $userid, $pwd, $key = '';

    /**
     * 需要发送的的参数
     * @var
     */
    private $param;

    /**
     * 响应内容
     * @var
     */
    private $output;

    /**
     * ip:端口
     * @param string $api
     * @return $this
     */
    public function api(string $api): self
    {
        $this->api = $api;
        return $this;
    }

    /**
     * 由鼎信商务提供
     * @param string $userid
     * @return $this
     */
    public function userid(string $userid): self
    {
        $this->userid = $userid;
        return $this;
    }

    /**
     * 由鼎信商务提供
     * @param string $pwd
     * @return $this
     */
    public function pwd(string $pwd): self
    {
        $this->pwd = $pwd;
        return $this;
    }

    /**
     * 由鼎信商务提供
     * @param string $key
     * @return $this
     */
    public function key(string $key): self
    {
        $this->key = $key;
        return $this;
    }

    /**
     * 请求参数
     * @param array $param
     * @return $this
     */
    public function param(array $param): self
    {
        $this->param = $param;
        return $this;
    }

    /**
     * 话费充值
     * @return $this
     */
    public function chongZhi(): self
    {
        $this->method = 'chongzhi_jkorders';
        return $this;
    }

    /**
     * 通用查询
     * @return $this
     */
    public function query(): self
    {
        $this->method = 'query_jkorders';
        return $this;
    }

    /**
     * 用户余额查询
     * @return $this
     */
    public function money(): self
    {
        $this->method = 'money_jkuser';
        return $this;
    }

    /**
     * 腾讯充值
     * @return $this
     */
    public function txchongzhi(): self
    {
        $this->method = 'txchongzhi';
        return $this;
    }

    /**
     * 可充值腾讯产品查询
     * @return $this
     */
    public function queryTXproduct(): self
    {
        $this->method = 'queryTXproduct';
        return $this;
    }

    /**
     * 流量充值
     * @return $this
     */
    public function gprsChongzhiAdvance(): self
    {
        $this->method = 'queryTXproduct';
        return $this;
    }

    /**
     * 会员订单成本价查询
     * @return $this
     */
    public function checkCost(): self
    {
        $this->method = 'checkCost';
        return $this;
    }

    /**
     * @throws DtaException
     */
    public function toArray()
    {
        //首先检测是否支持curl
        if (!extension_loaded("curl")) {
            throw new HttpException(404, '请开启curl模块!');
        }
        if (empty($this->api)) {
            throw new DtaException('请检查api参数');
        }
        $this->http();
        // 正常
        if (is_array($this->output)) {
            return $this->output;
        };
        if (is_object($this->output)) {
            $this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE);
        }
        $this->output = json_decode($this->output, true);
        return $this->output;
    }

    /**
     * 网络请求
     * @throws DtaException
     */
    private function http(): void
    {
        //生成签名
        $sign = $this->createSign();
        //组织参数
        $strParam = $this->createStrParam();
        $strParam .= '&userkey=' . $sign;
        $url = "http://" . $this->api . "/" . $this->method . ".do?{$strParam}";
        $result = file_get_contents($url);
        $result = Xmls::toArray($result);
        $this->output = $result;
    }

    /**
     * 签名
     * @return string
     * @throws DtaException
     */
    private function createSign(): string
    {
        if (empty($this->key)) {
            throw new DtaException('请检查key参数');
        }
        if (empty($this->userid)) {
            throw new DtaException('请检查userid参数');
        }
        if (empty($this->pwd)) {
            throw new DtaException('请检查pwd参数');
        }
        $this->param['userid'] = $this->userid;
        $this->param['pwd'] = $this->pwd;
        $sign = "userid{$this->userid}pwd{$this->pwd}";
        ksort($this->param);
        foreach ($this->param as $key => $val) {
            if ($key !== '' && $val !== '') {
                $sign .= $key . $val;
            }
        }
        $sign .= $this->key;
        $sign = strtoupper(md5($sign));
        return $sign;
    }

    /**
     * 组参
     * @return string
     */
    private function createStrParam(): string
    {
        $strParam = '';
        foreach ($this->param as $key => $val) {
            if ($key !== '' && $val !== '') {
                $strParam .= $key . '=' . urlencode($val) . '&';
            }
        }
        return substr($strParam, 0, -1);
    }
}