GC0202 /
ThinkLibrary
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DtApp\ThinkLibrary\service; |
||
| 4 | |||
| 5 | use DtApp\ThinkLibrary\Service; |
||
| 6 | use DtApp\ThinkLibrary\service\curl\HttpService; |
||
| 7 | use think\App; |
||
| 8 | use think\exception\HttpException; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * 卡商网 |
||
| 12 | * http://www.kashangwl.com/ |
||
| 13 | * Class KaShAngWl |
||
| 14 | * @package DtApp\ThinkLibrary\service |
||
| 15 | */ |
||
| 16 | class KaShAngWl extends Service |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * 接口地址 |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $api_url = 'http://www.kashangwl.com/api'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * 商家编号、商家密钥 |
||
| 26 | * @var |
||
| 27 | */ |
||
| 28 | private $customer_id, $customer_key; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * 设置商家编号 |
||
| 32 | * @param string $customer_id |
||
| 33 | * @return $this |
||
| 34 | */ |
||
| 35 | public function setCustomerId(string $customer_id): self |
||
| 36 | { |
||
| 37 | $this->customer_id = $customer_id; |
||
| 38 | return $this; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * 设置商家密钥 |
||
| 43 | * @param string $customer_key |
||
| 44 | * @return $this |
||
| 45 | */ |
||
| 46 | public function setCustomerKey(string $customer_key): self |
||
| 47 | { |
||
| 48 | $this->customer_key = $customer_key; |
||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 待请求的链接 |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $method = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 设置接口 |
||
| 60 | * @param $method |
||
| 61 | * @return KaShAngWl |
||
| 62 | */ |
||
| 63 | public function setMethod($method): self |
||
| 64 | { |
||
| 65 | $this->method = "{$this->api_url}/$method"; |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * 需要发送的的参数 |
||
| 71 | * @var |
||
| 72 | */ |
||
| 73 | private $param; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * 入参 |
||
| 77 | * @param $param |
||
| 78 | * @return KaShAngWl |
||
| 79 | */ |
||
| 80 | public function param($param): self |
||
| 81 | { |
||
| 82 | $this->param = $param; |
||
| 83 | return $this; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * 响应内容 |
||
| 88 | * @var |
||
| 89 | */ |
||
| 90 | private $output; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * 时间戳 |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | private $time; |
||
| 97 | |||
| 98 | public function __construct(App $app) |
||
| 99 | { |
||
| 100 | $this->time = time(); |
||
| 101 | parent::__construct($app); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array|mixed |
||
| 106 | */ |
||
| 107 | public function toArray() |
||
| 108 | { |
||
| 109 | //首先检测是否支持curl |
||
| 110 | if (!extension_loaded("curl")) { |
||
| 111 | throw new HttpException(404, '请开启curl模块!'); |
||
| 112 | } |
||
| 113 | $this->http(); |
||
| 114 | // 正常 |
||
| 115 | if (is_array($this->output)) { |
||
| 116 | return $this->output; |
||
| 117 | } |
||
| 118 | if (is_object($this->output)) { |
||
| 119 | $this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE); |
||
| 120 | } |
||
| 121 | $this->output = json_decode($this->output, true); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 122 | return $this->output; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * 网络请求 |
||
| 127 | */ |
||
| 128 | private function http(): void |
||
| 129 | { |
||
| 130 | //生成签名 |
||
| 131 | $sign = $this->createSign(); |
||
| 132 | //组织参数 |
||
| 133 | $this->param['customer_id'] = $this->customer_id; |
||
| 134 | $this->param['timestamp'] = $this->time; |
||
| 135 | $this->param['sign'] = $sign; |
||
| 136 | $result = HttpService::instance() |
||
| 137 | ->url($this->method) |
||
| 138 | ->data($this->param) |
||
| 139 | ->post() |
||
| 140 | ->toArray(); |
||
| 141 | $this->output = $result; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * 签名 |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | private function createSign(): string |
||
| 149 | { |
||
| 150 | $sign = $this->customer_key; |
||
| 151 | $this->param['customer_id'] = $this->customer_id; |
||
| 152 | $this->param['timestamp'] = $this->time; |
||
| 153 | ksort($this->param); |
||
| 154 | foreach ($this->param as $key => $val) { |
||
| 155 | if ($key !== '' && $val !== '') { |
||
| 156 | $sign .= $key . $val; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | $sign = strtolower(md5($sign)); |
||
| 160 | return $sign; |
||
| 161 | } |
||
| 162 | } |