Subscribe.php 1.8 KB

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