Subscribe.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace crmeb\services\template\storage;
  3. use crmeb\basic\BaseMessage;
  4. use crmeb\services\MiniProgramService;
  5. use think\facade\Log;
  6. use think\facade\Db;
  7. /**
  8. * 订阅消息
  9. * Class Subscribe
  10. * @package crmeb\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. * 发送订阅消息
  20. * @param string $templateId
  21. * @param array $data
  22. * @return bool|\EasyWeChat\Support\Collection|mixed|null
  23. */
  24. public function send(string $templateId, array $data = [])
  25. {
  26. $templateId = $this->getTemplateCode($templateId);
  27. if (!$templateId) {
  28. return $this->setError('Template number does not exist');
  29. }
  30. $tempid = Db::name('template_message')->where(['type' => 0, 'tempkey' => $templateId, 'status' => 1])->value('tempid');
  31. if (!$tempid) {
  32. return $this->setError('Template ID does not exist');
  33. }
  34. if (!$this->openId) {
  35. return $this->setError('Openid does not exist');
  36. }
  37. try {
  38. $res = MiniProgramService::sendSubscribeTemlate($this->openId, $tempid, $data, $this->toUrl);
  39. $this->clear();
  40. return $res;
  41. } catch (\Throwable $e) {
  42. $this->isLog() && Log::error('发送给openid为:' . $this->openId . '小程序订阅消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
  43. return $this->setError($e->getMessage());
  44. }
  45. }
  46. public function delete(string $templateId)
  47. {
  48. // TODO: Implement delete() method.
  49. }
  50. public function add(string $shortId)
  51. {
  52. // TODO: Implement add() method.
  53. }
  54. public function list()
  55. {
  56. // TODO: Implement list() method.
  57. }
  58. }