Completed
Push — master ( dd9eb5...fbd863 )
by phper
02:11
created

QQYunAgent::singlesSend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
1
<?php
2
3
namespace Laravelsms\Sms\Agents;
4
5
use Laravelsms\Sms\Contracts\Sms;
6
use Illuminate\Support\Arr;
7
8
class QQYunAgent extends Sms
9
{
10
    private $config = [];
11
12
    private $appId;
13
    private $appKey;
14
    private $strRand;
15
16
    private $host = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms';
17
18
    public function __construct($config)
19
    {
20
        $this->config = $config;
21
        $this->transformConfig();
22
    }
23
24 View Code Duplication
    protected function transformConfig()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        $credentials = Arr::pull($this->config, 'credentials');
27
        $this->appId = Arr::pull($credentials, 'appId');
28
        $this->appKey = Arr::pull($credentials, 'appKey');
29
        $this->setSignName();
30
        $this->setTemplateId(Arr::pull($this->config, 'templateId'));
31
        $this->strRand = $this->makeRandom();
32
    }
33
34
    private function computeSignature($parameters)
35
    {
36
        $canonicalizedQueryString = '';
37
        foreach ($parameters as $key => $value) {
38
            $canonicalizedQueryString .= '&' . $key . '=' . $value;
39
        }
40
41
        return hash("sha256", "appkey=" . $this->appKey . $canonicalizedQueryString);
42
    }
43
44
    private function formatMobile($phoneNumber)
45
    {
46
        if (is_array($phoneNumber)) {
47
            $to = array(
48
                'nationcode' => $phoneNumber[0],
49
                'mobile' => $phoneNumber[1]
50
            );
51
        } else {
52
            $to = array(
53
                'nationcode' => '86',
54
                'mobile' => $phoneNumber
55
            );
56
        }
57
58
        return $to;
59
    }
60
61
    public function singlesSend($mobile, $send = true)
62
    {
63
64
        $mobile = $this->formatMobile($mobile);
65
66
        $url = "{$this->host}?sdkappid={$this->appId}&random={$this->strRand}";
67
68
        $requestParams = array(
69
            'tel' => $mobile,
70
            'sign' => $this->signName,
71
            'tpl_id' => (int)($this->templateId),
72
            'params' => $this->templateVar
73
        );
74
75
        if ($send) {
76
            return $this->curl($url, $requestParams);
77
        }
78
79
        return $requestParams;
80
    }
81
82
    /**
83
     * @param $url
84
     * @param array $requestParams
85
     * @return array $result
86
     * @return int $result[].code 返回0则成功,返回其它则错误
87
     * @return string $result[].msg 返回消息
88
     * @return mixed $result[].verifyCode 验证码
89
     */
90
    protected function curl($url, $requestParams)
91
    {
92
        $timestamp = time();
93
94
        $publicParams = array(
95
            'time' => $timestamp,
96
            'extend' => '',
97
            'ext' => ''
98
        );
99
100
        $parameters = array(
101
            'random' => $this->strRand,
102
            'time' => $timestamp,
103
            'mobile' => $requestParams['tel']['mobile']
104
        );
105
106
        $signature = $this->computeSignature($parameters);
107
        $publicParams = array_merge(['sig' => $signature], $publicParams);
108
        $publicParams = array_merge($requestParams, $publicParams);
109
110
        $ch = curl_init();
111
112
        curl_setopt($ch, CURLOPT_URL, $url);
113
        curl_setopt($ch, CURLOPT_HEADER, 0);
114
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
115
        curl_setopt($ch, CURLOPT_POST, 1);
116
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($publicParams));
117
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
118
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
119
        $httpResponse = $this->httpResponse($ch);
120
        $result = $this->transformerResponse($httpResponse);
121
        curl_close($ch);
122
123
        return $result;
124
    }
125
126
    protected function transformerResponse($httpResponse)
127
    {
128
        if (empty($httpResponse['error'])) {
129
            $response = array_except(
130
                json_decode($httpResponse['jsonData'], true),
131
                ['ext', 'sid', 'fee']
132
            );
133
            if ($response['result'] === 0) {
134
                $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode];
135
            } else {
136
                $errmsg = 'result; ' . $response['result'] . ', errmsg: ' . $response['errmsg'];
137
                $result = ['code' => time(), 'msg' => $errmsg];
138
            }
139
            unset($response);
140
        } else {
141
            $result = ['code' => time(), 'msg' => $httpResponse['error']];
142
        }
143
144
        return $result;
145
    }
146
}
147