Template.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
  5. * Author: liaofei <136327134@qq.com>
  6. * Date: 2019/4/3 16:36
  7. */
  8. namespace crmeb\utils;
  9. use crmeb\exceptions\TemplateException;
  10. use crmeb\traits\LogicTrait;
  11. use think\app\Url;
  12. /** 消息发送
  13. * Class Template
  14. * @package crmeb\utils
  15. * @method $this setTemplateData(string $templateData) 设置发送模板消息数据
  16. * @method $this setTemplateOpenId(string $templateOpenId) 设置发送模板消息openid
  17. * @method $this setTemplateFormId(string $templateFormId) 设置发送模板消息formid
  18. * @method $this setTemplateDefaultColor(string $templateDefaultColor) 设置发送模板消息默认背景颜色
  19. * @method $this setTemplateCode(string $templateCode) 设置模板id
  20. * @method $this setHandleType($handleType) 设置发送类型句柄 1 = 小程序 , 2 = 公众号
  21. * @method $this setDefaultData($defaultData) 设置默认数据
  22. * @method $this setTemplateUrl($url, string $sux = '') 设置跳转Url
  23. * @method $this routine() 设置当前发送类型句柄为 小程序
  24. * @method $this wechat() 设置当前发送类型句柄为 公众号
  25. * @method $this subscribe() 设置当前发送类型句柄为 小程序订阅消息
  26. */
  27. class Template
  28. {
  29. use LogicTrait;
  30. /**
  31. * 注册服务 会自动添加$providers对应的key名称方法方便设置$handleType
  32. * @var array
  33. */
  34. protected $providers = [
  35. 'routine' => \crmeb\services\ProgramTemplateService::class,
  36. 'subscribe' => \crmeb\services\SubscribeTemplateService::class,
  37. 'wechat' => \crmeb\services\WechatTemplateService::class,
  38. ];
  39. /**
  40. * 可调用方法规则
  41. * @var array 'defaultData'=>[[],'array'] 生成的方法为 setDefaultData(array $value)
  42. */
  43. protected $propsRule = [
  44. 'defaultData' => [null, 'string'],
  45. 'templateCode' => [null, 'string'],
  46. 'templateData' => [[], 'array'],
  47. 'templateUrl' => [null, 'callable', 'postpositionUrl'],
  48. 'templateFormId' => [null, 'string'],
  49. 'handleType' => [null, 'string'],
  50. 'templateOpenId' => [null, 'string'],
  51. 'templateOpenId' => [null, 'string'],
  52. ];
  53. /**
  54. * 默认参数
  55. * @var array | int | bool
  56. */
  57. protected $defaultData;
  58. /**
  59. * 模板编号
  60. * @var string
  61. */
  62. protected $templateCode;
  63. /**
  64. * 模板消息数据
  65. * @var array
  66. */
  67. protected $templateData = [];
  68. /**
  69. * 模板消息跳转路径
  70. * @var string
  71. */
  72. protected $templateUrl = '';
  73. /**
  74. * 模板消息formid 为小程序提供
  75. * @var string
  76. */
  77. protected $templateFormId = '';
  78. /**
  79. * 发送类型 对应 $providers key
  80. * @var string | int
  81. */
  82. protected $handleType;
  83. /**
  84. * 接收人openid 小程序 和 公众号使用
  85. * @var string
  86. */
  87. protected $templateOpenId;
  88. /**
  89. * 模板消息默认颜色
  90. * @var string
  91. */
  92. protected $templateDefaultColor;
  93. /**
  94. * 实例化后操作
  95. */
  96. public function bool()
  97. {
  98. }
  99. /**
  100. * 自定义方法 后置改变Url
  101. * @param Url $url
  102. * @param string $suffix
  103. * @return string
  104. */
  105. public function postpositionUrl($url, string $suffix = '')
  106. {
  107. if ($url instanceof Url)
  108. $url = $url->suffix($suffix)->domain(true)->build();
  109. return $url;
  110. }
  111. /**
  112. * 验证参数
  113. */
  114. protected function validate()
  115. {
  116. $keys = array_keys($this->providers);
  117. if (is_string($this->handleType)) {
  118. if (!in_array($this->handleType, $keys))
  119. throw new TemplateException('设置的发送类型句柄不存在:' . $this->handleType);
  120. } elseif (is_int($this->handleType)) {
  121. if ($this->handleType > count($keys))
  122. throw new TemplateException('设置的发送类型句柄不存在:' . $this->handleType);
  123. $this->handleType = $keys[$this->handleType - 1];
  124. }
  125. if (!$this->handleType)
  126. throw new TemplateException('请设置发送类型句柄');
  127. if (!$this->templateData)
  128. throw new TemplateException('请设置发送模板数据');
  129. if (!$this->templateOpenId)
  130. throw new TemplateException('请设置发送模板OPENID');
  131. }
  132. /**
  133. * 发送消息
  134. * @param bool $excep 是否抛出异常
  135. * @return bool|null
  136. */
  137. public function send(bool $excep = false)
  138. {
  139. try {
  140. $this->validate();
  141. $resource = null;
  142. switch ($this->handleType) {
  143. case 'routine':
  144. $resource = self::$instance->routine->sendTemplate(
  145. $this->templateCode,
  146. $this->templateOpenId,
  147. $this->templateData,
  148. $this->templateFormId,
  149. $this->templateUrl,
  150. $this->templateDefaultColor
  151. );
  152. break;
  153. case 'wechat':
  154. $resource = self::$instance->wechat->sendTemplate(
  155. $this->templateOpenId,
  156. $this->templateCode,
  157. $this->templateData,
  158. $this->templateUrl,
  159. $this->templateDefaultColor
  160. );
  161. break;
  162. case 'subscribe':
  163. $resource = self::$instance->subscribe->sendTemplate(
  164. $this->templateCode,
  165. $this->templateOpenId,
  166. $this->templateData,
  167. $this->templateUrl
  168. );
  169. break;
  170. default:
  171. $resource = false;
  172. break;
  173. }
  174. $this->clear();
  175. return $resource;
  176. } catch (\Throwable $e) {
  177. if ($excep)
  178. throw new TemplateException($e->getMessage());
  179. return false;
  180. }
  181. }
  182. /**
  183. * 恢复默认值
  184. * @return $this
  185. */
  186. protected function clear()
  187. {
  188. $this->templateOpenId = null;
  189. $this->defaultData = null;
  190. $this->templateDefaultColor = null;
  191. $this->templateData = [];
  192. $this->templateUrl = null;
  193. $this->handleType = null;
  194. $this->templateFormId = null;
  195. $this->templateCode = null;
  196. return $this;
  197. }
  198. public function __destruct()
  199. {
  200. $this->clear();
  201. }
  202. }