Completed
Pull Request — master (#59)
by
unknown
03:43 queued 01:23
created

AlidayuAgent::genResult()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 15
rs 8.8571
cc 6
eloc 11
nc 4
nop 2
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class AlidayuAgent
7
 *
8
 * @property string $appKey
9
 * @property string $secretKey
10
 * @property string $smsFreeSignName
11
 */
12
class AlidayuAgent extends Agent
13
{
14
    public function sendSms($tempId, $to, array $data, $content)
15
    {
16
        $this->sendTemplateSms($tempId, $to, $data);
17
    }
18
19
    public function sendContentSms($to, $content)
20
    {
21
    }
22
23
    public function sendTemplateSms($tempId, $to, array $data)
24
    {
25
        $sendUrl = 'https://eco.taobao.com/router/rest';
26
        $params = [
27
            'app_key'            => $this->appKey,
28
            'v'                  => '2.0',
29
            'format'             => 'json',
30
            'sign_method'        => 'md5',
31
            'method'             => 'alibaba.aliqin.fc.sms.num.send',
32
            'timestamp'          => date('Y-m-d H:i:s'),
33
            'sms_type'           => 'normal',
34
            'sms_free_sign_name' => $this->smsFreeSignName,
35
            'sms_param'          => json_encode($data),
36
            'rec_num'            => $to,
37
            'sms_template_code'  => $tempId,
38
        ];
39
        $params['sign'] = $this->generateSign($params);
40
        $result = $this->curl($sendUrl, $params, true);
41
        $this->genResult($result, 'alibaba_aliqin_fc_sms_num_send_response');
42
    }
43
44
    public function voiceVerify($to, $code)
45
    {
46
        // remain discussion
47
    }
48
49
    public function genResult($result, $callbackName)
50
    {
51
        if ($result['response']) {
52
            $result = json_decode($result['response'], true);
53
            if (isset($result[$callbackName]['result']) && $result[$callbackName]['result']['err_code'] == '0') {
54
                $this->result['success'] = true;
55
                return;
56
            } elseif (isset($result['error_response'])) {
57
                $this->result['info'] = $result['error_response']['msg'] . '|sub_msg:' . $result['error_response']['sub_msg'] . '|result:' . json_encode($result ?: '');
58
                $this->result['code'] = $result['error_response']['code'] . '_' . $result['error_response']['sub_code'];
59
                return;
60
            }
61
        }
62
        $this->result['info'] = '请求失败';
63
    }
64
65
    protected function generateSign($params)
66
    {
67
        ksort($params);
68
69
        $stringToBeSigned = $this->secretKey;
70
        foreach ($params as $k => $v) {
71
            if (is_string($v) && '@' !== substr($v, 0, 1)) {
72
                $stringToBeSigned .= "$k$v";
73
            }
74
        }
75
        unset($k, $v);
76
        $stringToBeSigned .= $this->secretKey;
77
78
        return strtoupper(md5($stringToBeSigned));
79
    }
80
}
81