调整模板消息发送工具类

This commit is contained in:
liaofei 2019-11-28 18:39:29 +08:00
parent 0d8c4d7cb8
commit ae4a0e23c7

View File

@ -22,7 +22,7 @@ use think\app\Url;
* @method $this setTemplateCode(string $templateCode) 设置模板id * @method $this setTemplateCode(string $templateCode) 设置模板id
* @method $this setSendType($sendType) 设置发送类型句柄 1 = 小程序 2 = 公众号 * @method $this setSendType($sendType) 设置发送类型句柄 1 = 小程序 2 = 公众号
* @method $this setDefaultData($defaultData) 设置默认数据 * @method $this setDefaultData($defaultData) 设置默认数据
* @method $this setTemplateUrl(Url $url,string $sux = '') 设置跳转Url * @method $this setTemplateUrl(Url $url, string $sux = '') 设置跳转Url
* @method $this routine() 设置当前发送类型句柄为 小程序 * @method $this routine() 设置当前发送类型句柄为 小程序
* @method $this wechat() 设置当前发送类型句柄为 公众号 * @method $this wechat() 设置当前发送类型句柄为 公众号
*/ */
@ -44,15 +44,14 @@ class Template
* @var array 'defaultData'=>[[],'array'] 生成的方法为 setDefaultData(array $value) * @var array 'defaultData'=>[[],'array'] 生成的方法为 setDefaultData(array $value)
*/ */
protected $propsRule = [ protected $propsRule = [
'defaultData' => '', 'defaultData' => [null, 'string'],
'templateCode' => [null,'string'], 'templateCode' => [null, 'string'],
'templateData' => [[],'array'], 'templateData' => [[], 'array'],
'templateUrl' => [null,'callable','postpositionUrl'], 'templateUrl' => [null, 'callable', 'postpositionUrl'],
'templateFormId' => [null,'string'], 'templateFormId' => [null, 'string'],
'sendType' => [null,'string'], 'sendType' => [null, 'string'],
'templateOpenId' => [null,'string'], 'templateOpenId' => [null, 'string'],
'templateOpenId' => [null,'string'], 'templateOpenId' => [null, 'string'],
'templateDefaultColor' => [null,'string'],
]; ];
/** /**
@ -83,7 +82,7 @@ class Template
* 模板消息formid 为小程序提供 * 模板消息formid 为小程序提供
* @var string * @var string
*/ */
protected $templateFormId =''; protected $templateFormId = '';
/** /**
* 发送类型 对应 $providers key * 发送类型 对应 $providers key
@ -117,9 +116,11 @@ class Template
* @param string $suffix * @param string $suffix
* @return string * @return string
*/ */
public function postpositionUrl(Url $url,string $suffix = '') public function postpositionUrl($url, string $suffix = '')
{ {
return $url->suffix($suffix)->domain(true)->build(); if($url instanceof Url)
$url = $url->suffix($suffix)->domain(true)->build();
return $url;
} }
/** /**
@ -129,21 +130,21 @@ class Template
{ {
$keys = array_keys($this->providers); $keys = array_keys($this->providers);
if (is_string($this->sendType)) { if (is_string($this->sendType)) {
if(!in_array($this->sendType,$keys)) if (!in_array($this->sendType, $keys))
throw new AuthException('设置的发送类型句柄不存在:'.$this->sendType); throw new AuthException('设置的发送类型句柄不存在:' . $this->sendType);
} elseif (is_int($this->sendType)) { } elseif (is_int($this->sendType)) {
if($this->sendType > count($keys)) if ($this->sendType > count($keys))
throw new AuthException('设置的发送类型句柄不存在:'.$this->sendType); throw new AuthException('设置的发送类型句柄不存在:' . $this->sendType);
$this->sendType = $keys[$this->sendType - 1]; $this->sendType = $keys[$this->sendType - 1];
} }
if (!$this->sendType) if (!$this->sendType)
throw new AuthException('请设置发送类型句柄'); throw new AuthException('请设置发送类型句柄');
if(!$this->templateData) if (!$this->templateData)
throw new AuthException('请设置发送模板数据'); throw new AuthException('请设置发送模板数据');
if(!$this->templateOpenId) if (!$this->templateOpenId)
throw new AuthException('请设置发送模板OPENID'); throw new AuthException('请设置发送模板OPENID');
} }
@ -154,12 +155,12 @@ class Template
*/ */
public function send(bool $excep = false) public function send(bool $excep = false)
{ {
try{ try {
$this->validate(); $this->validate();
$resource = null; $resource = null;
switch ($this->sendType){ switch ($this->sendType) {
case 'routine': case 'routine':
$resource = self::$instance->routine->sendTemplate( $resource = self::$instance->routine->sendTemplate(
$this->templateCode, $this->templateCode,
@ -187,8 +188,8 @@ class Template
$this->clear(); $this->clear();
return $resource; return $resource;
}catch (\Throwable $e){ } catch (\Throwable $e) {
if($excep) if ($excep)
throw new AuthException($e->getMessage()); throw new AuthException($e->getMessage());
return false; return false;
} }