Subscribe.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\common\repositories\wechat\TemplateMessageRepository;
  13. use crmeb\basic\BaseMessage;
  14. use crmeb\services\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 function initialize(array $config)
  24. {
  25. parent::initialize($config); // TODO: Change the autogenerated stub
  26. }
  27. /**
  28. * @param string $templateId
  29. * @return mixed
  30. */
  31. public function getTempId(string $templateId)
  32. {
  33. return app()->make(TemplateMessageRepository::class)->getTempId($templateId, 0);
  34. }
  35. /**
  36. * 发送订阅消息
  37. * @param string $templateId
  38. * @param array $data
  39. * @return bool|\EasyWeChat\Support\Collection|mixed|null
  40. */
  41. public function send(string $templateId, array $data = [])
  42. {
  43. $templateId = $this->getTemplateCode($templateId);
  44. if (!$templateId) {
  45. return ; //$this->setError('Template number does not exist');
  46. }
  47. $tempid = $this->getTempId($templateId);
  48. if (!$tempid) {
  49. return ; //$this->setError('Template ID does not exist');
  50. }
  51. if (!$this->openId) {
  52. return ; //$this->setError('Openid does not exist');
  53. }
  54. try {
  55. $res = MiniProgramService::create()->sendSubscribeTemlate($this->openId, $tempid, $data, $this->toUrl);
  56. $this->clear();
  57. return $res;
  58. } catch (\Throwable $e) {
  59. $this->isLog() && Log::error('发送给openid为:' . $this->openId . '小程序订阅消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  60. return $this->setError($e->getMessage());
  61. }
  62. }
  63. public function delete(string $templateId)
  64. {
  65. // TODO: Implement delete() method.
  66. }
  67. public function add(string $shortId)
  68. {
  69. // TODO: Implement add() method.
  70. }
  71. public function list()
  72. {
  73. // TODO: Implement list() method.
  74. }
  75. }