TemplateJob.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. //判断小程序还是公众号,获取数据id
  55. $is_type = $type == 'wechat' ? 'is_wechat' : 'is_routine';
  56. $key = $is_type == 'is_wechat' ? 'wechat_' . $tempCode : 'routine_' . $tempCode;
  57. $tempid = CacheService::remember($key, function () use ($type, $tempCode, $is_type) {
  58. /** @var SystemNotificationServices $notifyServices */
  59. $notifyServices = app()->make(SystemNotificationServices::class);
  60. return $notifyServices->getNotInfo(['type' => $is_type, 'mark' => $tempCode])['tempid'];
  61. });
  62. return $template->send($tempid, $data);
  63. } catch (\Exception $e) {
  64. Log::error($e->getMessage());
  65. return true;
  66. }
  67. }
  68. }