Subscribe.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\common\repositories\system\notice\SystemNoticeConfigRepository;
  13. use app\common\repositories\wechat\TemplateMessageRepository;
  14. use crmeb\basic\BaseMessage;
  15. use crmeb\services\MiniProgramService;
  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); // TODO: Change the autogenerated stub
  27. }
  28. /**
  29. * @param string $templateId
  30. * @return mixed
  31. */
  32. public function getTempId(string $templateId)
  33. {
  34. $tempkey = app()->make(SystemNoticeConfigRepository::class)->getSearch(['const_key' => $templateId])->find();
  35. return $tempkey['routine_tempid'] ?? '';
  36. }
  37. /**
  38. * 发送订阅消息
  39. * @param string $templateId
  40. * @param array $data
  41. * @return bool|\EasyWeChat\Support\Collection|mixed|null
  42. */
  43. public function send(string $templateId, array $data = [])
  44. {
  45. // $templateId = $this->getTemplateCode($templateId);
  46. // if (!$templateId) {
  47. // return ; //$this->setError('Template number does not exist');
  48. // }
  49. $tempid = $this->getTempId($templateId);
  50. if (!$tempid || !$this->openId) {
  51. return ; //$this->setError('Template ID does not exist');
  52. }
  53. try {
  54. $res = MiniProgramService::create()->sendSubscribeTemlate($this->openId, $tempid, $data, $this->toUrl);
  55. $this->clear();
  56. return $res;
  57. } catch (\Throwable $e) {
  58. Log::error('发送给openid为:' . $this->openId . '小程序订阅消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  59. return $this->setError($e->getMessage());
  60. }
  61. }
  62. public function delete(string $templateId)
  63. {
  64. // TODO: Implement delete() method.
  65. }
  66. public function add(string $shortId)
  67. {
  68. // TODO: Implement add() method.
  69. }
  70. public function list()
  71. {
  72. // TODO: Implement list() method.
  73. }
  74. }