UserSubscribe.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace crmeb\subscribes;
  3. use app\models\user\User;
  4. use app\models\user\WechatUser;
  5. use think\facade\Cookie;
  6. use app\admin\model\system\SystemAttachment;
  7. use app\models\user\UserLevel;
  8. use think\facade\Log;
  9. /**
  10. * 用户事件
  11. * Class UserSubscribe
  12. * @package crmeb\subscribes
  13. */
  14. class UserSubscribe
  15. {
  16. public function handle()
  17. {
  18. }
  19. /**
  20. * 管理员后台给用户添加金额 AdminAddMoney
  21. * @param $event
  22. */
  23. public function onAdminAddMoney($event)
  24. {
  25. list($user, $money) = $event;
  26. Log::warning('EVENT admin add money ' . $money . ' to ' . $user['uid']);
  27. }
  28. /**
  29. * 微信授权成功后 WechatOauthAfter
  30. * @param $event
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. */
  35. public function onWechatOauthAfter($event)
  36. {
  37. list($openid, $wechatInfo, $spreadId, $login_type) = $event;
  38. if (!User::be(['uid' => $spreadId])) $spreadId = 0;
  39. $wechatInfo['nickname'] = filter_emoji($wechatInfo['nickname']);
  40. Cookie::set('is_login', 1);
  41. if (isset($wechatInfo['unionid']) && $wechatInfo['unionid'] != '' && ($uid = WechatUser::where('unionid', $wechatInfo['unionid'])->where('user_type', '<>', 'h5')->value('uid'))) {
  42. WechatUser::edit($wechatInfo, $uid, 'uid');
  43. if (!User::be(['uid' => $uid])) {
  44. $wechatInfo = WechatUser::where('uid', $uid)->find();
  45. User::setWechatUser($wechatInfo, $spreadId);
  46. } else {
  47. if ($login_type) $wechatInfo['login_type'] = $login_type;
  48. User::updateWechatUser($wechatInfo, $uid);
  49. }
  50. } else if ($uid = WechatUser::where(['openid' => $wechatInfo['openid']])->where('user_type', '<>', 'h5')->value('uid')) {
  51. WechatUser::edit($wechatInfo, $uid, 'uid');
  52. if ($login_type) $wechatInfo['login_type'] = $login_type;
  53. User::updateWechatUser($wechatInfo, $uid);
  54. } else {
  55. if (isset($wechatInfo['subscribe_scene'])) unset($wechatInfo['subscribe_scene']);
  56. if (isset($wechatInfo['qr_scene'])) unset($wechatInfo['qr_scene']);
  57. if (isset($wechatInfo['qr_scene_str'])) unset($wechatInfo['qr_scene_str']);
  58. // $isLogin = request()->isLogin();
  59. // $bind = false;
  60. // if($isLogin){
  61. // $loginUid = request()->user();
  62. // $isUser = $loginUid ? request()->tokenData()->type === 'user' : false;
  63. // $bind = $loginUid && $isUser && !$loginUid->openid && !User::be(['openid' => $wechatInfo['openid']]);
  64. // //微信用户绑定 h5用户
  65. // if ($bind) {
  66. // $wechatInfo['uid'] = $loginUid->uid;
  67. // };
  68. // }
  69. $wechatInfo = WechatUser::create($wechatInfo);
  70. // if ($isLogin && $bind)
  71. // User::where('uid', $wechatInfo['uid'])
  72. // ->limit(1)->update(['openid' => $wechatInfo['openid']]);
  73. // else
  74. User::setWechatUser($wechatInfo, $spreadId);
  75. }
  76. $uid = WechatUser::openidToUid($openid, 'openid');
  77. // 设置推广关系
  78. User::setSpread($spreadId, $uid);
  79. User::where('uid', $uid)
  80. ->limit(1)->update(['last_time' => time(), 'last_ip' => app('request')->ip()]);
  81. }
  82. /**
  83. * 用户登录成功 UserLogin
  84. * @param $event
  85. */
  86. public function onUserLogin($event)
  87. {
  88. list($userInfo) = $event;
  89. $request = app('request');
  90. User::edit(['last_time' => time(), 'last_ip' => $request->ip()], $userInfo->uid, 'uid');
  91. Log::debug('EVENT user ' . $userInfo->uid . ' login.');
  92. }
  93. /**
  94. * 检查是否能成为会员 UserLevelAfter
  95. * @param $event
  96. */
  97. public function onUserLevelAfter($event)
  98. {
  99. list($userUid) = $event;
  100. try {
  101. if(UserLevel::setLevelComplete($userUid)) {
  102. event('UserLevelUp', $event);
  103. }
  104. } catch (\Exception $e) {
  105. }
  106. }
  107. /**
  108. * 用户升级成功 UserLevelUp
  109. */
  110. public function onUserLevelUp($event)
  111. {
  112. list($userUid) = $event;
  113. $userinfo = User::get($userUid);
  114. Log::debug("EVENT user " . $userUid . ' update to level ' . $userinfo['level']);
  115. }
  116. /**
  117. * 新用戶注冊 UserRegistered
  118. */
  119. public function onUserRegistered($event)
  120. {
  121. list('user' => $user) = $event;
  122. Log::debug('EVENT new user registered:' . $user['uid']);
  123. }
  124. /**
  125. * 首次下訂單 UserFirstOrder
  126. */
  127. public function onUserFirstOrder($event)
  128. {
  129. list('order' => $order) = $event;
  130. Log::debug('EVENT user ' . $order['uid'] . ' first order:' . $order['id']);
  131. }
  132. /**
  133. * 用戶提現 UserRequestWithdrawal
  134. */
  135. public function onUserRequestWithdrawal($event)
  136. {
  137. list('user' => $user, 'info' => $info) = $event;
  138. Log::debug('EVENT user ' . $user['uid'] . ' withdraw ' . mapped_implode(',', $info));
  139. }
  140. /**
  141. * 首次挖礦得到弊 UserFirstDig
  142. */
  143. public function onUserFirstDig($event)
  144. {
  145. }
  146. /**
  147. * 用户提币 UserWithdrawalCoin
  148. */
  149. public function onUserWithdrawalCoin($event)
  150. {
  151. }
  152. /**
  153. * 用户参加活动 UserActivity
  154. */
  155. public function onUserActivity($event)
  156. {
  157. }
  158. }