UserSubscribe.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 app\models\user\UserNotice;
  9. use think\facade\Log;
  10. use think\facade\Config;
  11. /**
  12. * 用户事件
  13. * Class UserSubscribe
  14. * @package crmeb\subscribes
  15. */
  16. class UserSubscribe
  17. {
  18. public function handle()
  19. {
  20. }
  21. /**
  22. * 管理员后台给用户添加金额 AdminAddMoney
  23. * @param $event
  24. */
  25. public function onAdminAddMoney($event)
  26. {
  27. list($user, $money) = $event;
  28. Log::warning('EVENT admin add money ' . $money . ' to ' . $user['uid']);
  29. }
  30. /**
  31. * 微信授权成功后 WechatOauthAfter
  32. * @param $event
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. */
  37. public function onWechatOauthAfter($event)
  38. {
  39. list($openid, $wechatInfo, $spreadId, $login_type) = $event;
  40. if (!User::be(['uid' => $spreadId])) $spreadId = 0;
  41. $wechatInfo['nickname'] = filter_emoji($wechatInfo['nickname']);
  42. Cookie::set('is_login', 1);
  43. if (isset($wechatInfo['unionid']) && $wechatInfo['unionid'] != '' && ($uid = WechatUser::where('unionid', $wechatInfo['unionid'])->where('user_type', '<>', 'h5')->value('uid'))) {
  44. WechatUser::edit($wechatInfo, $uid, 'uid');
  45. if (!User::be(['uid' => $uid])) {
  46. $wechatInfo = WechatUser::where('uid', $uid)->find();
  47. User::setWechatUser($wechatInfo, $spreadId);
  48. } else {
  49. if ($login_type) $wechatInfo['login_type'] = $login_type;
  50. User::updateWechatUser($wechatInfo, $uid);
  51. }
  52. } else if ($uid = WechatUser::where(['openid' => $wechatInfo['openid']])->where('user_type', '<>', 'h5')->value('uid')) {
  53. WechatUser::edit($wechatInfo, $uid, 'uid');
  54. if ($login_type) $wechatInfo['login_type'] = $login_type;
  55. User::updateWechatUser($wechatInfo, $uid);
  56. } else {
  57. if (isset($wechatInfo['subscribe_scene'])) unset($wechatInfo['subscribe_scene']);
  58. if (isset($wechatInfo['qr_scene'])) unset($wechatInfo['qr_scene']);
  59. if (isset($wechatInfo['qr_scene_str'])) unset($wechatInfo['qr_scene_str']);
  60. // $isLogin = request()->isLogin();
  61. // $bind = false;
  62. // if($isLogin){
  63. // $loginUid = request()->user();
  64. // $isUser = $loginUid ? request()->tokenData()->type === 'user' : false;
  65. // $bind = $loginUid && $isUser && !$loginUid->openid && !User::be(['openid' => $wechatInfo['openid']]);
  66. // //微信用户绑定 h5用户
  67. // if ($bind) {
  68. // $wechatInfo['uid'] = $loginUid->uid;
  69. // };
  70. // }
  71. $wechatInfo = WechatUser::create($wechatInfo);
  72. // if ($isLogin && $bind)
  73. // User::where('uid', $wechatInfo['uid'])
  74. // ->limit(1)->update(['openid' => $wechatInfo['openid']]);
  75. // else
  76. User::setWechatUser($wechatInfo, $spreadId);
  77. }
  78. $uid = WechatUser::openidToUid($openid, 'openid');
  79. // 设置推广关系
  80. User::setSpread($spreadId, $uid);
  81. User::where('uid', $uid)
  82. ->limit(1)->update(['last_time' => time(), 'last_ip' => app('request')->ip()]);
  83. }
  84. /**
  85. * 用户登录成功 UserLogin
  86. * @param $event
  87. */
  88. public function onUserLogin($event)
  89. {
  90. list($userInfo) = $event;
  91. $request = app('request');
  92. User::edit(['last_time' => time(), 'last_ip' => $request->ip()], $userInfo->uid, 'uid');
  93. Log::debug('EVENT user ' . $userInfo->uid . ' login.');
  94. }
  95. /**
  96. * 检查是否能成为会员 UserLevelAfter
  97. * @param $event
  98. */
  99. public function onUserLevelAfter($event)
  100. {
  101. list($userUid) = $event;
  102. try {
  103. if(UserLevel::setLevelComplete($userUid)) {
  104. event('UserLevelUp', $event);
  105. }
  106. } catch (\Exception $e) {
  107. }
  108. }
  109. /**
  110. * 用户升级成功 UserLevelUp
  111. */
  112. public function onUserLevelUp($event)
  113. {
  114. list($userUid) = $event;
  115. $userinfo = User::get($userUid);
  116. Log::debug("EVENT user " . $userUid . ' update to level ' . $userinfo['level']);
  117. }
  118. /**
  119. * 新用戶注冊 UserRegistered
  120. */
  121. public function onUserRegistered($event)
  122. {
  123. list('user' => $user) = $event;
  124. Log::debug('EVENT new user registered:' . $user['uid']);
  125. }
  126. /**
  127. * 首次下訂單 UserFirstOrder
  128. */
  129. public function onUserFirstOrder($event)
  130. {
  131. list('order' => $order) = $event;
  132. Log::debug('EVENT user ' . $order['uid'] . ' first order:' . $order['id']);
  133. }
  134. /**
  135. * 用戶提現 UserRequestWithdrawal
  136. */
  137. public function onUserRequestWithdrawal($event)
  138. {
  139. list('user' => $user, 'info' => $info) = $event;
  140. Log::debug('EVENT user ' . $user['uid'] . ' withdraw ' . mapped_implode(',', $info));
  141. }
  142. /**
  143. * 後臺批準提現 UserExtractSucc
  144. */
  145. public function onUserExtractSucc($event)
  146. {
  147. list($extract) = $event;
  148. $icon = Config::get('app.header_cs_2', '');
  149. UserNotice::sendNoticeTo($extract['uid'], '您的提现申请已处理',
  150. '您申请提现 ' . $extract['extract_price']
  151. . ' 元已转账,请耐心等待收款方系统确认后核对金额。衷心感谢您对美天旺的信任和支持。如有问题,请联系客服。', $icon);
  152. }
  153. /**
  154. * 後臺拒絕提現 UserExtractFail
  155. */
  156. public function onUserExtractFail($event)
  157. {
  158. list($extract, $fail_msg) = $event;
  159. $icon = Config::get('app.header_cs_2', '');
  160. UserNotice::sendNoticeTo($extract['uid'], '您的提现申请已处理',
  161. '您申请提现 ' . $extract['extract_price']
  162. . ' 元未能成功执行,原因:' . $fail_msg['message'] . '。请联系客服处理。', $icon);
  163. }
  164. /**
  165. * 首次挖礦得到弊 UserFirstDig
  166. */
  167. public function onUserFirstDig($event)
  168. {
  169. }
  170. /**
  171. * 用户提币 UserWithdrawalCoin
  172. */
  173. public function onUserWithdrawalCoin($event)
  174. {
  175. }
  176. /**
  177. * 用户参加活动 UserActivity
  178. */
  179. public function onUserActivity($event)
  180. {
  181. }
  182. }