Test Failed
Push — master ( daed3f...0a62d9 )
by Carlos
03:35 queued 35s
created

Staff::messages()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 4
dl 11
loc 11
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
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
/**
13
 * Staff.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @see      https://github.com/overtrue
19
 * @see      http://overtrue.me
20
 */
21
22
namespace EasyWeChat\Staff;
23
24
use EasyWeChat\Core\AbstractAPI;
25
use EasyWeChat\Support\Collection;
26
27
/**
28
 * Class Staff.
29
 */
30
class Staff extends AbstractAPI
31
{
32
    const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist';
33
    const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist';
34
    const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del';
35
    const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update';
36
    const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add';
37
    const API_INVITE_BIND = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker';
38
    const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
39
    const API_AVATAR_UPLOAD = 'https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
40
    const API_RECORDS = 'https://api.weixin.qq.com/customservice/msgrecord/getrecord';
41
    const API_MSG_LIST = 'https://api.weixin.qq.com/customservice/msgrecord/getmsglist';
42
43
    /**
44
     * List all staffs.
45
     *
46
     * @return \EasyWeChat\Support\Collection
47 1
     */
48
    public function lists()
49 1
    {
50
        return $this->parseJSON('get', [self::API_LISTS]);
51
    }
52
53
    /**
54
     * List all online staffs.
55
     *
56
     * @return \EasyWeChat\Support\Collection
57 1
     */
58
    public function onlines()
59 1
    {
60
        return $this->parseJSON('get', [self::API_ONLINE]);
61
    }
62
63
    /**
64
     * Create a staff.
65
     *
66
     * @param string $account
67
     * @param string $nickname
68
     *
69
     * @return \EasyWeChat\Support\Collection
70 1
     */
71 View Code Duplication
    public function create($account, $nickname)
72
    {
73 1
        $params = [
74 1
                   'kf_account' => $account,
75 1
                   'nickname' => $nickname,
76
                  ];
77 1
78
        return $this->parseJSON('json', [self::API_CREATE, $params]);
79
    }
80
81
    /**
82
     * Update a staff.
83
     *
84
     * @param string $account
85
     * @param string $nickname
86
     *
87
     * @return \EasyWeChat\Support\Collection
88 1
     */
89 View Code Duplication
    public function update($account, $nickname)
90
    {
91 1
        $params = [
92 1
                   'kf_account' => $account,
93 1
                   'nickname' => $nickname,
94
                  ];
95 1
96
        return $this->parseJSON('json', [self::API_UPDATE, $params]);
97
    }
98
99
    /**
100
     * Delete a staff.
101
     *
102
     * @param string $account
103
     *
104
     * @return \EasyWeChat\Support\Collection
105
     */
106
    public function delete($account)
107
    {
108
        // XXX: 微信那帮搞技术的都 TM 是 SB,url上的文本居然不 TM urlencode,
109
        // 这里客服账号因为有 @ 符,而微信不接收urlencode的账号。。
110
        // 简直是日了...
111
        // #222
112
        // PS: 如果你是微信做接口的,奉劝你们,尊重技术,不会别乱搞,笨不是你们的错,你们出来坑人就是大错特错。
113
        $accessTokenField = sprintf('%s=%s', $this->accessToken->getQueryName(), $this->accessToken->getToken());
114
        $url = sprintf(self::API_DELETE.'?%s&kf_account=%s', $accessTokenField, $account);
115
116
        $contents = $this->getHttp()->parseJSON(file_get_contents($url));
117
118
        $this->checkAndThrow($contents);
119
120
        return new Collection($contents);
121
    }
122
123
    /**
124
     * Invite a staff.
125
     *
126
     * @param string $account
127
     * @param string $wechatId
128
     *
129
     * @return \EasyWeChat\Support\Collection
130 1
     */
131
    public function invite($account, $wechatId)
132
    {
133 1
        $params = [
134 1
                   'kf_account' => $account,
135 1
                   'invite_wx' => $wechatId,
136
                  ];
137 1
138
        return $this->parseJSON('json', [self::API_INVITE_BIND, $params]);
139
    }
140
141
    /**
142
     * Set staff avatar.
143
     *
144
     * @param string $account
145
     * @param string $path
146
     *
147
     * @return \EasyWeChat\Support\Collection
148 1
     */
149
    public function avatar($account, $path)
150 1
    {
151
        return $this->parseJSON('upload', [self::API_AVATAR_UPLOAD, ['media' => $path], [], ['kf_account' => $account]]);
152
    }
153
154
    /**
155
     * Get message builder.
156
     *
157
     * @param \EasyWeChat\Message\AbstractMessage|string $message
158
     *
159
     * @return \EasyWeChat\Staff\MessageBuilder
160
     *
161
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
162
     */
163
    public function message($message)
164
    {
165
        $messageBuilder = new MessageBuilder($this);
166
167
        return $messageBuilder->message($message);
168
    }
169
170
    /**
171
     * Send a message.
172
     *
173
     * @param string|array $message
174
     *
175
     * @return \EasyWeChat\Support\Collection
176
     */
177
    public function send($message)
178
    {
179
        return $this->parseJSON('json', [self::API_MESSAGE_SEND, $message]);
180
    }
181
182
    /**
183
     * Get staff session history.
184
     *
185
     * @param int $startTime
186
     * @param int $endTime
187
     * @param int $page
188
     * @param int $pageSize
189
     *
190
     * @return \EasyWeChat\Support\Collection
191
     */
192 View Code Duplication
    public function records($startTime, $endTime, $page = 1, $pageSize = 10)
193
    {
194
        $params = [
195
                   'starttime' => is_numeric($startTime) ? $startTime : strtotime($startTime),
196
                   'endtime' => is_numeric($endTime) ? $endTime : strtotime($endTime),
197
                   'pageindex' => $page,
198
                   'pagesize' => $pageSize,
199
                  ];
200
201
        return $this->parseJSON('json', [self::API_RECORDS, $params]);
202
    }
203
    
204
    /**
205
     * 获取聊天记录.
206
     *
207
     * @param int $startTime
208
     * @param int $endTime
209
     * @param int $msgId
210
     * @param int $number
211
     *
212
     * @return \EasyWeChat\Support\Collection
213
     */
214 View Code Duplication
    public function messages($startTime, $endTime, $msgId = 1, $number = 10000)
215
    {
216
        $params = [
217
                   'starttime' => is_numeric($startTime) ? $startTime : strtotime($startTime),
218
                   'endtime' => is_numeric($endTime) ? $endTime : strtotime($endTime),
219
                   'msgid' => $msgId,
220
                   'number' => $number,
221
                  ];
222
223
        return $this->parseJSON('json', [self::API_MSG_LIST, $params]);
224
    }
225
}
226