RoutineTemplateService.php 986 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-20
  7. *
  8. *
  9. */
  10. namespace crmeb\services;
  11. use app\common\repositories\wechat\WechatUserRepository;
  12. use crmeb\services\template\Template;
  13. /**
  14. * Class WechatTemplateService
  15. * @package crmeb\services
  16. * @author xaboy
  17. * @day 2020-04-20
  18. */
  19. class RoutineTemplateService
  20. {
  21. /**
  22. * 发送模板消息
  23. * @param string $tempCode
  24. * @param int $uid 用户uid
  25. * @param array $data 模板内容
  26. * @param string $link 跳转链接
  27. * @return bool
  28. */
  29. public function sendTemplate(string $tempCode, $uid, array $data, string $link = '')
  30. {
  31. try {
  32. $openid = app()->make(WechatUserRepository::class)->idByOpenId((int)$uid);
  33. if (!$openid) return true;
  34. $template = new Template('subscribe');
  35. return $template->to($openid)->url($link)->send($tempCode, $data);
  36. } catch (\Exception $e) {
  37. return true;
  38. }
  39. }
  40. }