<?php
namespace DtApp\ThinkLibrary\service\qiniu;
use DtApp\ThinkLibrary\exception\DtaException;
use DtApp\ThinkLibrary\Service;
use Qiniu\Auth;
use Qiniu\Http\Client;
use think\exception\HttpException;
class SmsService extends Service
{
private $accessKey, $secretKey, $url, $method;
private $param = [];
private $output;
private $contentType = "application/json";
public function accessKey(string $accessKey): self
{
$this->accessKey = $accessKey;
return $this;
}
public function secretKey(string $secretKey): self
{
$this->secretKey = $secretKey;
return $this;
}
public function param(array $param): self
{
$this->param = $param;
return $this;
}
public function message(): self
{
$this->url = "https://sms.qiniuapi.com/v1/message";
$this->method = "POST";
return $this;
}
public function messageSingle(): self
{
$this->url = "https://sms.qiniuapi.com/v1/message/single";
$this->method = "POST";
return $this;
}
public function messageOversea(): self
{
$this->url = "https://sms.qiniuapi.com/v1/message/oversea";
$this->method = "POST";
return $this;
}
public function messageFulltext(): self
{
$this->url = "https://sms.qiniuapi.com/v1/message/fulltext";
$this->method = "POST";
return $this;
}
public function messages(): self
{
$this->url = "https://sms.qiniuapi.com/v1/messages";
$this->method = "GET";
return $this;
}
public function signature(): self
{
$this->url = "https://sms.qiniuapi.com/v1/signature";
$this->method = "POST";
return $this;
}
public function signatureQuery(): self
{
$this->url = "https://sms.qiniuapi.com/v1/signature";
$this->method = "GET";
$this->contentType = "application/x-www-form-urlencoded";
return $this;
}
public function signatureQueryId($id): self
{
if (empty($id)) {
throw new DtaException('请检查id参数');
}
$this->url = "https://sms.qiniuapi.com/v1/signature/{$id}";
$this->method = "GET";
return $this;
}
public function signatureEditId($id): self
{
if (empty($id)) {
throw new DtaException('请检查id参数');
}
$this->url = "https://sms.qiniuapi.com/v1/signature/{$id}";
$this->method = "PUT";
return $this;
}
public function signatureDelId($id): self
{
if (empty($id)) {
throw new DtaException('请检查id参数');
}
$this->url = "https://sms.qiniuapi.com/v1/signature/{$id}";
$this->method = "DELETE";
return $this;
}
public function template(): self
{
$this->url = "https://sms.qiniuapi.com/v1/template";
$this->method = "POST";
return $this;
}
public function templateQuery(): self
{
$this->url = "https://sms.qiniuapi.com/v1/template";
$this->method = "GET";
return $this;
}
public function templateQueryId($id): self
{
if (empty($id)) {
throw new DtaException('请检查id参数');
}
$this->url = "https://sms.qiniuapi.com/v1/template/{$id}";
$this->method = "GET";
return $this;
}
public function templateEditId($id): self
{
if (empty($id)) {
throw new DtaException('请检查id参数');
}
$this->url = "https://sms.qiniuapi.com/v1/template/{$id}";
$this->method = "PUT";
return $this;
}
public function templateDelId($id): self
{
if (empty($id)) {
throw new DtaException('请检查id参数');
}
$this->url = "https://sms.qiniuapi.com/v1/template/{$id}";
$this->method = "DELETE";
return $this;
}
public function toArray()
{
if (!extension_loaded("curl")) {
throw new HttpException(404, '请开启curl模块!');
}
$this->http();
if (is_array($this->output)) {
return $this->output;
}
if (is_object($this->output)) {
$this->output = $this->object2array($this->output);
return $this->output;
}
$this->output = json_decode($this->output, true);
return $this->output;
}
private function object2array(&$object): array
{
if (is_object($object)) {
$arr = (array)($object);
} else {
$arr = &$object;
}
if (is_array($arr)) {
foreach ($arr as $varName => $varValue) {
$arr[$varName] = $this->object2array($varValue);
}
}
return $arr;
}
private function http(): self
{
if (empty($this->accessKey)) {
throw new DtaException('请检查accessKey参数');
}
if (empty($this->secretKey)) {
throw new DtaException('请检查secretKey参数');
}
$headers = [
'Authorization' => $this->authentication(),
'Content-Type' => $this->contentType
];
$auth = new Auth($this->accessKey, $this->secretKey);
$recToken = $auth->authorizationV2($this->url, $this->method, json_encode($this->param), $this->contentType);
$rtcToken['Content-Type'] = $this->contentType;
dump($recToken);
dump($this->url . "?" . http_build_query($this->param));
$ret = Client::get($this->url . "?" . http_build_query($this->param), $recToken);
dump($ret);
$this->output = $ret->json();
return $this;
}
private function authentication()
{
$url = parse_url($this->url);
$data = $url['path'] ?? '';
if (isset($url['query'])) {
$data .= '?' . $url['query'];
}
$data .= "\n";
if (isset($this->param) && $this->contentType === 'application/x-www-form-urlencoded') {
$data .= json_encode($this->param);
}
return $this->sign($data);
}
private function sign($data)
{
$sign = hash_hmac('sha1', $data, $this->secretKey, true);
return "Qiniu " . sprintf('%s:%s', $this->accessKey, $this->encode($sign));
}
private function encode($string)
{
$find = ['+', '/'];
$replace = ['-', '_'];
return str_replace($find, $replace, base64_encode($string));
}
}