123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace crmeb\utils;
- use crmeb\exceptions\TemplateException;
- use crmeb\traits\LogicTrait;
- use think\app\Url;
- class Template
- {
- use LogicTrait;
-
- protected $providers = [
- 'routine' => \crmeb\services\ProgramTemplateService::class,
- 'subscribe' => \crmeb\services\SubscribeTemplateService::class,
- 'wechat' => \crmeb\services\WechatTemplateService::class,
- ];
-
- protected $propsRule = [
- 'defaultData' => [null, 'string'],
- 'templateCode' => [null, 'string'],
- 'templateData' => [[], 'array'],
- 'templateUrl' => [null, 'callable', 'postpositionUrl'],
- 'templateFormId' => [null, 'string'],
- 'handleType' => [null, 'string'],
- 'templateOpenId' => [null, 'string'],
- 'templateOpenId' => [null, 'string'],
- ];
-
- protected $defaultData;
-
- protected $templateCode;
-
- protected $templateData = [];
-
- protected $templateUrl = '';
-
- protected $templateFormId = '';
-
- protected $handleType;
-
- protected $templateOpenId;
-
- protected $templateDefaultColor;
-
- public function bool()
- {
- }
-
- public function postpositionUrl($url, string $suffix = '')
- {
- if ($url instanceof Url)
- $url = $url->suffix($suffix)->domain(true)->build();
- return $url;
- }
-
- protected function validate()
- {
- $keys = array_keys($this->providers);
- if (is_string($this->handleType)) {
- if (!in_array($this->handleType, $keys))
- throw new TemplateException('设置的发送类型句柄不存在:' . $this->handleType);
- } elseif (is_int($this->handleType)) {
- if ($this->handleType > count($keys))
- throw new TemplateException('设置的发送类型句柄不存在:' . $this->handleType);
- $this->handleType = $keys[$this->handleType - 1];
- }
- if (!$this->handleType)
- throw new TemplateException('请设置发送类型句柄');
- if (!$this->templateData)
- throw new TemplateException('请设置发送模板数据');
- if (!$this->templateOpenId)
- throw new TemplateException('请设置发送模板OPENID');
- }
-
- public function send(bool $excep = false)
- {
- try {
- $this->validate();
- $resource = null;
- switch ($this->handleType) {
- case 'routine':
- $resource = self::$instance->routine->sendTemplate(
- $this->templateCode,
- $this->templateOpenId,
- $this->templateData,
- $this->templateFormId,
- $this->templateUrl,
- $this->templateDefaultColor
- );
- break;
- case 'wechat':
- $resource = self::$instance->wechat->sendTemplate(
- $this->templateOpenId,
- $this->templateCode,
- $this->templateData,
- $this->templateUrl,
- $this->templateDefaultColor
- );
- break;
- case 'subscribe':
- $resource = self::$instance->subscribe->sendTemplate(
- $this->templateCode,
- $this->templateOpenId,
- $this->templateData,
- $this->templateUrl
- );
- break;
- default:
- $resource = false;
- break;
- }
- $this->clear();
- return $resource;
- } catch (\Throwable $e) {
- if ($excep)
- throw new TemplateException($e->getMessage());
- return false;
- }
- }
-
- protected function clear()
- {
- $this->templateOpenId = null;
- $this->defaultData = null;
- $this->templateDefaultColor = null;
- $this->templateData = [];
- $this->templateUrl = null;
- $this->handleType = null;
- $this->templateFormId = null;
- $this->templateCode = null;
- return $this;
- }
- public function __destruct()
- {
- $this->clear();
- }
- }
|