You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Wechat.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace crmeb\services\template\storage;
  3. use app\services\message\TemplateMessageServices;
  4. use crmeb\basic\BaseMessage;
  5. use crmeb\services\CacheService;
  6. use crmeb\services\wechat\OfficialAccount;
  7. use EasyWeChat\Kernel\Support\Collection;
  8. use GuzzleHttp\Exception\GuzzleException;
  9. use Psr\Http\Message\ResponseInterface;
  10. use think\facade\Log;
  11. /**
  12. * 公众号模板消息
  13. * Class Wechat\template\storage
  14. */
  15. class Wechat extends BaseMessage
  16. {
  17. /**
  18. * 初始化
  19. * @param array $config
  20. * @return mixed|void
  21. */
  22. protected function initialize(array $config)
  23. {
  24. parent::initialize($config);
  25. }
  26. /**
  27. * @param string $templateId
  28. * @return mixed
  29. * @throws \throwable
  30. */
  31. public function getTempId(string $templateId)
  32. {
  33. /** @var TemplateMessageServices $services */
  34. $services = app()->make(TemplateMessageServices::class);
  35. return CacheService::handler('TEMPLATE')->remember('wechat_' . $templateId, function () use ($services, $templateId) {
  36. return $services->getTempId($templateId, 1);
  37. });
  38. }
  39. /**
  40. * 发送消息
  41. * @param string $templateId
  42. * @param array $data
  43. * @return bool|mixed
  44. * @throws GuzzleException
  45. */
  46. public function send(string $templateId, array $data = [])
  47. {
  48. $templateId = $this->getTemplateCode($templateId);
  49. if (!$templateId) {
  50. return $this->setError('Template number does not exist');
  51. }
  52. $tempid = $this->getTempId($templateId);
  53. if (!$tempid) {
  54. return $this->setError('Template ID does not exist');
  55. }
  56. if (!$this->openId) {
  57. return $this->setError('Openid does not exist');
  58. }
  59. try {
  60. $res = OfficialAccount::sendTemplate($this->openId, $tempid, $data, $this->toUrl, $this->color);
  61. $this->clear();
  62. return $res;
  63. } catch (\Exception $e) {
  64. $this->isLog() && Log::error('发送给openid为:' . $this->openId . '微信模板消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  65. return $this->setError($e->getMessage());
  66. }
  67. }
  68. /**
  69. * 获取所有模板
  70. * @return array|Collection|mixed|object|ResponseInterface|string
  71. * @throws GuzzleException
  72. */
  73. public function list()
  74. {
  75. return OfficialAccount::getPrivateTemplates();
  76. }
  77. /**
  78. * 添加模板消息
  79. * @param string $shortId
  80. * @return array|Collection|mixed|object|ResponseInterface|string
  81. * @throws GuzzleException
  82. */
  83. public function add(string $shortId)
  84. {
  85. return OfficialAccount::addTemplateId($shortId);
  86. }
  87. /**
  88. * 删除模板消息
  89. * @param string $templateId
  90. * @return array|Collection|mixed|object|ResponseInterface|string
  91. * @throws GuzzleException
  92. */
  93. public function delete(string $templateId)
  94. {
  95. return OfficialAccount::deleleTemplate($templateId);
  96. }
  97. /**
  98. * 返回所有支持的行业列表
  99. * @return array|Collection|object|ResponseInterface|string
  100. */
  101. public function getIndustry()
  102. {
  103. return OfficialAccount::getIndustry();
  104. }
  105. }