NewUserPushMsg.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Jobs\Push;
  3. use App\Modules\Push\Models\QappNewUserPushTask;
  4. use App\Modules\Push\Models\QappNewUserPushTaskLog;
  5. use App\Modules\Push\Models\QappPushUser;
  6. use App\Modules\Push\Services\PushMessageService;
  7. use App\Modules\Trade\Models\Order;
  8. use App\Modules\User\Models\QappUserAddDestop;
  9. use App\Modules\User\Models\User;
  10. use Illuminate\Bus\Queueable;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. class NewUserPushMsg implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. const condition = [
  19. 'balance' => [
  20. ['name' => '低于500', 'value' => 'a', 'condition' => [0, 500]],
  21. ['name' => '500-2000', 'value' => 'b', 'condition' => [500, 2000]],
  22. ['name' => '2000-5000', 'value' => 'c', 'condition' => [2000, 5000]],
  23. ['name' => '5000以上', 'value' => 'd', 'condition' => [5000, 0]],
  24. ['name' => '不限', 'value' => 'z', 'condition' => []]
  25. ],
  26. 'paid' => [
  27. ['name' => '不限', 'value' => 'z', 'condition' => []],
  28. ['name' => '未付费', 'value' => 'a', 'condition' => ['unpaid']],
  29. ['name' => '已付费', 'value' => 'b', 'condition' => ['paid']],
  30. ['name' => 'VIP用户', 'value' => 'c', 'condition' => ['vip']],
  31. ],
  32. 'add_desktop' => [
  33. ['name' => '不限', 'value' => 'z', 'condition' => []],
  34. ['name' => '未加桌', 'value' => 'a', 'condition' => [0]],
  35. ['name' => '已加桌', 'value' => 'b', 'condition' => [1]],
  36. ]
  37. ];
  38. private $task;
  39. private $uid;
  40. /**
  41. * Create a new job instance.
  42. *
  43. * @return void
  44. */
  45. public function __construct(int $uid, QappNewUserPushTask $task)
  46. {
  47. $this->uid = $uid;
  48. $this->task = $task;
  49. }
  50. private function createLog(): QappNewUserPushTaskLog
  51. {
  52. return QappNewUserPushTaskLog::create([
  53. 'task_id' => $this->task->id,
  54. 'app_id' => '',
  55. 'uid' => $this->uid,
  56. 'status' => 1,
  57. ]);
  58. }
  59. /**
  60. * Execute the job.
  61. *
  62. * @return void
  63. */
  64. public function handle()
  65. {
  66. if (stripos($this->task->providers, $this->findUserProvider($this->uid)) >= 0) {
  67. $log = $this->createLog();
  68. $filter = $this->fliterUser($this->task->push_filter, $this->uid);
  69. if ($filter) {
  70. $result = null;
  71. try {
  72. $result = PushMessageService::pushMessageToUser($this->uid, $this->task->title, $this->task->content, $this->task->url);
  73. }catch (\Exception $e){
  74. myLog('NewUserPushMsg')->error($e);
  75. }
  76. $log->push_time = now();
  77. if ($result) {
  78. $log->status = 3;
  79. $log->push_result = json_encode($result);
  80. } else {
  81. $log->status = 4;
  82. }
  83. } else {
  84. $log->status = 5;
  85. }
  86. $log->save();
  87. }
  88. }
  89. /**
  90. * 查找用户机型
  91. */
  92. private function findUserProvider(int $uid)
  93. {
  94. $push_user = QappPushUser::where('uid', $uid)->first();
  95. return $push_user ? $push_user->provider : '';
  96. }
  97. private function findUserBalance(int $uid): int
  98. {
  99. $user = User::find($uid);
  100. return $user ? $user->balance : 0;
  101. }
  102. private function fliterUser(string $filter, int $uid)
  103. {
  104. $result = true;
  105. $conditions = json_decode($filter, true);
  106. if ($conditions && is_array($conditions)) {
  107. foreach ($conditions as $key => $value) {
  108. $condition = $this->getCondition($key, $value);
  109. if ($result && $condition) {
  110. switch ($key) {
  111. case 'balance':
  112. $balance = $this->findUserBalance($uid);
  113. $result = $condition[0] ? $balance >= $condition[0] && ($condition[1] ? $balance <= $condition[1] : true) : true;
  114. break;
  115. case 'paid':
  116. switch ($value) {
  117. case 'unpaid':
  118. $result = !Order::where('uid', $uid)->where('status', 'PAID')->exists();
  119. break;
  120. case 'paid':
  121. $result = Order::where('uid', $uid)->where('status', 'PAID')->exists();
  122. break;
  123. case 'vip':
  124. $result = Order::where('uid', $uid)->where('status', 'PAID')->where('order_type', 'YEAR')->exists();
  125. break;
  126. }
  127. break;
  128. case 'add_desktop':
  129. $result = ($value == QappUserAddDestop::where('uid', $uid)->where('status', 1)->exists());
  130. break;
  131. }
  132. }
  133. }
  134. }
  135. return $result;
  136. }
  137. private function getCondition($key, $value)
  138. {
  139. if (array_key_exists($key, self::condition))
  140. foreach (self::condition[$key] as $item) {
  141. if ($item['value'] == $value) {
  142. return $item['condition'];
  143. }
  144. }
  145. return '';
  146. }
  147. }