TemplateJob.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\notice\template;
  12. use crmeb\basic\BaseJobs;
  13. use crmeb\services\template\Template;
  14. use crmeb\traits\QueueTrait;
  15. use think\facade\Log;
  16. use think\facade\Route;
  17. /**
  18. * Class TemplateJob
  19. * @package app\jobs
  20. */
  21. class TemplateJob extends BaseJobs
  22. {
  23. use QueueTrait;
  24. /**
  25. * @param $type
  26. * @param $openid
  27. * @param $tempCode
  28. * @param $data
  29. * @param $link
  30. * @param $color
  31. * @return bool|mixed
  32. */
  33. public function doJob($type, $openid, $tempCode, $data, $link, $color)
  34. {
  35. try {
  36. if (!$openid) return true;
  37. $template = new Template($type ?: 'wechat');
  38. $template->to($openid);
  39. if ($color) {
  40. $template->color($color);
  41. }
  42. if ($link) {
  43. switch ($type) {
  44. case 'wechat':
  45. $link =
  46. sys_config('site_url') . Route::buildUrl($link)
  47. ->suffix('')
  48. ->domain(false)->build();
  49. break;
  50. }
  51. $template->url($link);
  52. }
  53. $res = $template->send($tempCode, $data);
  54. if (!$res) {
  55. $msg = $type == 'wechat' ? '微信模版消息' : '订阅消息';
  56. Log::error($msg . '发送失败,原因:' . $template->getError() . '----参数:' . json_encode(compact('tempCode', 'openid', 'data', 'link')));
  57. }
  58. return true;
  59. } catch (\Exception $e) {
  60. Log::error($e->getMessage(), [
  61. 'file' => $e->getFile(),
  62. 'line' => $e->getLine()
  63. ]);
  64. return true;
  65. }
  66. }
  67. }