UserSubscribe.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\user\UserBill;
  7. use app\models\user\UserLevel;
  8. use app\models\user\UserNotice;
  9. use tw\async\tasks\AsyncClass;
  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. warnlog('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. // 对现存用户处理,用于过渡
  94. AsyncClass::push('tw\async\tasks\UserTaskClass', [], 'generate_user_poster', [$userInfo->uid]);
  95. debuglog('EVENT user ' . $userInfo->uid . ' login.');
  96. }
  97. /**
  98. * 检查是否能成为会员 UserLevelAfter
  99. * @param $event
  100. */
  101. public function onUserLevelAfter($event)
  102. {
  103. list($userUid) = $event;
  104. try {
  105. if(UserLevel::setLevelComplete($userUid)) {
  106. event('UserLevelUp', $event);
  107. }
  108. } catch (\Exception $e) {
  109. }
  110. }
  111. /**
  112. * 用户成为推广员 UserBecomedPromoter
  113. */
  114. public function onUserBecomedPromoter($event)
  115. {
  116. list($user) = $event;
  117. AsyncClass::push('tw\async\tasks\UserTaskClass', [], 'generate_user_poster', [$user['uid']]);
  118. debuglog('EVENT user' . $user['uid'] . ' has becomed promoter');
  119. }
  120. /**
  121. * 用户升级成功 UserLevelUp
  122. */
  123. public function onUserLevelUp($event)
  124. {
  125. list($userUid) = $event;
  126. $userinfo = User::get($userUid);
  127. debuglog("EVENT user " . $userUid . ' update to level ' . $userinfo['level']);
  128. }
  129. /**
  130. * 新用戶注冊 UserRegistered
  131. */
  132. public function onUserRegistered($event)
  133. {
  134. list('user' => $user) = $event;
  135. $res = UserBill::income('新用户送钱活动', $user['uid'], 'now_money', 'activity_1_gift', $user['now_money'], $user['uid'], $user['now_money'], '新用户送' . $user['now_money'] . '元');
  136. if (!$res) {
  137. errlog('user ' . $user['uid'] . 'registered send money ' . $user['now_money'] . ' failed.');
  138. }
  139. debuglog('EVENT new user registered:' . $user['uid']);
  140. }
  141. /**
  142. * 首次下訂單 UserFirstOrder
  143. */
  144. public function onUserFirstOrder($event)
  145. {
  146. list('order' => $order) = $event;
  147. debuglog('EVENT user ' . $order['uid'] . ' first order:' . $order['id']);
  148. }
  149. /**
  150. * 用戶申请提現 UserRequestWithdrawal
  151. *
  152. * 插入异步任务,自动审核。效果等同于后台手动批准。
  153. * 如果不满足自动提现条件,则不处理,发出机器人通知,交给人工审核
  154. */
  155. public function onUserRequestWithdrawal($event)
  156. {
  157. list('user' => $user, 'info' => $info) = $event;
  158. debuglog('EVENT user ' . $user['uid'] . ' withdraw ' . mapped_implode(',', $info));
  159. }
  160. /**
  161. * 後臺批準提現 UserExtractSucc
  162. */
  163. public function onUserExtractSucc($event)
  164. {
  165. list($extract) = $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. . '已转账,请耐心等待您的收款账户系统确认后核对金额。衷心感谢您对美天旺的信任和支持。如有问题,请联系客服。', $icon);
  172. }
  173. /**
  174. * 後臺拒絕提現 UserExtractRejected
  175. */
  176. public function onUserExtractRejected($event)
  177. {
  178. list($extract, $fail_msg) = $event;
  179. $icon = Config::get('app.header_cs_2', '');
  180. list($extract_type, $account) = get_extract_name($extract);
  181. UserNotice::sendNoticeTo($extract['uid'], '您的提现申请已处理',
  182. '您申请提现 ' . $extract['extract_price']
  183. . ' 元到' . $extract_type . '帐号' . $account
  184. . '未能成功执行,原因:' . $fail_msg . '。请联系客服。', $icon);
  185. }
  186. /**
  187. * 首次挖礦得到弊 UserFirstDig
  188. */
  189. public function onUserFirstDig($event)
  190. {
  191. }
  192. /**
  193. * 用户提币 UserWithdrawalCoin
  194. */
  195. public function onUserWithdrawalCoin($event)
  196. {
  197. }
  198. /**
  199. * 用户参加活动 UserActivity
  200. */
  201. public function onUserActivity($event)
  202. {
  203. }
  204. }