OutPushJob.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\jobs\out;
  3. use app\services\order\StoreOrderRefundServices;
  4. use app\services\order\StoreOrderServices;
  5. use app\services\out\OutAccountServices;
  6. use app\services\user\UserServices;
  7. use crmeb\basic\BaseJobs;
  8. use crmeb\services\CacheService;
  9. use crmeb\services\HttpService;
  10. use crmeb\traits\QueueTrait;
  11. use think\facade\Log;
  12. class OutPushJob extends BaseJobs
  13. {
  14. use QueueTrait;
  15. public function push($type, $data)
  16. {
  17. /** @var OutAccountServices $outAccountServices */
  18. $outAccountServices = app()->make(OutAccountServices::class);
  19. $outAccountList = $outAccountServices->selectList(['is_del' => 0, 'status' => 1])->toArray();
  20. foreach ($outAccountList as $item) {
  21. if ($item['push_open'] == 1) {
  22. $token = $this->getPushToken($item);
  23. if ($type == 'order_create_push') {
  24. OutPushJob::dispatchDo('orderCreate', [$data['order_id'], $item['order_create_push'] . '?pushToken=' . $token]);
  25. } elseif ($type == 'order_pay_push') {
  26. OutPushJob::dispatchDo('paySuccess', [$data['order_id'], $item['order_pay_push'] . '?pushToken=' . $token]);
  27. } elseif ($type == 'refund_create_push') {
  28. OutPushJob::dispatchDo('refundCreate', [$data['order_id'], $item['refund_create_push'] . '?pushToken=' . $token]);
  29. } elseif ($type == 'refund_cancel_push') {
  30. OutPushJob::dispatchDo('refundCancel', [$data['order_id'], $item['refund_cancel_push'] . '?pushToken=' . $token]);
  31. } elseif ($type == 'user_update_push') {
  32. OutPushJob::dispatchDo('userUpdate', [$data, $item['user_update_push'] . '?pushToken=' . $token]);
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. /**
  39. * 获取推送token
  40. * @param array $info
  41. * @return false|mixed
  42. */
  43. public function getPushToken(array $info)
  44. {
  45. $token = CacheService::redisHandler()->get('pushToken' . $info['id']);
  46. if (!$token) {
  47. $param = json_encode(['push_account' => $info['push_account'], 'push_password' => $info['push_password']], JSON_UNESCAPED_UNICODE);
  48. $res = HttpService::postRequest($info['push_token_url'], $param, ['Content-Type:application/json', 'Content-Length:' . strlen($param)], 5);
  49. $res = $res ? json_decode($res, true) : [];
  50. if (!$res || !isset($res['code']) || $res['code'] != 0) {
  51. Log::error(['msg' => $info['title'] . ',获取token失败']);
  52. return false;
  53. }
  54. CacheService::redisHandler()->set('pushToken' . $info['id'], $res['token'], $res['time']);
  55. return $res['token'];
  56. } else {
  57. return $token;
  58. }
  59. }
  60. /**
  61. * 订单推送
  62. * @param int $oid
  63. * @param string $pushUrl
  64. * @param int $step
  65. * @return bool
  66. */
  67. public function orderCreate(int $oid, string $pushUrl, int $step = 0): bool
  68. {
  69. if ($step > 2) {
  70. Log::error('订单' . $oid . '推送失败');
  71. return true;
  72. }
  73. try {
  74. /** @var StoreOrderServices $services */
  75. $services = app()->make(StoreOrderServices::class);
  76. if (!$services->orderCreatePush($oid, $pushUrl)) {
  77. OutPushJob::dispatchSece(($step + 1) * 5, 'orderCreate', [$oid, $pushUrl, $step + 1]);
  78. }
  79. } catch (\Exception $e) {
  80. Log::error('订单' . $oid . '推送失败,失败原因:' . $e->getMessage());
  81. OutPushJob::dispatchSece(($step + 1) * 5, 'orderCreate', [$oid, $pushUrl, $step + 1]);
  82. }
  83. return true;
  84. }
  85. /**
  86. * 订单支付推送
  87. * @param int $oid
  88. * @param string $pushUrl
  89. * @param int $step
  90. * @return bool
  91. */
  92. public function paySuccess(int $oid, string $pushUrl, int $step = 0): bool
  93. {
  94. if ($step > 2) {
  95. Log::error('订单支付' . $oid . '推送失败');
  96. return true;
  97. }
  98. try {
  99. /** @var StoreOrderServices $services */
  100. $services = app()->make(StoreOrderServices::class);
  101. if (!$services->paySuccessPush($oid, $pushUrl)) {
  102. OutPushJob::dispatchSece(($step + 1) * 5, 'paySuccess', [$oid, $pushUrl, $step + 1]);
  103. }
  104. } catch (\Exception $e) {
  105. Log::error('订单支付' . $oid . '推送失败,失败原因:' . $e->getMessage());
  106. OutPushJob::dispatchSece(($step + 1) * 5, 'paySuccess', [$oid, $pushUrl, $step + 1]);
  107. }
  108. return true;
  109. }
  110. /**
  111. * 售后单生成
  112. * @param int $oid
  113. * @param string $pushUrl
  114. * @param int $step
  115. * @return bool
  116. */
  117. public function refundCreate(int $oid, string $pushUrl, int $step = 0): bool
  118. {
  119. if ($step > 2) {
  120. Log::error('售后单' . $oid . '推送失败');
  121. return true;
  122. }
  123. try {
  124. /** @var StoreOrderRefundServices $services */
  125. $services = app()->make(StoreOrderRefundServices::class);
  126. if (!$services->refundCreatePush($oid, $pushUrl)) {
  127. OutPushJob::dispatchSece(($step + 1) * 5, 'refundCreate', [$oid, $pushUrl, $step + 1]);
  128. }
  129. } catch (\Exception $e) {
  130. Log::error('售后单' . $oid . '推送失败,失败原因:' . $e->getMessage());
  131. OutPushJob::dispatchSece(($step + 1) * 5, 'refundCreate', [$oid, $pushUrl, $step + 1]);
  132. }
  133. return true;
  134. }
  135. /**
  136. * 取消申请
  137. * @param int $oid
  138. * @param string $pushUrl
  139. * @param int $step
  140. * @return bool
  141. */
  142. public function refundCancel(int $oid, string $pushUrl, int $step = 0): bool
  143. {
  144. if ($step > 2) {
  145. Log::error('取消售后单' . $oid . '推送失败');
  146. return true;
  147. }
  148. try {
  149. /** @var StoreOrderRefundServices $services */
  150. $services = app()->make(StoreOrderRefundServices::class);
  151. if (!$services->cancelApplyPush($oid, $pushUrl)) {
  152. OutPushJob::dispatchSece(($step + 1) * 5, 'refundCancel', [$oid, $pushUrl, $step + 1]);
  153. }
  154. } catch (\Exception $e) {
  155. Log::error('取消售后单' . $oid . '推送失败,失败原因:' . $e->getMessage());
  156. OutPushJob::dispatchSece(($step + 1) * 5, 'refundCancel', [$oid, $pushUrl, $step + 1]);
  157. }
  158. return true;
  159. }
  160. /**
  161. * 余额,积分,佣金,经验变动推送
  162. * @param array $data
  163. * @param string $pushUrl
  164. * @param int $step
  165. * @return bool
  166. */
  167. public function userUpdate(array $data, string $pushUrl, int $step = 0): bool
  168. {
  169. if ($step > 2) {
  170. Log::error('用户变动推送失败');
  171. return true;
  172. }
  173. try {
  174. /** @var UserServices $services */
  175. $services = app()->make(UserServices::class);
  176. if (!$services->userUpdate($data, $pushUrl)) {
  177. OutPushJob::dispatchSece(($step + 1) * 5, 'userUpdate', [$data, $pushUrl, $step + 1]);
  178. }
  179. } catch (\Exception $e) {
  180. OutPushJob::dispatchSece(($step + 1) * 5, 'userUpdate', [$data, $pushUrl, $step + 1]);
  181. }
  182. return true;
  183. }
  184. }