<?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 extends Service
{
private $api, $method = '';
private $userid, $pwd, $key = '';
private $param;
private $output;
public function api(string $api): self
{
$this->api = $api;
return $this;
}
public function userid(string $userid): self
{
$this->userid = $userid;
return $this;
}
public function pwd(string $pwd): self
{
$this->pwd = $pwd;
return $this;
}
public function key(string $key): self
{
$this->key = $key;
return $this;
}
public function chongZhi(string $orderid, int $face, string $account, int $amount = 1): self
{
$this->method = 'chongzhi_jkorders';
$this->param = "userid={$this->userid}&pwd={$this->pwd}&orderid={$orderid}&face={$face}&account={$account}&amount={$amount}";
return $this;
}
public function query(string $orderid): self
{
$this->method = 'query_jkorders';
$this->param = "userid={$this->userid}&pwd={$this->pwd}&orderid={$orderid}";
return $this;
}
public function money(): self
{
$this->method = 'money_jkuser';
$this->param = "userid={$this->userid}&pwd={$this->pwd}";
return $this;
}
public function txchongzhi(string $orderid, string $account, int $productid, int $amount, string $ip, string $times): self
{
$this->method = 'txchongzhi';
$this->param = "userid={$this->userid}&pwd={$this->pwd}&orderid={$orderid}&account={$account}&productid={$productid}&amount={$amount}&ip={$ip}×={$times}";
return $this;
}
public function queryTXproduct(): self
{
$this->method = 'queryTXproduct';
$this->param = "userid={$this->userid}&pwd={$this->pwd}";
return $this;
}
public function gprsChongzhiAdvance(string $orderid, string $account, int $gprs, int $area, int $effecttime, int $validity, string $times): self
{
$this->method = 'gprsChongzhiAdvance';
$this->param = "userid={$this->userid}&pwd={$this->pwd}&orderid={$orderid}&account={$account}&gprs={$gprs}&area={$area}&effecttime={$effecttime}&validity={$validity}×={$times}";
return $this;
}
public function checkCost(string $orderid): self
{
$this->method = 'checkCost';
$this->param = "userid={$this->userid}&pwd={$this->pwd}&orderid={$orderid}";
return $this;
}
public function toArray()
{
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;
}
private function http(): void
{
$sign = $this->createSign();
$this->param .= '&userkey=' . $sign;
$url = "http://" . $this->api . "/" . $this->method . ".do?{$this->param}";
$result = file_get_contents($url);
$result = Xmls::toArray($result);
$this->output = $result;
}
private function createSign(): string
{
$sign = str_replace(array("&", "="), array("", ""), $this->param);
$sign .= $this->key;
$sign = strtoupper(md5($sign));
return $sign;
}
}