TemplateJob.php 2.5 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;
  12. use app\services\message\SystemNotificationServices;
  13. use crmeb\basic\BaseJobs;
  14. use crmeb\services\CacheService;
  15. use crmeb\services\template\Template;
  16. use crmeb\traits\QueueTrait;
  17. use think\facade\Log;
  18. use think\facade\Route;
  19. /**
  20. * Class TemplateJob
  21. * @package app\jobs
  22. */
  23. class TemplateJob extends BaseJobs
  24. {
  25. use QueueTrait;
  26. /**
  27. * @param $type
  28. * @param $openid
  29. * @param $tempCode
  30. * @param $data
  31. * @param $link
  32. * @param $color
  33. * @return bool|mixed
  34. */
  35. public function doJob($type, $openid, $tempCode, $data, $link, $color)
  36. {
  37. try {
  38. if (!$openid) return true;
  39. $template = new Template($type ?: 'wechat');
  40. $template->to($openid);
  41. if ($color) {
  42. $template->color($color);
  43. }
  44. if ($link) {
  45. switch ($type) {
  46. case 'wechat':
  47. $link = sys_config('site_url') . Route::buildUrl($link)->suffix('')->domain(false)->build();
  48. break;
  49. default:
  50. break;
  51. }
  52. $template->url($link);
  53. }
  54. $tempid = CacheService::get('wechat_'.$tempCode, function () use ($type, $tempCode) {
  55. //判断小程序还是公众号,获取数据id
  56. $is_type = $type == 'wechat' ? 'is_wechat' : 'is_routine';
  57. /** @var SystemNotificationServices $notifyServices */
  58. $notifyServices = app()->make(SystemNotificationServices::class);
  59. return $notifyServices->getNotInfo(['type' => $is_type, 'mark' => $tempCode])['tempid'];
  60. });
  61. return $template->send($tempid, $data);
  62. } catch (\Exception $e) {
  63. Log::error($e->getMessage());
  64. return true;
  65. }
  66. }
  67. }