RoutineTemplateService.php 905 B

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