Subscribe.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\SystemNotificationServices;
  13. use crmeb\services\template\BaseMessage;
  14. use crmeb\services\app\MiniProgramService;
  15. use think\facade\Log;
  16. /**
  17. * 订阅消息
  18. * Class Subscribe
  19. * @package crmeb\services\template\storage
  20. */
  21. class Subscribe extends BaseMessage
  22. {
  23. protected $error;
  24. protected function initialize(array $config)
  25. {
  26. parent::initialize($config); // TODO: Change the autogenerated stub
  27. }
  28. /**
  29. * @param string $templateId
  30. * @return mixed
  31. */
  32. public function getTempId(string $templateId)
  33. {
  34. return app()->make(SystemNotificationServices::class)->value(['routine_tempkey' => $templateId], 'routine_tempid');
  35. }
  36. /**
  37. * 发送订阅消息
  38. * @param string $tempid
  39. * @param array $data
  40. * @return bool|\EasyWeChat\Support\Collection|mixed|null
  41. */
  42. public function send(string $tempid, array $data = [])
  43. {
  44. if (!$tempid) {
  45. return $this->setError('Template ID does not exist');
  46. }
  47. if (!$this->openId) {
  48. return $this->setError('Openid does not exist');
  49. }
  50. try {
  51. $res = MiniProgramService::sendSubscribeTemlate($this->openId, $tempid, $data, $this->toUrl);
  52. $this->clear();
  53. return $res;
  54. } catch (\Throwable $e) {
  55. Log::error('发送给openid为:' . $this->openId . '小程序订阅消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  56. return $this->setError($e->getMessage());
  57. }
  58. }
  59. public function delete(string $templateId)
  60. {
  61. // TODO: Implement delete() method.
  62. }
  63. public function add(string $shortId)
  64. {
  65. // TODO: Implement add() method.
  66. }
  67. public function list()
  68. {
  69. // TODO: Implement list() method.
  70. }
  71. /**
  72. * 设置错误信息
  73. * @param string|null $error
  74. * @return bool
  75. */
  76. protected function setError(?string $error = null)
  77. {
  78. $this->error = $error ?: '未知错误';
  79. return false;
  80. }
  81. /**
  82. * 获取错误信息
  83. * @return string
  84. */
  85. public function getError()
  86. {
  87. $error = $this->error;
  88. $this->error = null;
  89. return $error;
  90. }
  91. }