UserSubscribe.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, $site_id) = $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'])->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, $site_id);
  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']])->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['site_id'] = $site_id;
  70. $wechatInfo = WechatUser::create($wechatInfo);
  71. // if ($isLogin && $bind)
  72. // User::where('uid', $wechatInfo['uid'])
  73. // ->limit(1)->update(['openid' => $wechatInfo['openid']]);
  74. // else
  75. User::setWechatUser($wechatInfo, $spreadId, $site_id);
  76. }
  77. $uid = WechatUser::openidToUid($openid, 'openid');
  78. // 设置推广关系
  79. User::setSpread($spreadId, $uid);
  80. User::where('uid', $uid)
  81. ->limit(1)->update(['last_time' => time(), 'last_ip' => app('request')->ip()]);
  82. }
  83. /**
  84. * 用户访问记录
  85. * @param $event
  86. */
  87. public function onInitLogin($event)
  88. {
  89. list($userInfo) = $event;
  90. $request = app('request');
  91. User::edit(['last_time' => time(), 'last_ip' => $request->ip()], $userInfo->uid, 'uid');
  92. }
  93. /**
  94. * 检查是否能成为会员
  95. * @param $event
  96. */
  97. public function onUserLevelAfter($event)
  98. {
  99. list($userUid) = $event;
  100. UserLevel::setLevelComplete($userUid);
  101. }
  102. }