User.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\admin\behavior;
  3. use app\common\model\UserRelation;
  4. use app\common\model\User as UserModel;
  5. class User
  6. {
  7. /**
  8. * 微信授权成功后
  9. * @param $event
  10. * @throws \think\db\exception\DataNotFoundException
  11. * @throws \think\db\exception\ModelNotFoundException
  12. * @throws \think\exception\DbException
  13. */
  14. public function WechatOauth($event)
  15. {
  16. list($openid, $wechatInfo, $spreadId, $login_type) = $event;
  17. if (!UserModel::where(['id' => $spreadId])->find()) $spreadId = 0;
  18. $wechatInfo['nickname'] = filter_emoji($wechatInfo['nickname']);
  19. if (isset($wechatInfo['unionid']) && $wechatInfo['unionid'] != '' && ($uid = UserRelation::where('unionid', $wechatInfo['unionid'])->where('login_type', '<>', 0)->value('user_id'))) {
  20. UserRelation::where('user_id',$uid)->update($wechatInfo);
  21. if (!UserRelation::where(['user_id' => $uid])->find()) {
  22. $wechatInfo = UserRelation::where('user_id', $uid)->find();
  23. UserModel::setWechatUser($wechatInfo, $spreadId);
  24. } else {
  25. if ($login_type) $wechatInfo['login_type'] = $login_type;
  26. UserModel::updateWechatUser($wechatInfo, $uid);
  27. }
  28. } else if ($uid = UserRelation::where(['openid' => $wechatInfo['openid']])->where('login_type', '<>', 0)->value('user_id')) {
  29. UserRelation::where('user_id',$uid)->update($wechatInfo);
  30. if ($login_type) $wechatInfo['login_type'] = $login_type;
  31. UserModel::updateWechatUser($wechatInfo, $uid);
  32. } else {
  33. $wechatInfo['login_type'] = $login_type;
  34. if (isset($wechatInfo['subscribe_scene'])) unset($wechatInfo['subscribe_scene']);
  35. if (isset($wechatInfo['qr_scene'])) unset($wechatInfo['qr_scene']);
  36. if (isset($wechatInfo['qr_scene_str'])) unset($wechatInfo['qr_scene_str']);
  37. UserModel::setWechatUser($wechatInfo, $spreadId);
  38. }
  39. $uid = UserRelation::openidToUid($openid, 'openid');
  40. // 设置推广关系
  41. UserModel::setSpread($spreadId, $uid);
  42. return true;
  43. }
  44. }