This commit is contained in:
候学杰 2019-02-19 16:58:49 +08:00
parent ca76cbc976
commit 73660db7d3
4 changed files with 73 additions and 52 deletions

View File

@ -12,12 +12,12 @@ abstract class MessageInterface
const TYPE_TEXT_CARD = 'textcard'; const TYPE_TEXT_CARD = 'textcard';
const TYPE_NEWS = 'news'; const TYPE_NEWS = 'news';
const TYPE_MP_NEWS = 'mpnews'; const TYPE_MP_NEWS = 'mpnews';
const TYPE_MINI_PROGRAM_NOTICE = 'miniprogram_notice';
protected $type; protected $type;
protected $attr;
protected $safe;
protected $params; protected $params;
protected $attr;
public function toUser($user) public function toUser($user)
{ {
@ -41,10 +41,17 @@ abstract class MessageInterface
{ {
$params = array_merge([ $params = array_merge([
'msgtype' => $this->type, 'msgtype' => $this->type,
'safe' => $this->safe, 'safe' => 0,
], $this->params); ], $this->params);
switch ($this->type) { switch ($this->type) {
case self::TYPE_TEXT:
$params = array_merge($params, [
$this->type => [
'content' => $this->attr['content'],
],
]);
break;
case self::TYPE_TEXT_CARD: case self::TYPE_TEXT_CARD:
$params = array_merge($params, [ $params = array_merge($params, [
$this->type => [ $this->type => [
@ -55,10 +62,15 @@ abstract class MessageInterface
], ],
]); ]);
break; break;
case self::TYPE_MINI_PROGRAM_NOTICE:
$params = array_merge($params, [
$this->type => $this->attr,
]);
break;
default: default:
throw new \Exception('message type ' . $this->type . ' is not allow'); throw new \Exception('message type ' . $this->type . ' is not allow');
} }
return $arr; return $params;
} }
} }

View File

@ -0,0 +1,52 @@
<?php
namespace Wechat\Work\Message;
use Wechat\Work\Message\MessageInterface;
class MiniProgramMessage extends MessageInterface
{
public function __construct()
{
$this->type = self::TYPE_MINI_PROGRAM_NOTICE;
}
public function appid($val)
{
$this->attr['appid'] = $val;
return $this;
}
public function page($val)
{
$this->attr['page'] = $val;
return $this;
}
public function title($id)
{
$this->attr['title'] = $id;
return $this;
}
public function description($id)
{
$this->attr['description'] = $id;
return $this;
}
public function emphasisFirstItem($id)
{
$this->attr['emphasis_first_item'] = $id;
return $this;
}
public function contentItem($id)
{
$this->attr['content_item'] = $id;
return $this;
}
}

View File

@ -6,11 +6,7 @@ use Wechat\Work\Message\MessageInterface;
class TextCardMessage extends MessageInterface class TextCardMessage extends MessageInterface
{ {
protected $type;
protected $attr;
public function __construct() public function __construct()
{ {
$this->type = self::TYPE_TEXT_CARD; $this->type = self::TYPE_TEXT_CARD;
@ -40,24 +36,4 @@ class TextCardMessage extends MessageInterface
return $this; return $this;
} }
public function toArray()
{
$arr = [
'msgtype' => $this->type,
$this->type => [
'title' => $this->attr['title'],
'description' => $this->attr['description'],
'url' => $this->attr['url'],
'btntxt' => $this->attr['btntxt'],
],
'safe' => $this->safe,
];
if (!empty($this->params)) {
$arr = array_merge($this->params, $arr);
}
return $arr;
}
} }

View File

@ -1,35 +1,16 @@
<?php <?php
namespace App\Wechat\Work; namespace Wechat\Work\Message;
class TextMessage extends Message use Wechat\Work\Message\MessageInterface;
class TextMessage extends MessageInterface
{ {
protected $type;
protected $attr;
public function __construct($content) public function __construct($content)
{ {
parent::__construct();
$this->type = self::TYPE_TEXT; $this->type = self::TYPE_TEXT;
$this->attr['content'] = $content; $this->attr['content'] = $content;
} }
public function toArray()
{
$arr = [
'msgtype' => $this->type,
$this->type => [
'content' => $this->attr['content'],
],
'safe' => $this->safe,
];
if (!empty($this->params)) {
$arr = array_merge($this->params, $arr);
}
return $arr;
}
} }