UserSubscribe.php 7.0 KB

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