UserJob.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\user;
  12. use app\services\activity\lottery\LuckLotteryServices;
  13. use app\services\activity\newcomer\StoreNewcomerServices;
  14. use app\services\message\wechat\MessageServices;
  15. use app\services\user\UserServices;
  16. use app\services\wechat\WechatUserServices;
  17. use app\services\work\WorkClientServices;
  18. use app\services\work\WorkMemberServices;
  19. use crmeb\basic\BaseJobs;
  20. use crmeb\traits\QueueTrait;
  21. use think\facade\Log;
  22. /**
  23. * 同步用户
  24. * Class UserJob
  25. * @package app\jobs
  26. */
  27. class UserJob extends BaseJobs
  28. {
  29. use QueueTrait;
  30. /**
  31. * 执行同步数据后
  32. * @param $openids
  33. * @return bool
  34. */
  35. public function doJob($openids)
  36. {
  37. if (!$openids || !is_array($openids)) {
  38. return true;
  39. }
  40. $noBeOpenids = [];
  41. try {
  42. /** @var WechatUserServices $wechatUser */
  43. $wechatUser = app()->make(WechatUserServices::class);
  44. $noBeOpenids = $wechatUser->syncWechatUser($openids);
  45. } catch (\Throwable $e) {
  46. Log::error('更新wechatUser用户信息失败,失败原因:' . $e->getMessage());
  47. }
  48. if (!$noBeOpenids) {
  49. return true;
  50. }
  51. try {
  52. /** @var UserServices $user */
  53. $user = app()->make(UserServices::class);
  54. $user->importUser($noBeOpenids);
  55. } catch (\Throwable $e) {
  56. Log::error('新增用户失败,失败原因:' . $e->getMessage());
  57. }
  58. return true;
  59. }
  60. /**
  61. * 关注推官新用户(发送抽奖消息 、推广用户增加抽奖次数)
  62. * @param int $uid
  63. * @param string $openid
  64. * @param int $spread_uid
  65. * @return bool
  66. */
  67. public function subscribeSpreadLottery(int $uid, string $openid, int $spread_uid)
  68. {
  69. /** @var LuckLotteryServices $lotteryServices */
  70. $lotteryServices = app()->make(LuckLotteryServices::class);
  71. $lottery = $lotteryServices->getFactorLottery(5);
  72. if (!$lottery) {
  73. return true;
  74. }
  75. try {
  76. /** @var MessageServices $messageServices */
  77. $messageServices = app()->make(MessageServices::class);
  78. $messageServices->wechatEvent(['third_type' => 'luckLottery-' . $uid, 'lottery' => $lottery], $openid);
  79. } catch (\Throwable $e) {
  80. Log::error('发送关注抽奖消息失败,原因:' . $e->getMessage());
  81. }
  82. if (!$spread_uid) {
  83. return true;
  84. }
  85. try {
  86. /** @var UserServices $userServices */
  87. $userServices = app()->make(UserServices::class);
  88. $spreadUser = $userServices->getUserInfo($spread_uid, 'uid,spread_lottery');
  89. //增加推广用户抽奖次数
  90. if (!$spreadUser) {
  91. return true;
  92. }
  93. if ($lottery['lottery_num_term'] == 1) {
  94. $where = ['time' => 'today', 'timeKey' => 'spread_time'];
  95. } else {
  96. $where = ['spread_uid' => $spreadUser['uid']];
  97. }
  98. $spreadCount = $userServices->count($where);
  99. if ($spreadCount) {
  100. $lotterySum = bcmul((string)$spreadCount, (string)$lottery['spread_num'], 0);
  101. if ($lotterySum < $lottery['lottery_num']) {
  102. $update_lottery_num = $spreadUser['spread_lottery'] + (int)$lottery['spread_num'];
  103. if ($update_lottery_num > $lottery['lottery_num']) {
  104. $update_lottery_num = $lottery['lottery_num'];
  105. }
  106. $userServices->update($spreadUser['uid'], ['spread_lottery' => $update_lottery_num], 'uid');
  107. }
  108. }
  109. } catch (\Throwable $e) {
  110. Log::error('增加推广用户抽奖次数失败,原因:' . $e->getMessage());
  111. }
  112. return true;
  113. }
  114. /**
  115. * 绑定企业微信成员
  116. * @param $uid
  117. * @param $unionid
  118. * @return bool
  119. */
  120. public function bindWorkMember($uid, $unionid)
  121. {
  122. if (!$unionid) {
  123. return true;
  124. }
  125. /** @var WorkClientServices $service */
  126. $service = app()->make(WorkClientServices::class);
  127. $clienInfo = $service->get(['unionid' => $unionid], ['id', 'external_userid', 'uid'], ['followOne']);
  128. if ($clienInfo) {
  129. if (!$clienInfo->uid) {
  130. $clienInfo->uid = $uid;
  131. $clienInfo->save();
  132. }
  133. if (!empty($clienInfo->followOne->userid)) {
  134. $userId = $clienInfo->followOne->userid;
  135. /** @var WorkMemberServices $memberService */
  136. $memberService = app()->make(WorkMemberServices::class);
  137. $menber = $memberService->get(['userid' => $userId], ['uid', 'mobile']);
  138. if (!$menber) {
  139. Log::error([
  140. 'message' => '绑定企业微信成员失败:没有查询到成员身份',
  141. 'request' => ['uid' => $uid, 'unionid' => $unionid],
  142. ]);
  143. return true;
  144. }
  145. /** @var UserServices $userService */
  146. $userService = app()->make(UserServices::class);
  147. if (!$menber->uid && $menber->mobile) {
  148. $menberUid = $userService->value(['phone' => $menber->mobile], 'uid');
  149. if ($menberUid && $menberUid != $uid) {
  150. $menber->uid = $menberUid;
  151. $menber->save();
  152. }
  153. }
  154. $userInfo = $userService->get($uid, ['uid', 'work_uid', 'work_userid']);
  155. if (!$userInfo) {
  156. return true;
  157. }
  158. if (!$userInfo->work_userid) {
  159. $userInfo->work_userid = $userId;
  160. $userInfo->work_uid = $menber->uid;
  161. $userInfo->save();
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. /**
  168. * 设置用户会员卡激活状态
  169. * @param $uid
  170. * @return bool
  171. */
  172. public function setUserLevel($uid)
  173. {
  174. if (!(int)$uid) {
  175. return true;
  176. }
  177. try {
  178. /** @var StoreNewcomerServices $newcomerServices */
  179. $newcomerServices = app()->make(StoreNewcomerServices::class);
  180. $newcomerServices->setUserLevel((int)$uid);
  181. } catch (\Throwable $e) {
  182. Log::error('设置用户会员卡激活状态失败,失败原因:' . $e->getMessage());
  183. }
  184. return true;
  185. }
  186. /**
  187. * 设置用户首单优惠、新人专享
  188. * @param $uid
  189. * @return bool
  190. */
  191. public function setUserNewcomer($uid)
  192. {
  193. if (!(int)$uid) {
  194. return true;
  195. }
  196. try {
  197. /** @var StoreNewcomerServices $newcomerServices */
  198. $newcomerServices = app()->make(StoreNewcomerServices::class);
  199. $newcomerServices->setUserNewcomer((int)$uid);
  200. } catch (\Throwable $e) {
  201. Log::error('设置用户首单优惠、新人专享失败,失败原因:' . $e->getMessage());
  202. }
  203. return true;
  204. }
  205. /**
  206. * 下单修改
  207. * @param $uid
  208. * @return bool|void
  209. */
  210. public function updateUserNewcomer($uid, $order)
  211. {
  212. if (!(int)$uid) {
  213. return true;
  214. }
  215. try {
  216. /** @var StoreNewcomerServices $newcomerServices */
  217. $newcomerServices = app()->make(StoreNewcomerServices::class);
  218. $newcomerServices->updateUserNewcomer((int)$uid, $order);
  219. } catch (\Throwable $e) {
  220. Log::error('修改用户首单优惠、新人专享使用状态失败,失败原因:' . $e->getMessage());
  221. }
  222. }
  223. }