Completed
Push — master ( 64c38c...9b5c24 )
by do
06:05 queued 03:07
created

Client::getQrCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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\Login;
13
14
use JinWeChat\Kernel\BaseClient;
15
16
class Client extends BaseClient
17
{
18
    /**
19
     * 初始化Cookies.
20
     *
21
     * @return \Psr\Http\Message\ResponseInterface
22
     *
23
     * @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException
24
     */
25
    public function init()
26
    {
27
        return $this->httpGet('');
28
    }
29
30
    /**
31
     * 发送登陆请求
32
     *
33
     * @return \Psr\Http\Message\ResponseInterface
34
     *
35
     * @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException
36
     */
37
    public function startLogin()
38
    {
39
        $this->init();
40
        $params = [
41
            'username' => $this->app['config']['username'],
42
            'pwd' => md5($this->app['config']['password']),
43
            'imgcode' => '',
44
            'f' => 'json',
45
            'userlang' => 'zh_CN',
46
            'token' => '',
47
            'lang' => 'zh_CN',
48
            'ajax' => '1',
49
        ];
50
51
        return $this->httpPost('cgi-bin/bizlogin?action=startlogin', $params);
52
    }
53
54
    /**
55
     * 请求调转链接,根据不同的链接判断公众号状态
56
     *
57
     * @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException
58
     */
59
    public function redirect()
60
    {
61
        $url = 'cgi-bin/bizlogin?action=validate&lang=zh_CN&account='.$this->app['config']['username'];
62
        $this->httpGet($url);
63
    }
64
65
    /**
66
     * 保存QrCode到指定文件.
67
     *
68
     * @return \Psr\Http\Message\ResponseInterface
69
     *
70
     * @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException
71
     */
72
    public function getQrCode()
73
    {
74
        return $this->httpGet('cgi-bin/loginqrcode?action=getqrcode&param=4300&rd=855');
75
    }
76
77
    /**
78
     * 获取二维码扫码确认状态
79
     *
80
     * @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException
81
     */
82
    public function ask()
83
    {
84
        $url = 'cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1';
85
        $res = $this->httpGet($url);
86
87
        return $this->judge($res);
88
    }
89
90
    /**
91
     * 判断方法.
92
     *
93
     * @param $res
94
     *
95
     * @return bool
96
     *
97
     * @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException
98
     */
99
    public function judge($res)
100
    {
101
        if (isset($res->status)) {
102
            switch ($res->status) {
103
                case 0://系统繁忙,请稍后再试
104
                    return false;
105
                case 1://未扫码
106
                    $this->ask();
107
108
                    break;
109
                case 2://已取消
110
                    return false;
111
112
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
113
                case 3://超时
114
                    return false;
115
116
                    break;
117
                case 4://已确认
118
                    return true;
119
120
                    break;
121
            }
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * 扫码确认后跳转请求跳转地址
129
     *
130
     * @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException
131
     */
132
    public function bizlogin()
133
    {
134
        $url = 'https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login';
135
        $this->httpGet($url);
136
    }
137
}
138