Completed
Pull Request — master (#1414)
by milkmeowo
04:39
created

Client::getPreAuthCode()   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 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[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 EasyWeChat\OpenWork\Corp;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\ServiceContainer;
16
17
/**
18
 * Client.
19
 *
20
 * @author xiaomin <[email protected]>
21
 */
22
class Client extends BaseClient
23
{
24
    /**
25
     * Client constructor.
26
     * 三方接口有三个access_token,这里用的是suite_access_token.
27
     *
28
     * @param \EasyWeChat\Kernel\ServiceContainer $app
29
     */
30 1
    public function __construct(ServiceContainer $app)
31
    {
32 1
        parent::__construct($app, $app['suite_access_token']);
33 1
    }
34
35
    /**
36
     * 企业微信安装应用授权 url.
37
     *
38
     * @param string $preAuthCode 预授权码
39
     * @param string $redirectUri 回调地址
40
     * @param string $state
41
     *
42
     * @return string
43
     *
44
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
45
     */
46
    public function getPreAuthorizationUrl(string $preAuthCode = '', string $redirectUri = '', string $state = '')
47
    {
48
        $redirectUri || $redirectUri = $this->app->config['redirect_uri_install'];
49
        $preAuthCode || $preAuthCode = $this->getPreAuthCode()['pre_auth_code'];
50
        $state || $state = rand();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51
52
        $params = [
53
            'suite_id' => $this->app['config']['suite_id'],
54
            'redirect_uri' => $redirectUri,
55
            'pre_auth_code' => $preAuthCode,
56
            'state' => $state,
57
        ];
58
59
        return 'https://open.work.weixin.qq.com/3rdapp/install?'.http_build_query($params);
60
    }
61
62
    /**
63
     * 获取预授权码.
64
     *
65
     * @return mixed
66
     *
67
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
68
     */
69
    public function getPreAuthCode()
70
    {
71
        return $this->httpGet('cgi-bin/service/get_pre_auth_code');
72
    }
73
74
    /**
75
     * 设置授权配置.
76
     * 该接口可对某次授权进行配置.
77
     *
78
     * @param string $preAuthCode
79
     * @param array  $sessionInfo
80
     *
81
     * @return mixed
82
     *
83
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
84
     */
85
    public function setSession(string $preAuthCode, array $sessionInfo)
86
    {
87
        $params = [
88
            'pre_auth_code' => $preAuthCode,
89
            'session_info' => $sessionInfo,
90
        ];
91
92
        return $this->httpPostJson('cgi-bin/service/set_session_info', $params);
93
    }
94
95
    /**
96
     * 获取企业永久授权码.
97
     *
98
     * @param string $authCode 临时授权码,会在授权成功时附加在redirect_uri中跳转回第三方服务商网站,或通过回调推送给服务商。长度为64至512个字节
99
     *
100
     * @return mixed
101
     *
102
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
103
     */
104
    public function getPermanentByCode(string $authCode)
105
    {
106
        $params = [
107
            'auth_code' => $authCode,
108
        ];
109
110
        return $this->httpPostJson('cgi-bin/service/get_permanent_code', $params);
111
    }
112
113
    /**
114
     * 获取企业授权信息.
115
     *
116
     * @param string $authCorpId
117
     * @param string $permanentCode
118
     *
119
     * @return mixed
120
     *
121
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
122
     */
123
    public function getAuthorization(string $authCorpId, string $permanentCode)
124
    {
125
        $params = [
126
            'auth_corpid' => $authCorpId,
127
            'permanent_code' => $permanentCode,
128
        ];
129
130
        return $this->httpPostJson('cgi-bin/service/get_auth_info', $params);
131
    }
132
133
    /**
134
     * 获取应用的管理员列表.
135
     *
136
     * @param string $authCorpId
137
     * @param string $agentId
138
     *
139
     * @return mixed
140
     *
141
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
142
     */
143
    public function getManagers(string $authCorpId, string $agentId)
144
    {
145
        $params = [
146
            'auth_corpid' => $authCorpId,
147
            'agentid' => $agentId,
148
        ];
149
150
        return $this->httpPostJson('cgi-bin/service/get_admin_lis', $params);
151
    }
152
153
    /**
154
     * 获取登录url.
155
     *
156
     * @param string      $redirectUri
157
     * @param string      $scope
158
     * @param string|null $state
159
     *
160
     * @return string
161
     */
162
    public function getOAuthRedirectUrl(string $redirectUri = '', string $scope = 'snsapi_userinfo', string $state = null)
163
    {
164
        $redirectUri || $redirectUri = $this->app->config['redirect_uri_oauth'];
165
        $state || $state = rand();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
166
        $params = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 22 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
167
            'appid' => $this->app['config']['suite_id'],
168
            'redirect_uri' => $redirectUri,
169
            'response_type' => 'code',
170
            'scope' => $scope,
171
            'state' => $state,
172
        ];
173
174
        return 'https://open.weixin.qq.com/connect/oauth2/authorize?'.http_build_query($params).'#wechat_redirect';
175
    }
176
177
    /**
178
     * 第三方根据code获取企业成员信息.
179
     *
180
     * @param string $code
181
     *
182
     * @return mixed
183
     *
184
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
185
     */
186
    public function getUserByCode(string $code)
187
    {
188
        $params = [
189
            'code' => $code,
190
        ];
191
192
        return $this->httpGet('cgi-bin/service/getuserinfo3rd', $params);
193
    }
194
195
    /**
196
     * 第三方使用user_ticket获取成员详情.
197
     *
198
     * @param string $userTicket
199
     *
200
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
201
     *
202
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
203
     */
204
    public function getUserByTicket(string $userTicket)
205
    {
206
        $params = [
207
            'user_ticket' => $userTicket,
208
        ];
209
210
        return $this->httpPostJson('cgi-bin/service/getuserdetail3rd', $params);
211
    }
212
}
213