Completed
Pull Request — master (#1275)
by
unknown
03:05
created

Client::getApp3rdInstallUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace EasyWeChat\OpenWork\Corp;
4
5
6
use EasyWeChat\Kernel\BaseClient;
7
use EasyWeChat\Kernel\ServiceContainer;
8
9
class Client extends BaseClient
10
{
11
12
    public function __construct(ServiceContainer $app)
13
    {
14
        parent::__construct($app, $app['suite_access_token']);
15
    }
16
17
    /**
18
     * 企业微信应用授权 url
19
     * @param string $pre_auth_code
20
     * @param string $redirect_uri
21
     * @param string $state
22
     * @return string
23
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
24
     */
25
    public function getApp3rdInstallUrl(string $redirect_uri, string $state = '')
26
    {
27
        $params = [
28
            'suite_id'      => $this->app['config']['suite_id'],
29
            'redirect_uri'  => urlencode($redirect_uri),
30
            'pre_auth_code' => $this->getPreAuthCode()['pre_auth_code'],
31
            'state'         => $state || rand()
32
        ];
33
        return 'https://open.work.weixin.qq.com/3rdapp/install?' . http_build_query($params);
34
    }
35
36
37
    /**
38
     * 获取预授权码
39
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
40
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
41
     */
42
    public function getPreAuthCode()
43
    {
44
        return $this->httpGet('cgi-bin/service/get_pre_auth_code');
45
    }
46
47
    /**
48
     * 设置授权配置
49
     * 该接口可对某次授权进行配置
50
     * @param array $data
51
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
52
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
53
     */
54
    public function setSessionInfo(array $data)
55
    {
56
        return $this->httpPostJson('cgi-bin/service/set_session_info', compact('data'));
57
    }
58
59
    /**
60
     * 获取企业永久授权码
61
     * @param string $auth_code 临时授权码,会在授权成功时附加在redirect_uri中跳转回第三方服务商网站,或通过回调推送给服务商。长度为64至512个字节
62
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
63
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
64
     */
65
    public function getPermanentCode(string $auth_code)
66
    {
67
        $params = [
68
            'auth_code' => $auth_code
69
        ];
70
        return $this->httpPostJson('cgi-bin/service/set_session_info', $params);
71
    }
72
73
    /**
74
     * 获取企业授权信息
75
     * @param string $auth_corpid
76
     * @param string $permanent_code
77
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
78
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
79
     */
80
    public function getAuthInfo(string $auth_corpid, string $permanent_code)
81
    {
82
        $params = [
83
            'auth_corpid'    => $auth_corpid,
84
            'permanent_code' => $permanent_code
85
        ];
86
        return $this->httpPostJson('cgi-bin/service/get_auth_info', $params);
87
    }
88
89
    /**
90
     * 获取应用的管理员列表
91
     * @param string $auth_corpid
92
     * @param string $agentid
93
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
94
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
95
     */
96
    public function getAdminList(string $auth_corpid, string $agentid)
97
    {
98
        $params = [
99
            'auth_corpid' => $auth_corpid,
100
            'agentid'     => $agentid
101
        ];
102
        return $this->httpPostJson('cgi-bin/service/get_admin_lis', $params);
103
    }
104
105
    /**
106
     * 获取登录url
107
     * @param string $redirect_uri
108
     * @param string $scope
109
     * @param string|null $state
110
     * @return string
111
     */
112
    public function getOauth2Uri(string $redirect_uri, string $scope = 'snsapi_userinfo', string $state = null)
113
    {
114
        $params = [
115
            'appid'         => $this->app['config']['suite_id'],
116
            'redirect_uri'  => urlencode($redirect_uri),
117
            'response_type' => 'code',
118
            'scope'         => $scope,
119
            'state'         => $state || rand()
120
        ];
121
        return 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($params) . '#wechat_redirect';
122
    }
123
124
125
    /**
126
     * 第三方根据code获取企业成员信息
127
     * @param string $code
128
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
129
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
130
     */
131
    public function getUserInfo3rd(string $code)
132
    {
133
        $params = [
134
            'code' => $code,
135
            'access_token' => $this->app['suite_access_token']->getToken()['suite_access_token']
136
        ];
137
        return $this->httpPostJson('cgi-bin/service/getuserinfo3rd', $params);
138
    }
139
140
    /**
141
     * 第三方使用user_ticket获取成员详情
142
     * @param string $user_ticket
143
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
144
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
145
     */
146
    public function getUserDetail3rd(string $user_ticket)
147
    {
148
        $params = [
149
            'user_ticket' => $user_ticket,
150
            'access_token' => $this->app['suite_access_token']->getToken()['suite_access_token']
151
        ];
152
        return $this->httpPostJson('cgi-bin/service/getuserdetail3rd', $params);
153
    }
154
155
}