Issues (10)

src/sender/WechatWork.php (3 issues)

1
<?php
2
3
namespace Dongdavid\Notify\sender;
4
5
use Dongdavid\Notify\Exceptions\InvalidArgumentException;
6
use Dongdavid\Notify\Sender;
7
8
class WechatWork extends Sender
9
{
10
    const MESSAGE_URL = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=';
11
12
    public function checkConfig()
13
    {
14
        if (!$this->config['access_token']) {
15
            throw new InvalidArgumentException('notify config require access_token ');
16
        }
17
    }
18
19
    public function checkMsgFormate($msg)
20
    {
21
        if (!isset($msg['agentid'])) {
22
            throw new \Exception('请传入部门ID');
23
        }
24
25
        if (!isset($msg['msgtype'])) {
26
            throw new \Exception('消息类型未传入');
27
        }
28
29
        if ('textcard' == $msg['msgtype']) {
30
            if (!isset($msg['textcard'])) {
31
                throw new \Exception('未传入消息主体');
32
            }
33
34
            if (!isset($msg['textcard']['title'])) {
35
                throw new \Exception('未设置标题');
36
            }
37
38
            if (!isset($msg['textcard']['description'])) {
39
                throw new \Exception('未设置描述');
40
            }
41
42
            if (!isset($msg['textcard']['url'])) {
43
                throw new \Exception('未设置跳转链接');
44
            }
45
        } elseif ('text' == $msg['msgtype']) {
46
            if (!isset($msg['text'])) {
47
                throw new \Exception('未传入消息主体');
48
            }
49
            if (!isset($msg['text']['content'])) {
50
                throw new \Exception('未设置消息内容');
51
            }
52
        }
53
54
        if (!isset($msg['touser']) && !isset($msg['toparty']) && !isset($msg['totag'])) {
55
            throw new \Exception('未设置接收人');
56
        }
57
58
        if (empty($msg['touser']) && empty($msg['toparty']) && empty($msg['totag'])) {
59
            throw new \Exception('接收人不能为空');
60
        }
61
    }
62
63
    public function send($msg)
64
    {
65
        $msg['agentid'] = $this->config['agentid'];
66
        $this->checkMsgFormate($msg);
67
        // 发送
68
        $res = $this->sendCard($msg);
69
        if (!isset($res['errcode'])) {
70
            throw new \Exception('发送失败:网络错误,无法请求微信服务器');
71
        }
72
        if (0 == $res['errcode']) {
73
            return true;
74
        } else {
75
            if (in_array($res['errcode'], ['40014', '41001', '42001'])) {
76
                $this->config['access_token'] = WechatManager::updateAccessToken($this->config);
0 ignored issues
show
The type Dongdavid\Notify\sender\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...
77
            }
78
79
            throw new \Exception('发送失败:'.$res['errcode'].$res['errmsg']);
80
        }
81
        // return false;
82
    }
83
84
    /**
85
     * [sendTextToUser 发送卡片消息]
86
     * look me baby.
87
     *
88
     * @Author   DongDavid
89
     * @DateTime 2017-07-12T14:31:25+0800
90
     *
91
     * @param [type] $access_token [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
92
     * @param int    $agentid      [应用id]
93
     * @param string $title        [标题128字符]
94
     * @param string $description  [描述 512字符]
95
     * @param string $url          [消息内容 不超过2048字节 换行用\n]
96
     * @param string $touser       [成员ID列表@all 为全部 多人用|分隔最多1000个 @all会忽略部门标签]
97
     * @param string $topart       [部门 多个用|分隔最多100个]
98
     * @param string $totag        [标签 多个用|分隔最多100个]
99
     * @param int    $safe         [是否保密 0不保密 1保密]
100
     *
101
     * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
102
     *                支持div标签 目前内置了3种文字颜色:灰色(gray)、高亮(highlight)、默认黑色(normal)
103
     *                以class方式引用即可 换行使用<br>
104
     */
105
    public function sendCard($msg)
106
    {
107
        $url = self::MESSAGE_URL.$this->config['access_token'];
108
        // halt($msg);
109
        return $this->post($url, $msg);
110
    }
111
}
112