Subscribe.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * @param string $templateId
  20. * @return mixed
  21. */
  22. public function getTempId(string $templateId)
  23. {
  24. return Db::name('template_message')->where(['type' => 0, 'tempkey' => $templateId, 'status' => 1])->value('tempid');
  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::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. }