Issues (10)

src/sender/WechatOffical.php (1 issue)

Labels
Severity
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 WechatOffical extends Sender
10
{
11
    const MESSAGE_URL = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
12
13
    public function checkConfig()
14
    {
15
        if (!$this->config['access_token']) {
16
            throw new InvalidArgumentException('缺失access_token');
17
        }
18
    }
19
20
    public function checkMsgFormate($msg)
21
    {
22
        if (!isset($msg['touser'])) {
23
            throw new \Exception('接收人openid未设置');
24
        }
25
26
        if (!isset($msg['template_id'])) {
27
            throw new \Exception('未传入模版ID');
28
        }
29
30
        if (!isset($msg['data'])) {
31
            throw new \Exception('未设置消息内容');
32
        }
33
        foreach ($msg['data'] as $item) {
34
            if (!isset($item['value'])) {
35
                throw new \Exception('模版消息内容未设置value');
36
            }
37
        }
38
        if (isset($msg['miniprogram'])) {
39
            if (!isset($msg['miniprogram']['appid'])) {
40
                throw new \Exception('缺失小程序appId');
41
            }
42
        }
43
    }
44
45
    public function send($msg)
46
    {
47
        $this->checkMsgFormate($msg);
48
        // 发送
49
        $res = $this->sendTemplate($msg);
50
51
        if (isset($res['errcode']) && 0 == $res['errcode']) {
52
            return true;
53
        }
54
        if (!isset($res['errcode'])) {
55
            throw new \Exception('发送失败:网络错误,无法请求微信服务器');
56
        }
57
        //if (0 == $res['errcode']) {
58
        //    return true;
59
        //} else {
60
        //    //if (in_array($res['errcode'], ['40014', '41001', '42001'])) {
61
        //        //$this->config['access_token'] = WechatManager::updateAccessToken($this->config);
62
        //    //}
63
        //}
64
        throw new \Exception('发送失败:'.$res['errcode'].$res['errmsg']);
65
    }
66
67
    public function sendTemplate($msg)
68
    {
69
        $url = self::MESSAGE_URL.$this->config['access_token'];
70
71
        return $this->post($url, $msg);
72
    }
73
}
74