Subscribe.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\MiniProgram;
  16. use think\facade\Log;
  17. /**
  18. * 订阅消息
  19. * Class Subscribe
  20. * @package crmeb\services\template\storage
  21. */
  22. class Subscribe extends BaseMessage
  23. {
  24. protected function initialize(array $config)
  25. {
  26. parent::initialize($config); //
  27. }
  28. /**
  29. * @param string $templateId
  30. * @return mixed
  31. */
  32. public function getTempId(string $templateId)
  33. {
  34. /** @var TemplateMessageServices $services */
  35. $services = app()->make(TemplateMessageServices::class);
  36. return CacheService::handler('TEMPLATE')->remember('subscribe_' . $templateId, function () use ($services, $templateId) {
  37. return $services->getTempId($templateId);
  38. });
  39. }
  40. /**
  41. * 发送订阅消息
  42. * @param string $templateId
  43. * @param array $data
  44. * @return bool|\EasyWeChat\Support\Collection|mixed|null
  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 = MiniProgram::sendSubscribeTemlate($this->openId, $tempid, $data, $this->toUrl);
  61. $this->clear();
  62. return $res;
  63. } catch (\Throwable $e) {
  64. $this->isLog() && Log::error('发送给openid为:' . $this->openId . '小程序订阅消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  65. return $this->setError($e->getMessage());
  66. }
  67. }
  68. public function delete(string $templateId)
  69. {
  70. //
  71. }
  72. public function add(string $shortId)
  73. {
  74. //
  75. }
  76. public function list()
  77. {
  78. //
  79. }
  80. }