UserSubscribe.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. use think\facade\Db;
  9. /**
  10. * 用户事件
  11. * Class UserSubscribe
  12. * @package crmeb\subscribes
  13. */
  14. class UserSubscribe
  15. {
  16. public function handle()
  17. {
  18. }
  19. /**
  20. * 管理员后台给用户添加金额
  21. * @param $event
  22. */
  23. public function onAdminAddMoney($event)
  24. {
  25. list($user, $money) = $event;
  26. //$user 用户信息
  27. //$money 添加的金额
  28. }
  29. /**
  30. * 微信授权成功后
  31. * @param $event
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. */
  36. public function onWechatOauthAfter($event)
  37. {
  38. list($openid, $wechatInfo, $spreadId, $login_type) = $event;
  39. if (!User::be(['uid' => $spreadId])) $spreadId = 0;
  40. if(sys_config('spread_look')==1) $spreadId = 0;
  41. $wechatInfo['nickname'] = filter_emoji($wechatInfo['nickname']);
  42. Cookie::set('is_login', 1);
  43. @file_put_contents("spread_look.txt",sys_config('spread_look'));
  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. if(sys_config('spread_look')==0) {
  53. if (!User::where('uid', $uid)->value('spread_uid') && $spreadId && $uid > $spreadId) { //绑定关系
  54. Db::name('user')->where('uid', $spreadId)->inc('spread_count', 1)->update();
  55. User::where('uid', $uid)->update([
  56. 'spread_uid' => $spreadId,
  57. 'spread_time' => time(),
  58. ]);
  59. }
  60. }
  61. }
  62. } else if ($uid = WechatUser::where(['openid' => $wechatInfo['openid']])->where('user_type', '<>', 'h5')->value('uid')) {
  63. WechatUser::edit($wechatInfo, $uid, 'uid');
  64. if ($login_type) $wechatInfo['login_type'] = $login_type;
  65. User::updateWechatUser($wechatInfo, $uid);
  66. if(sys_config('spread_look')==0) {
  67. if (!User::where('uid', $uid)->value('spread_uid') && $spreadId && $uid > $spreadId) { //绑定关系
  68. Db::name('user')->where('uid', $spreadId)->inc('spread_count', 1)->update();
  69. User::where('uid', $uid)->update([
  70. 'spread_uid' => $spreadId,
  71. 'spread_time' => time(),
  72. ]);
  73. }
  74. }
  75. } else {
  76. if (isset($wechatInfo['subscribe_scene'])) unset($wechatInfo['subscribe_scene']);
  77. if (isset($wechatInfo['qr_scene'])) unset($wechatInfo['qr_scene']);
  78. if (isset($wechatInfo['qr_scene_str'])) unset($wechatInfo['qr_scene_str']);
  79. // $isLogin = request()->isLogin();
  80. // $bind = false;
  81. // if($isLogin){
  82. // $loginUid = request()->user();
  83. // $isUser = $loginUid ? request()->tokenData()->type === 'user' : false;
  84. // $bind = $loginUid && $isUser && !$loginUid->openid && !User::be(['openid' => $wechatInfo['openid']]);
  85. // //微信用户绑定 h5用户
  86. // if ($bind) {
  87. // $wechatInfo['uid'] = $loginUid->uid;
  88. // };
  89. // }
  90. $wechatInfo = WechatUser::create($wechatInfo);
  91. // if ($isLogin && $bind)
  92. // User::where('uid', $wechatInfo['uid'])
  93. // ->limit(1)->update(['openid' => $wechatInfo['openid']]);
  94. // else
  95. User::setWechatUser($wechatInfo, $spreadId);
  96. }
  97. $uid = WechatUser::openidToUid($openid, 'openid');
  98. // 设置推广关系
  99. //User::setSpread($spreadId, $uid);
  100. if(intval(User::where('uid', $uid)->value('level'))==0)
  101. {
  102. UserLevel::setUserLevel($uid,1);
  103. }
  104. User::where('uid', $uid)
  105. ->limit(1)->update(['last_time' => time(), 'last_ip' => app('request')->ip()]);
  106. }
  107. /**
  108. * 用户访问记录
  109. * @param $event
  110. */
  111. public function onInitLogin($event)
  112. {
  113. list($userInfo) = $event;
  114. $request = app('request');
  115. User::edit(['last_time' => time(), 'last_ip' => $request->ip()], $userInfo->uid, 'uid');
  116. }
  117. /**
  118. * 检查是否能成为会员
  119. * @param $event
  120. */
  121. public function onUserLevelAfter($event)
  122. {
  123. list($userUid) = $event;
  124. UserLevel::setLevelComplete($userUid);
  125. }
  126. }