UserSubscribe.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace crmeb\subscribes;
  3. use app\models\user\User;
  4. use app\models\user\WechatUser;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. use think\facade\Cookie;
  9. use app\models\system\SystemAttachment;
  10. use app\models\user\UserLevel;
  11. /**
  12. * 用户事件
  13. * Class UserSubscribe
  14. * @package crmeb\subscribes
  15. */
  16. class UserSubscribe
  17. {
  18. public function handle()
  19. {
  20. }
  21. /**
  22. * 管理员后台给用户添加金额
  23. * @param $event
  24. */
  25. public function onAdminAddMoney($event)
  26. {
  27. list($user, $money) = $event;
  28. //$user 用户信息
  29. //$money 添加的金额
  30. }
  31. /**
  32. * 微信授权成功后
  33. * @param $event
  34. * @throws DataNotFoundException
  35. * @throws 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. * 用户访问记录
  87. * @param $event
  88. */
  89. public function onInitLogin($event)
  90. {
  91. list($userInfo) = $event;
  92. $request = app('request');
  93. User::edit(['last_time' => time(), 'last_ip' => $request->ip()], $userInfo->uid, 'uid');
  94. }
  95. /**
  96. * 检查是否能成为会员
  97. * @param $event
  98. * @throws DataNotFoundException
  99. * @throws DbException
  100. * @throws ModelNotFoundException
  101. */
  102. public function onUserLevelAfter($event)
  103. {
  104. list($user) = $event;
  105. UserLevel::setLevelComplete($user['uid'], false, $user['mer_id']);
  106. }
  107. }