You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserJob.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\jobs\user;
  3. use app\services\activity\lottery\LuckLotteryServices;
  4. use app\services\message\wechat\MessageServices;
  5. use app\services\user\UserServices;
  6. use app\services\wechat\WechatUserServices;
  7. use app\services\work\WorkClientServices;
  8. use app\services\work\WorkMemberServices;
  9. use crmeb\basic\BaseJobs;
  10. use crmeb\traits\QueueTrait;
  11. use think\facade\Log;
  12. /**
  13. * 同步用户
  14. * Class UserJob
  15. * @package app\jobs
  16. */
  17. class UserJob extends BaseJobs
  18. {
  19. use QueueTrait;
  20. /**
  21. * 执行同步数据后
  22. * @param $openids
  23. * @return bool
  24. */
  25. public function doJob($openids)
  26. {
  27. if (!$openids || !is_array($openids)) {
  28. return true;
  29. }
  30. $noBeOpenids = [];
  31. try {
  32. /** @var WechatUserServices $wechatUser */
  33. $wechatUser = app()->make(WechatUserServices::class);
  34. $noBeOpenids = $wechatUser->syncWechatUser($openids);
  35. } catch (\Throwable $e) {
  36. Log::error('更新wechatUser用户信息失败,失败原因:' . $e->getMessage());
  37. }
  38. if (!$noBeOpenids) {
  39. return true;
  40. }
  41. try {
  42. /** @var UserServices $user */
  43. $user = app()->make(UserServices::class);
  44. $user->importUser($noBeOpenids);
  45. } catch (\Throwable $e) {
  46. Log::error('新增用户失败,失败原因:' . $e->getMessage());
  47. }
  48. return true;
  49. }
  50. /**
  51. * 关注推官新用户(发送抽奖消息 、推广用户增加抽奖次数)
  52. * @param int $uid
  53. * @param string $openid
  54. * @param int $spread_uid
  55. * @return bool
  56. */
  57. public function subscribeSpreadLottery(int $uid, string $openid, int $spread_uid)
  58. {
  59. /** @var LuckLotteryServices $lotteryServices */
  60. $lotteryServices = app()->make(LuckLotteryServices::class);
  61. $lottery = $lotteryServices->getFactorLottery(5);
  62. if (!$lottery) {
  63. return true;
  64. }
  65. try {
  66. /** @var MessageServices $messageServices */
  67. $messageServices = app()->make(MessageServices::class);
  68. $messageServices->wechatEvent(['third_type' => 'luckLottery-' . $uid, 'lottery' => $lottery], $openid);
  69. } catch (\Throwable $e) {
  70. Log::error('发送关注抽奖消息失败,原因:' . $e->getMessage());
  71. }
  72. if (!$spread_uid) {
  73. return true;
  74. }
  75. try {
  76. /** @var UserServices $userServices */
  77. $userServices = app()->make(UserServices::class);
  78. $spreadUser = $userServices->getUserInfo($spread_uid, 'uid,spread_lottery');
  79. //增加推广用户抽奖次数
  80. if (!$spreadUser) {
  81. return true;
  82. }
  83. if ($lottery['lottery_num_term'] == 1) {
  84. $where = ['time' => 'today', 'timeKey' => 'spread_time'];
  85. } else {
  86. $where = ['spread_uid' => $spreadUser['uid']];
  87. }
  88. $spreadCount = $userServices->count($where);
  89. if ($spreadCount) {
  90. $lotterySum = bcmul((string)$spreadCount, (string)$lottery['spread_num'], 0);
  91. if ($lotterySum < $lottery['lottery_num']) {
  92. $update_lottery_num = $spreadUser['spread_lottery'] + (int)$lottery['spread_num'];
  93. if ($update_lottery_num > $lottery['lottery_num']) {
  94. $update_lottery_num = $lottery['lottery_num'];
  95. }
  96. $userServices->update($spreadUser['uid'], ['spread_lottery' => $update_lottery_num], 'uid');
  97. }
  98. }
  99. } catch (\Throwable $e) {
  100. Log::error('增加推广用户抽奖次数失败,原因:' . $e->getMessage());
  101. }
  102. return true;
  103. }
  104. /**
  105. * 绑定企业微信成员
  106. * @param $uid
  107. * @param $unionid
  108. * @return bool
  109. */
  110. public function bindWorkMember($uid, $unionid)
  111. {
  112. if (!$unionid) {
  113. return true;
  114. }
  115. /** @var WorkClientServices $service */
  116. $service = app()->make(WorkClientServices::class);
  117. $clienInfo = $service->get(['unionid' => $unionid], ['id', 'external_userid', 'uid'], ['followOne']);
  118. if ($clienInfo) {
  119. if (!$clienInfo->uid) {
  120. $clienInfo->uid = $uid;
  121. $clienInfo->save();
  122. }
  123. if (!empty($clienInfo->followOne->userid)) {
  124. $userId = $clienInfo->followOne->userid;
  125. /** @var WorkMemberServices $memberService */
  126. $memberService = app()->make(WorkMemberServices::class);
  127. $menber = $memberService->get(['userid' => $userId], ['uid', 'mobile']);
  128. if (!$menber) {
  129. Log::error([
  130. 'message' => '绑定企业微信成员失败:没有查询到成员身份',
  131. 'request' => ['uid' => $uid, 'unionid' => $unionid],
  132. ]);
  133. return true;
  134. }
  135. /** @var UserServices $userService */
  136. $userService = app()->make(UserServices::class);
  137. if (!$menber->uid && $menber->mobile) {
  138. $menberUid = $userService->value(['phone' => $menber->mobile], 'uid');
  139. if ($menberUid && $menberUid != $uid) {
  140. $menber->uid = $menberUid;
  141. $menber->save();
  142. }
  143. }
  144. $userInfo = $userService->get($uid, ['uid', 'work_uid', 'work_userid']);
  145. if (!$userInfo) {
  146. return true;
  147. }
  148. if (!$userInfo->work_userid) {
  149. $userInfo->work_userid = $userId;
  150. $userInfo->work_uid = $menber->uid;
  151. $userInfo->save();
  152. }
  153. }
  154. }
  155. return true;
  156. }
  157. }