Wechat.php 3.8 KB

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