Total Complexity | 53 |
Total Lines | 663 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 0 |
Complex classes like MiniService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MiniService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class MiniService extends Service |
||
33 | { |
||
34 | /** |
||
35 | * @var |
||
36 | */ |
||
37 | private $app_id, $app_secret; |
||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $grant_type = "client_credential"; |
||
42 | |||
43 | /** |
||
44 | * 驱动方式 |
||
45 | * @var string |
||
46 | */ |
||
47 | private $cache = "file"; |
||
48 | |||
49 | /** |
||
50 | * @param string $appId |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function appId(string $appId) |
||
54 | { |
||
55 | $this->app_id = $appId; |
||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param string $appSecret |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function appSecret(string $appSecret) |
||
64 | { |
||
65 | $this->app_secret = $appSecret; |
||
66 | return $this; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * 驱动方式 |
||
71 | * @param string $cache |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function cache(string $cache): self |
||
75 | { |
||
76 | $this->cache = $cache; |
||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * 获取配置信息 |
||
82 | * @return $this |
||
83 | */ |
||
84 | private function getConfig(): self |
||
85 | { |
||
86 | $this->cache = config('dtapp.wechat.mini.cache'); |
||
|
|||
87 | $this->app_id = config('dtapp.wechat.mini.app_id'); |
||
88 | $this->app_secret = config('dtapp.wechat.mini.app_secret'); |
||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * 用户支付完成后,获取该用户的 UnionId,无需用户授权 |
||
94 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPaidUnionId.html |
||
95 | * @param string $openid |
||
96 | * @return bool|mixed|string |
||
97 | * @throws DbException |
||
98 | * @throws DtaException |
||
99 | */ |
||
100 | public function getPaidUnionId(string $openid) |
||
101 | { |
||
102 | // 获取数据 |
||
103 | $accessToken = $this->getAccessToken(); |
||
104 | $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token={$accessToken['access_token']}&openid={$openid}"; |
||
105 | return HttpService::instance() |
||
106 | ->url($url) |
||
107 | ->toArray(); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * 获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 |
||
112 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html |
||
113 | * @param array $data |
||
114 | * @return array|bool|mixed|string |
||
115 | * @throws DbException |
||
116 | * @throws DtaException |
||
117 | */ |
||
118 | public function createWxaQrCode(array $data = []) |
||
119 | { |
||
120 | // 获取数据 |
||
121 | $accessToken = $this->getAccessToken(); |
||
122 | $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken['access_token']}"; |
||
123 | return HttpService::instance() |
||
124 | ->url($url) |
||
125 | ->data($data) |
||
126 | ->post() |
||
127 | ->toArray(false); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 |
||
132 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html |
||
133 | * @param array $data |
||
134 | * @return array|bool|mixed|string |
||
135 | * @throws DbException |
||
136 | * @throws DtaException |
||
137 | */ |
||
138 | public function getWxaCode(array $data = []) |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制 |
||
152 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html |
||
153 | * @param array $data |
||
154 | * @return array|bool|mixed|string |
||
155 | * @throws DbException |
||
156 | * @throws DtaException |
||
157 | */ |
||
158 | public function getWxaCodeUnLimit(array $data = []) |
||
159 | { |
||
160 | // 获取数据 |
||
161 | $accessToken = $this->getAccessToken(); |
||
162 | $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken['access_token']}"; |
||
163 | return HttpService::instance() |
||
164 | ->url($url) |
||
165 | ->data($data) |
||
166 | ->post() |
||
167 | ->toArray(false); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * 组合模板并添加至帐号下的个人模板库 |
||
172 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html |
||
173 | * @param array $data |
||
174 | * @return bool|mixed|string |
||
175 | * @throws DbException |
||
176 | * @throws DtaException |
||
177 | */ |
||
178 | public function addTemplate(array $data = []) |
||
179 | { |
||
180 | // 获取数据 |
||
181 | $accessToken = $this->getAccessToken(); |
||
182 | $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={$accessToken['access_token']}"; |
||
183 | return HttpService::instance() |
||
184 | ->url($url) |
||
185 | ->data($data) |
||
186 | ->toArray(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * 删除帐号下的个人模板 |
||
191 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.deleteTemplate.html |
||
192 | * @param string $priTmplId 要删除的模板id |
||
193 | * @return bool|mixed|string |
||
194 | * @throws DbException |
||
195 | * @throws DtaException |
||
196 | */ |
||
197 | public function deleteTemplate(string $priTmplId) |
||
198 | { |
||
199 | // 获取数据 |
||
200 | $accessToken = $this->getAccessToken(); |
||
201 | $url = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token={$accessToken['access_token']}"; |
||
202 | $data = [ |
||
203 | 'priTmplId' => $priTmplId |
||
204 | ]; |
||
205 | return HttpService::instance() |
||
206 | ->url($url) |
||
207 | ->data($data) |
||
208 | ->toArray(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * 获取小程序账号的类目 |
||
213 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getCategory.html |
||
214 | * @return bool|mixed|string |
||
215 | * @throws DbException |
||
216 | * @throws DtaException |
||
217 | */ |
||
218 | public function getCategory() |
||
219 | { |
||
220 | // 获取数据 |
||
221 | $accessToken = $this->getAccessToken(); |
||
222 | $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token={$accessToken['access_token']}"; |
||
223 | return HttpService::instance() |
||
224 | ->url($url) |
||
225 | ->toArray(); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * 获取模板标题下的关键词列表 |
||
230 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateKeyWordsById.html |
||
231 | * @param string $tid 模板标题 id |
||
232 | * @return bool|mixed|string |
||
233 | * @throws DbException |
||
234 | * @throws DtaException |
||
235 | */ |
||
236 | public function getPubTemplateKeyWordsById(string $tid) |
||
237 | { |
||
238 | // 获取数据 |
||
239 | $accessToken = $this->getAccessToken(); |
||
240 | $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token={$accessToken['access_token']}"; |
||
241 | $data = [ |
||
242 | 'tid' => $tid |
||
243 | ]; |
||
244 | return HttpService::instance() |
||
245 | ->url($url) |
||
246 | ->data($data) |
||
247 | ->toArray(); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * 获取帐号所属类目下的公共模板标题 |
||
252 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateTitleList.html |
||
253 | * @param array $data |
||
254 | * @return bool|mixed|string |
||
255 | * @throws DbException |
||
256 | * @throws DtaException |
||
257 | */ |
||
258 | public function getPubTemplateTitleList(array $data = []) |
||
259 | { |
||
260 | // 获取数据 |
||
261 | $accessToken = $this->getAccessToken(); |
||
262 | $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token={$accessToken['access_token']}"; |
||
263 | return HttpService::instance() |
||
264 | ->url($url) |
||
265 | ->data($data) |
||
266 | ->toArray(); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * 获取当前帐号下的个人模板列表 |
||
271 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html |
||
272 | * @return bool|mixed|string |
||
273 | * @throws DbException |
||
274 | * @throws DtaException |
||
275 | */ |
||
276 | public function getTemplateList() |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * 发送订阅消息 |
||
288 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html |
||
289 | * @param array $data |
||
290 | * @return bool|mixed|string |
||
291 | * @throws DbException |
||
292 | * @throws DtaException |
||
293 | */ |
||
294 | public function subscribeMessageSend(array $data = []) |
||
295 | { |
||
296 | // 获取数据 |
||
297 | $accessToken = $this->getAccessToken(); |
||
298 | $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken['access_token']}"; |
||
299 | return HttpService::instance() |
||
300 | ->url($url) |
||
301 | ->data($data) |
||
302 | ->post() |
||
303 | ->toArray(); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * 登录凭证校验 |
||
308 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html |
||
309 | * @param string $js_code |
||
310 | * @return bool|mixed|string |
||
311 | * @throws DtaException |
||
312 | */ |
||
313 | public function code2Session(string $js_code) |
||
314 | { |
||
315 | if (empty($this->app_id) || empty($this->app_secret)) { |
||
316 | $this->getConfig(); |
||
317 | } |
||
318 | if (empty($this->app_id)) { |
||
319 | throw new DtaException('请检查app_id参数'); |
||
320 | } |
||
321 | if (empty($this->app_secret)) { |
||
322 | throw new DtaException('请检查app_secret参数'); |
||
323 | } |
||
324 | $this->grant_type = "authorization_code"; |
||
325 | $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->app_id}&secret={$this->app_secret}&js_code={$js_code}&grant_type={$this->grant_type}"; |
||
326 | return HttpService::instance() |
||
327 | ->url($url) |
||
328 | ->toArray(); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * 检验数据的真实性,并且获取解密后的明文. |
||
333 | * @param string $js_code |
||
334 | * @param string $encrypted_data |
||
335 | * @param string $iv |
||
336 | * @return bool|mixed |
||
337 | * @throws DtaException |
||
338 | */ |
||
339 | public function userInfo(string $js_code, string $encrypted_data, string $iv) |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * 数据签名校验,并且获取解密后的明文. |
||
351 | * @param string $js_code |
||
352 | * @param string $encrypted_data |
||
353 | * @param string $iv |
||
354 | * @return mixed |
||
355 | * @throws DtaException |
||
356 | */ |
||
357 | public function userPhone(string $js_code, string $encrypted_data, string $iv) |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * 数据签名校验,并且获取解密后的明文. |
||
369 | * @param string $session_key |
||
370 | * @param string $encrypted_data |
||
371 | * @param string $iv |
||
372 | * @return mixed |
||
373 | */ |
||
374 | public function decode(string $session_key, string $encrypted_data, string $iv) |
||
375 | { |
||
376 | $result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session_key), 1, base64_decode($iv)); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * 【小程序直播】直播间管理接口 - 创建直播间 |
||
382 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#1 |
||
383 | * @param array $data |
||
384 | * @return array|bool|mixed|string |
||
385 | * @throws DbException |
||
386 | * @throws DtaException |
||
387 | */ |
||
388 | public function broadcastRoomCreate(array $data = []) |
||
389 | { |
||
390 | // 获取数据 |
||
391 | $accessToken = $this->getAccessToken(); |
||
392 | $url = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token={$accessToken['access_token']}"; |
||
393 | return HttpService::instance() |
||
394 | ->url($url) |
||
395 | ->data($data) |
||
396 | ->post() |
||
397 | ->toArray(); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * 【小程序直播】直播间管理接口 - 获取直播间列表 |
||
402 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#2 |
||
403 | * @param array $data |
||
404 | * @return array|bool|mixed|string |
||
405 | * @throws DbException |
||
406 | * @throws DtaException |
||
407 | */ |
||
408 | public function broadcastGetLiveInfos(array $data = []) |
||
409 | { |
||
410 | // 获取数据 |
||
411 | $accessToken = $this->getAccessToken(); |
||
412 | $url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$accessToken['access_token']}"; |
||
413 | return HttpService::instance() |
||
414 | ->url($url) |
||
415 | ->data($data) |
||
416 | ->post() |
||
417 | ->toArray(); |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * 【小程序直播】直播间管理接口 - 获取直播间回放 |
||
422 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#3 |
||
423 | * @param array $data |
||
424 | * @return array|bool|mixed|string |
||
425 | * @throws DbException |
||
426 | * @throws DtaException |
||
427 | */ |
||
428 | public function broadcastGetLiveInfo(array $data = []) |
||
429 | { |
||
430 | // 获取数据 |
||
431 | $accessToken = $this->getAccessToken(); |
||
432 | $url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$accessToken['access_token']}"; |
||
433 | return HttpService::instance() |
||
434 | ->url($url) |
||
435 | ->data($data) |
||
436 | ->post() |
||
437 | ->toArray(); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * 【小程序直播】直播间管理接口 - 直播间导入商品 |
||
442 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#4 |
||
443 | * @param array $data |
||
444 | * @return array|bool|mixed|string |
||
445 | * @throws DbException |
||
446 | * @throws DtaException |
||
447 | */ |
||
448 | public function broadcastRoomAddGoods(array $data = []) |
||
449 | { |
||
450 | // 获取数据 |
||
451 | $accessToken = $this->getAccessToken(); |
||
452 | $url = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods?access_token={$accessToken['access_token']}"; |
||
453 | return HttpService::instance() |
||
454 | ->url($url) |
||
455 | ->data($data) |
||
456 | ->post() |
||
457 | ->toArray(); |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * 【小程序直播】直播商品管理接口 - 商品添加并提审 |
||
462 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#1 |
||
463 | * @param array $data |
||
464 | * @return array|bool|mixed|string |
||
465 | * @throws DbException |
||
466 | * @throws DtaException |
||
467 | */ |
||
468 | public function broadcastGoodsAdd(array $data = []) |
||
469 | { |
||
470 | // 获取数据 |
||
471 | $accessToken = $this->getAccessToken(); |
||
472 | $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token={$accessToken['access_token']}"; |
||
473 | return HttpService::instance() |
||
474 | ->url($url) |
||
475 | ->data($data) |
||
476 | ->post() |
||
477 | ->toArray(); |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * 【小程序直播】直播商品管理接口 - 撤回审核 |
||
482 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#2 |
||
483 | * @param array $data |
||
484 | * @return array|bool|mixed|string |
||
485 | * @throws DbException |
||
486 | * @throws DtaException |
||
487 | */ |
||
488 | public function broadcastGoodsResetAudit(array $data = []) |
||
498 | } |
||
499 | |||
500 | |||
501 | /** |
||
502 | * 【小程序直播】直播商品管理接口 - 重新提交审核 |
||
503 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#3 |
||
504 | * @param array $data |
||
505 | * @return array|bool|mixed|string |
||
506 | * @throws DbException |
||
507 | * @throws DtaException |
||
508 | */ |
||
509 | public function broadcastGoodsAudit(array $data = []) |
||
510 | { |
||
511 | // 获取数据 |
||
512 | $accessToken = $this->getAccessToken(); |
||
513 | $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/audit?access_token={$accessToken['access_token']}"; |
||
514 | return HttpService::instance() |
||
515 | ->url($url) |
||
516 | ->data($data) |
||
517 | ->post() |
||
518 | ->toArray(); |
||
519 | } |
||
520 | |||
521 | |||
522 | /** |
||
523 | * 【小程序直播】直播商品管理接口 - 删除商品 |
||
524 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#4 |
||
525 | * @param array $data |
||
526 | * @return array|bool|mixed|string |
||
527 | * @throws DbException |
||
528 | * @throws DtaException |
||
529 | */ |
||
530 | public function broadcastGoodsDelete(array $data = []) |
||
531 | { |
||
532 | // 获取数据 |
||
533 | $accessToken = $this->getAccessToken(); |
||
534 | $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token={$accessToken['access_token']}"; |
||
535 | return HttpService::instance() |
||
536 | ->url($url) |
||
537 | ->data($data) |
||
538 | ->post() |
||
539 | ->toArray(); |
||
540 | } |
||
541 | |||
542 | |||
543 | /** |
||
544 | * 【小程序直播】直播商品管理接口 - 更新商品 |
||
545 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#5 |
||
546 | * @param array $data |
||
547 | * @return array|bool|mixed|string |
||
548 | * @throws DbException |
||
549 | * @throws DtaException |
||
550 | */ |
||
551 | public function broadcastGoodsUpdate(array $data = []) |
||
552 | { |
||
553 | // 获取数据 |
||
554 | $accessToken = $this->getAccessToken(); |
||
555 | $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/update?access_token={$accessToken['access_token']}"; |
||
556 | return HttpService::instance() |
||
557 | ->url($url) |
||
558 | ->data($data) |
||
559 | ->post() |
||
560 | ->toArray(); |
||
561 | } |
||
562 | |||
563 | |||
564 | /** |
||
565 | * 【小程序直播】直播商品管理接口 - 获取商品状态 |
||
566 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#6 |
||
567 | * @param array $data |
||
568 | * @return array|bool|mixed|string |
||
569 | * @throws DbException |
||
570 | * @throws DtaException |
||
571 | */ |
||
572 | public function broadcastGetGoodsWarehouse(array $data = []) |
||
582 | } |
||
583 | |||
584 | |||
585 | /** |
||
586 | * 【小程序直播】直播商品管理接口 - 获取商品列表 |
||
587 | * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#7 |
||
588 | * @param array $data |
||
589 | * @return array|bool|mixed|string |
||
590 | * @throws DbException |
||
591 | * @throws DtaException |
||
592 | */ |
||
593 | public function broadcastGoodsGetAppRoved(array $data = []) |
||
594 | { |
||
595 | // 获取数据 |
||
596 | $accessToken = $this->getAccessToken(); |
||
597 | $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved?access_token={$accessToken['access_token']}"; |
||
598 | return HttpService::instance() |
||
599 | ->url($url) |
||
600 | ->data($data) |
||
601 | ->post() |
||
602 | ->toArray(); |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * 获取小程序全局唯一后台接口调用凭据(access_token) |
||
607 | * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html |
||
608 | * @return bool|mixed|string |
||
609 | * @throws DbException |
||
610 | * @throws DtaException |
||
611 | */ |
||
612 | public function accessToken() |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * 获取access_token信息 |
||
620 | * @return array|bool|mixed|string|string[] |
||
621 | * @throws DbException |
||
622 | * @throws DtaException |
||
623 | */ |
||
624 | private function getAccessToken() |
||
695 | } |
||
696 | } |
||
697 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.