UserSubscribe.php 7.7 KB

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