Issues (10)

src/sender/MiniProgram.php (2 issues)

1
<?php
2
3
namespace Dongdavid\Notify\sender;
4
5
use Dongdavid\Notify\Exceptions\InvalidArgumentException;
6
use Dongdavid\Notify\Sender;
7
use Dongdavid\Notify\WechatManager;
0 ignored issues
show
The type Dongdavid\Notify\WechatManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class MiniProgram extends Sender
10
{
11
    const MESSAGE_URL = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=';
12
13
    public function checkConfig()
14
    {
15
        if (!$this->config['access_token']) {
16
            throw new InvalidArgumentException('notify config require access_token ');
17
        }
18
    }
19
20
    public function checkMsgFormate($msg)
21
    {
22
        if (!isset($msg['touser'])) {
23
            throw new \Exception('接收人openid未设置');
24
        }
25
        if (!isset($msg['template_id'])) {
26
            throw new \Exception('未传入模版ID');
27
        }
28
        if (!isset($msg['data'])) {
29
            throw new \Exception('未设置消息内容');
30
        }
31
        foreach ($msg['data'] as $item) {
32
            if (!isset($item['value'])) {
33
                throw new \Exception('模版消息内容未设置value');
34
            }
35
        }
36
    }
37
38
    public function send($msg)
39
    {
40
        $this->checkMsgFormate($msg);
41
        // 发送
42
        $res = $this->sendTemplate($msg);
43
        var_dump($res);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($res) looks like debug code. Are you sure you do not want to remove it?
Loading history...
44
        if (isset($res['errcode']) && 0 == $res['errcode']) {
45
            return true;
46
        }
47
        if (!isset($res['errcode'])) {
48
            throw new \Exception('发送失败:网络错误,无法请求微信服务器');
49
        }
50
        //if (0 == $res['errcode']) {
51
        //    return true;
52
        //} else {
53
        //    //if (in_array($res['errcode'], ['40014', '41001', '42001'])) {
54
        //    //    $this->config['access_token'] = WechatManager::updateAccessToken($this->config);
55
        //    //}
56
        //    throw new \Exception('发送失败:'.$res['errcode'].$res['errmsg']);
57
        //}
58
        throw new \Exception('发送失败:'.$res['errcode'].$res['errmsg']);
59
    }
60
61
    public function sendTemplate($msg)
62
    {
63
        $url = self::MESSAGE_URL.$this->config['access_token'];
64
65
        return $this->post($url, $msg);
66
    }
67
}
68