Subscribe.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace ln\services\template\storage;
  3. use app\common\repositories\wechat\TemplateMessageRepository;
  4. use ln\basic\BaseMessage;
  5. use ln\services\MiniProgramService;
  6. use think\facade\Log;
  7. /**
  8. * 订阅消息
  9. * Class Subscribe
  10. * @package ln\services\template\storage
  11. */
  12. class Subscribe extends BaseMessage
  13. {
  14. protected function initialize(array $config)
  15. {
  16. parent::initialize($config); // TODO: Change the autogenerated stub
  17. }
  18. /**
  19. * @param string $templateId
  20. * @return mixed
  21. */
  22. public function getTempId(string $templateId)
  23. {
  24. return app()->make(TemplateMessageRepository::class)->getTempId($templateId, 0);
  25. }
  26. /**
  27. * 发送订阅消息
  28. * @param string $templateId
  29. * @param array $data
  30. * @return bool|\EasyWeChat\Support\Collection|mixed|null
  31. */
  32. public function send(string $templateId, array $data = [])
  33. {
  34. $templateId = $this->getTemplateCode($templateId);
  35. if (!$templateId) {
  36. return ; //$this->setError('Template number does not exist');
  37. }
  38. $tempid = $this->getTempId($templateId);
  39. if (!$tempid) {
  40. return ; //$this->setError('Template ID does not exist');
  41. }
  42. if (!$this->openId) {
  43. return ; //$this->setError('Openid does not exist');
  44. }
  45. try {
  46. $res = MiniProgramService::create()->sendSubscribeTemlate($this->openId, $tempid, $data, $this->toUrl);
  47. $this->clear();
  48. return $res;
  49. } catch (\Throwable $e) {
  50. $this->isLog() && Log::error('发送给openid为:' . $this->openId . '小程序订阅消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  51. return $this->setError($e->getMessage());
  52. }
  53. }
  54. public function delete(string $templateId)
  55. {
  56. // TODO: Implement delete() method.
  57. }
  58. public function add(string $shortId)
  59. {
  60. // TODO: Implement add() method.
  61. }
  62. public function list()
  63. {
  64. // TODO: Implement list() method.
  65. }
  66. }