UserSubscribe.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  9. * 用户事件
  10. * Class UserSubscribe
  11. * @package crmeb\subscribes
  12. */
  13. class UserSubscribe
  14. {
  15. public function handle()
  16. {
  17. }
  18. /**
  19. * 管理员后台给用户添加金额
  20. * @param $event
  21. */
  22. public function onAdminAddMoney($event)
  23. {
  24. list($user, $money) = $event;
  25. //$user 用户信息
  26. //$money 添加的金额
  27. }
  28. /**
  29. * 微信授权成功后
  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. * 用户访问记录
  84. * @param $event
  85. */
  86. public function onInitLogin($event)
  87. {
  88. list($userInfo) = $event;
  89. $request = app('request');
  90. User::edit(['last_time' => time(), 'last_ip' => $request->ip()], $userInfo->uid, 'uid');
  91. }
  92. /**
  93. * 检查是否能成为会员
  94. * @param $event
  95. */
  96. public function onUserLevelAfter($event)
  97. {
  98. list($userUid) = $event;
  99. UserLevel::setLevelComplete($userUid);
  100. }
  101. }