Completed
Push — master ( 40113d...1cc7c2 )
by do
03:14
created

Client::getQrCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 21
ccs 0
cts 20
cp 0
crap 2
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the docodeit/wechat.
5
 *
6
 * (c) docodeit <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace JinWeChat\OfficialAccount\Mass;
13
14
use JinWeChat\Kernel\BaseClient;
15
use JinWeChat\Kernel\Console\QrCode;
16
17
class Client extends BaseClient
18
{
19
    /**
20
     * 获取msgid
21
     * 通过页面抓取,正则匹配
22
     * @return string
23
     */
24
    public function msgid()
25
    {
26
        $query = [
27
            'query' => [
28
                't' => 'mass/send',
29
                'lang' => 'zh_CN'
30
            ]
31
        ];
32
        $url = 'cgi-bin/masssendpage';
33
        $res = $this->httpGet($url, $query);
34
        $operation_seq = false;
35
        $matchRes = preg_match('/operation_seq:[\s]+\"(.*?)\"/i', $res, $match);
0 ignored issues
show
Bug introduced by
$res of type Psr\Http\Message\ResponseInterface is incompatible with the type string expected by parameter $subject of preg_match(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $matchRes = preg_match('/operation_seq:[\s]+\"(.*?)\"/i', /** @scrutinizer ignore-type */ $res, $match);
Loading history...
36
        if ($matchRes) {
37
            $operation_seq = $match[1];
38
        }
39
        return $operation_seq;
40
    }
41
42
    /**
43
     * 获取ticket
44
     */
45
    public function ticket()
46
    {
47
        $options = [
48
            'token' => $this->token,
49
            'lang' => 'zh_CN',
50
            'f' => 'json',
51
            'ajax' => '1',
52
            'random' => $this->getMillisecond(),
53
            'action' => 'get_ticket'
54
        ];
55
        $url = 'misc/safeassistant?lang=zh_CN';
56
        $res = $this->httpPost($url, $options);
57
        $ticket = false;
58
        if ($res) {
59
            if ($json = $this->jsonDecode($res)) {
60
                $ticket = $json->ticket;
61
            }
62
        }
63
        return $ticket;
64
    }
65
66
    /**
67
     * 获取uuid
68
     * @param $ticket
69
     * @return string|bool
70
     */
71
    public function uuid($ticket)
72
    {
73
        $options = [
74
            'token' => $this->token,
75
            'lang' => 'zh_CN',
76
            'f' => 'json',
77
            'ajax' => '1',
78
            'random' => $this->getMillisecond(),
79
            'state' => '0',
80
            'login_type' => 'safe_center',
81
            'type' => 'json',
82
            'ticket' => $ticket
83
        ];
84
        $uuid = false;
85
        $url = 'safe/safeqrconnect?lang=zh_CN';
86
        $res = $this->httpPost($url, $options);
87
        if ($res) {
88
            if ($json = $this->jsonDecode($res)) {
89
                $uuid = $json->uuid;
90
            }
91
        }
92
        return $uuid;
93
    }
94
95
    /**
96
     * 获取二维码图片
97
     */
98
    public function getQrCode()
99
    {
100
        $msgid = $this->msgid();
101
        $ticket = $this->ticket();
102
        $uuid = $this->uuid($ticket);
103
        $query = [
104
            'query' => [
105
                'ticket' => $ticket,
106
                'uuid' => $uuid,
107
                'action' => 'check',
108
                'type' => 'msgs',
109
                'msgid' => $msgid,
110
            ]
111
        ];
112
        $text = "https://mp.weixin.qq.com/safe/safeqrcode?ticket=$ticket&uuid=$uuid&action=check&type=msgs&msgid=$msgid";
0 ignored issues
show
Unused Code introduced by
The assignment to $text is dead and can be removed.
Loading history...
113
        $qr = new QrCode();
114
//        $qr->show($text);
115
        $qr->show('https://login.weixin.qq.com/qrcode/wYHHLQ9y5A==');
116
        $url = 'safe/safeqrcode';
117
        $res = $this->httpGet($url, $query);
118
        file_put_contents('qr.jpg', $res);
119
    }
120
121
    /**
122
     * 检查二维码是否扫码
123
     * @param $uuid
124
     * @param $token
125
     */
126
    public function checkScan($token, $uuid)
127
    {
128
        $time = $this->getMillisecond();
129
        $options = [
130
            'token' => $token,
131
            'lang' => 'zh_CN',
132
            'f' => 'json',
133
            'ajax' => '1',
134
            'random' => $time,
135
            'uuid' => $uuid,
136
            'action' => 'json',
137
            'type' => 'json'
138
        ];
139
        $url = 'safe/safeuuid?timespam=' . $time . '&lang=zh_CN';
140
        $res = $this->httpPost($url, $options);
141
        if ($res) {
142
            var_dump($res);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($res) looks like debug code. Are you sure you do not want to remove it?
Loading history...
143
        }
144
    }
145
}
146