NewOpenidNotice4Job.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Fpdx\PairModel;
  4. use App\Models\User\AuthKey;
  5. use App\Models\User\UserModel;
  6. use App\Services\QQMiniApp\Template;
  7. use Carbon\Carbon;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. /**
  14. * Class NewOpenidNotice4Job
  15. * @package App\Jobs
  16. */
  17. class NewOpenidNotice4Job implements ShouldQueue
  18. {
  19. use Dispatchable;
  20. use InteractsWithQueue;
  21. use Queueable;
  22. use SerializesModels;
  23. private $openid;
  24. /**
  25. * @var int 当前执行次数
  26. */
  27. private $execCnt;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @param string $openid
  32. * @param int $execCnt
  33. */
  34. public function __construct(string $openid, int $execCnt = 0)
  35. {
  36. $this->openid = $openid;
  37. $this->execCnt = $execCnt;
  38. }
  39. /**
  40. * Execute the job.
  41. *
  42. * @return bool
  43. */
  44. public function handle()
  45. {
  46. if ($this->sendQQ($this->openid)) {
  47. return true;
  48. } else {
  49. if ($this->execCnt < 3) {
  50. NewOpenidNotice4Job::dispatch($this->openid, $this->execCnt + 1)->delay(Carbon::now()->addDay(1));
  51. }
  52. }
  53. }
  54. /**
  55. * @param string $openid
  56. * @return bool
  57. */
  58. public function sendQQ(string $openid)
  59. {
  60. try {
  61. $auth = AuthKey::where('auth_key', $openid)->first();
  62. if (collect($auth)->isEmpty()) {
  63. } else {
  64. try {
  65. $user = UserModel::findOrFail($auth->uid);
  66. if ($user->login_at > mktime(0, 0, 0)) {
  67. return true;
  68. }
  69. if (PairModel::where('uid', $user->uid)->exists() || $user->partner_id > 0) {
  70. return true;
  71. }
  72. } catch (\Exception $exception) {
  73. }
  74. }
  75. $templates = array(
  76. [
  77. 'template_id' => '8a4e8c0f73be381f49935dd20d79b6b9',
  78. 'data' => [
  79. 'keyword1' => array(
  80. 'value' => '交友信息待完善'
  81. ),
  82. 'keyword2' => array(
  83. 'value' => "完善你的交友信息,小象就能把你推荐给更多与你契合的人,助你快速脱单~"
  84. ),
  85. 'keyword3' => array(
  86. 'value' => "暂无访客"
  87. )
  88. ]
  89. ]
  90. );
  91. $template = $templates[rand(0, count($templates) - 1)];
  92. $title = '新用户引导隔3日通知';
  93. $page = "pages/my-friend-card-editor/my-friend-card-editor";
  94. $app = new Template(config("qqprogram.app_id"));
  95. $app->setTitle($title)
  96. ->setUuid(uuid())
  97. ->setParameters($template['data'])
  98. ->setTemplateId($template['template_id'])
  99. ->setPage($page)
  100. ->toUser($openid)
  101. ->send();
  102. if (0 == $app->getCode()) {
  103. return true;
  104. }
  105. return false;
  106. } catch (\Exception $exception) {
  107. return false;
  108. }
  109. }
  110. }