You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TemplateJob.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\jobs\template;
  3. use crmeb\basic\BaseJobs;
  4. use crmeb\services\template\Template;
  5. use crmeb\traits\QueueTrait;
  6. use think\facade\Log;
  7. use think\facade\Route;
  8. /**
  9. * Class TemplateJob
  10. * @package app\jobs
  11. */
  12. class TemplateJob extends BaseJobs
  13. {
  14. use QueueTrait;
  15. /**
  16. * @param $type
  17. * @param $openid
  18. * @param $tempCode
  19. * @param $data
  20. * @param $link
  21. * @param $color
  22. * @return bool|mixed
  23. */
  24. public function doJob($type, $openid, $tempCode, $data, $link, $color)
  25. {
  26. try {
  27. if (!$openid) return true;
  28. $template = new Template($type ?: 'wechat');
  29. $template->to($openid);
  30. if ($color) {
  31. $template->color($color);
  32. }
  33. if ($link) {
  34. switch ($type) {
  35. case 'wechat':
  36. $link =
  37. sys_config('site_url') . Route::buildUrl($link)
  38. ->suffix('')
  39. ->domain(false)->build();
  40. break;
  41. }
  42. $template->url($link);
  43. }
  44. $res = $template->send($tempCode, $data);
  45. if (!$res) {
  46. $msg = $type == 'wechat' ? '微信模版消息' : '订阅消息';
  47. Log::error($msg . '发送失败,原因:' . $template->getError() . '----参数:' . json_encode(compact('tempCode', 'openid', 'data', 'link')));
  48. }
  49. return true;
  50. } catch (\Exception $e) {
  51. Log::error($e->getMessage());
  52. return true;
  53. }
  54. }
  55. }