Commit f8c39b14 by feixiang

消息发送扩展

parent e3934500
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/6
* Time: 10:23
*/
namespace Hdll\Services\Notice\Extensions\Notice;
use Hdll\Services\Notice\Common\WxMiniNotice;
use Hdll\Services\Notice\Enum\NoticeEnum;
class MiniBackendSender implements SenderInterface
{
private $sendType = NoticeEnum::TYPE_MINI_BACKEND_SEND;
private $data;
private $user;
private $page;
private $sendData;
private $template;
public function __construct(array $user,array $page, array $sendData, string $template)
{
$this->user = $user;
$this->page = $page;
$this->sendData = $sendData;
$this->template = $template;
}
/**
* 格式化数据
*/
public function format()
{
//$sendType
$this->data[$this->sendType] = [
"uid" => $this->user['id'], //用户的id
"template_id" => $this->template,//消息模板id
"page" => WxMiniNotice::generatePage(
$this->page['page'],
$this->page['param']
), //消息模板页面
];
foreach ( $this->sendData as $key => $data ) {
$this->data[$this->sendType]['data']["keyword{$key}"]['value'] = $data;
}
}
/**
* 获取发送数据
*
* @return mixed
*/
public function getData ()
{
return $this->data;
}
/**
* 获取发送类型
*
* @return array
*/
public function getSendType()
{
return [$this->sendType];
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/6
* Time: 10:23
*/
namespace Hdll\Services\Notice\Extensions\Notice;
use Hdll\Services\Notice\Common\WxMiniNotice;
use Hdll\Services\Notice\Enum\NoticeEnum;
class MiniFrontendSender implements SenderInterface
{
private $sendType = NoticeEnum::TYPE_MINI_FRONTEND_SEND;
private $data;
private $user;
private $page;
private $sendData;
private $template;
public function __construct(array $user,array $page, array $sendData, string $template)
{
$this->user = $user;
$this->page = $page;
$this->sendData = $sendData;
$this->template = $template;
}
/**
* 格式化数据
*/
public function format()
{
//$sendType
$this->data[$this->sendType] = [
"uid" => $this->user['id'], //用户的id
"storeId" => $this->user['storeId'], //店铺的id
"template_id" => $this->template,//消息模板id
"page" => WxMiniNotice::generatePage(
$this->page['page'],
$this->page['param']
), //消息模板页面
];
foreach ( $this->sendData as $key => $data ) {
$this->data[$this->sendType]['data']["keyword{$key}"]['value'] = $data;
}
}
/**
* 获取发送数据
*
* @return mixed
*/
public function getData ()
{
return $this->data;
}
/**
* 获取发送类型
*
* @return array
*/
public function getSendType()
{
return [$this->sendType];
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/6
* Time: 10:21
*/
namespace Hdll\Services\Notice\Extensions\Notice;
interface SenderInterface
{
public function format();
public function getData ();
public function getSendType();
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/10
* Time: 14:53
*/
namespace Hdll\Services\Notice\Extensions\Notice;
use Hdll\Services\Notice\Enum\NoticeEnum;
class TencentSMS implements SenderInterface
{
private $sendType = NoticeEnum::TYPE_TENCENT_SMS;
private $data;
private $phone;
private $template;
private $param;
public function __construct(string $phone, string $template, array $param)
{
$this->phone = $phone;
$this->template = $template;
$this->param = $param;
}
public function format()
{
$this->data[$this->sendType] = [
[
'templateId' => $this->template,
'phoneNumber' => $this->phone,
'param' => $this->param,
]
];
}
public function getData()
{
$this->data;
}
public function getSendType()
{
return [$this->sendType];
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/10/9
* Time: 9:22
*/
namespace Hdll\Services\Notice\Extensions;
use Hdll\Services\Notice\Extensions\Notice\SenderInterface;
use Hdll\Services\Notice\Lib\NoticeInterface;
class NoticeExtension
{
private $notice;
public function __construct(NoticeInterface $notice)
{
$this->notice = $notice;
}
/**
* 发送消息通知
*
* @param int $sendTime
* @param mixed ...$senders
* @return bool|mixed
*/
public function send(int $sendTime, ... $senders)
{
$data = [];
$sendType = [];
foreach ( $senders as $sender ) {
if ( ! $sender instanceof SenderInterface) {
continue;
}
if ( in_array($sender->getSendType(), $sendType) ) {
continue;
}
$data += $sender->getData();
$sendType += $sender->getSendType();
}
return $this->notice->send($sendType, $data, $sendTime);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment