OrderJob.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace crmeb\jobs;
  3. use crmeb\interfaces\JobInterface;
  4. use think\facade\Db;
  5. use think\queue\Job;
  6. class OrderJob implements JobInterface
  7. {
  8. public function fire(Job $job, $data): void
  9. {
  10. $doDefaultJod = $data['method'] ?? null;
  11. $beforeMethod = $data['beforeMethod'] ?? null;
  12. $jobData = $data['data'] ?? null;
  13. $errorTimes = $data['errorTimes'] ?? 0;
  14. $release = $data['release'] ?? 0;
  15. if (method_exists($this, $beforeMethod)) {
  16. $isJobStillNeedToBeDone = $this->{$beforeMethod}($jobData);
  17. if ($isJobStillNeedToBeDone) {
  18. $job->delete();
  19. return;
  20. }
  21. }
  22. if (method_exists($this, $doDefaultJod)) {
  23. $isJobDone = $this->{$doDefaultJod}($jobData);
  24. if ($isJobDone) {
  25. $job->delete();
  26. return;
  27. } else {
  28. if ($job->attempts() > $errorTimes && $errorTimes) {
  29. $job->delete();
  30. return;
  31. } else {
  32. $job->release($release);
  33. }
  34. }
  35. }
  36. }
  37. public function doDefaultJod($data): bool
  38. {
  39. try {
  40. [$order, $formId] = $data;
  41. Db::name('cache')->insert(['key'=>'test_'.rand(10,20),'result'=>json_encode($order)]);
  42. event('OrderPaySuccess', [$order, $formId]);
  43. } catch (\Throwable $e) {
  44. }
  45. return true;
  46. }
  47. }