SendNewsJob.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 crmeb\jobs;
  12. use app\common\repositories\user\UserRepository;
  13. use app\common\repositories\wechat\WechatUserRepository;
  14. use crmeb\interfaces\JobInterface;
  15. use crmeb\services\WechatService;
  16. use think\queue\Job;
  17. class SendNewsJob implements JobInterface
  18. {
  19. public function fire($job, $data)
  20. {
  21. $wechatUserRepository = app()->make(WechatUserRepository::class);
  22. [$id, $news] = $data;
  23. $wechatUid = app()->make(UserRepository::class)->uidByWechatUserId(intval($id));
  24. if (!$wechatUid || !($openId = $wechatUserRepository->idByOpenId((int)$wechatUid))) {
  25. $job->delete();
  26. return;
  27. }
  28. try {
  29. WechatService::create()->staffTo($openId, WechatService::newsMessage($news));
  30. } catch (\Exception $e) {
  31. $job->failed($e);
  32. }
  33. $job->delete();
  34. }
  35. public function failed($data)
  36. {
  37. // TODO: Implement failed() method.
  38. }
  39. }