Completed
Push — master ( e74dfe...64c38c )
by do
03:09
created

Client::ask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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