UserBehavior.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/25
  6. */
  7. namespace behavior\wechat;
  8. use app\wap\model\user\User;
  9. use app\wap\model\user\WechatUser;
  10. use think\Cookie;
  11. use think\Request;
  12. class UserBehavior
  13. {
  14. /**
  15. * 微信授权成功后
  16. * @param $userInfo
  17. */
  18. public static function wechatOauthAfter($openid,$wechatInfo)
  19. {
  20. Cookie::set('is_login',1);
  21. if($wechatInfo['unionid'] != '' && WechatUser::be(['unionid'=>$wechatInfo['unionid']])){
  22. WechatUser::edit($wechatInfo,$wechatInfo['unionid'],'unionid');
  23. $uid = WechatUser::where('unionid',$wechatInfo['unionid'])->value('uid');
  24. if(!User::be(['uid'=>$uid])){
  25. $wechatInfo = WechatUser::where('uid',$uid)->find();
  26. User::setWechatUser($wechatInfo);
  27. }else{
  28. User::updateWechatUser($wechatInfo,$uid);
  29. }
  30. }else if(WechatUser::be(['openid'=>$wechatInfo['openid']])){
  31. WechatUser::edit($wechatInfo,$wechatInfo['openid'],'openid');
  32. User::updateWechatUser($wechatInfo,WechatUser::openidToUid($wechatInfo['openid']));
  33. }else{
  34. $wechatInfo = WechatUser::set($wechatInfo);
  35. User::setWechatUser($wechatInfo);
  36. }
  37. User::where('uid',WechatUser::openidToUid($openid))
  38. ->limit(1)->update(['last_time'=>time(),'last_ip'=>Request::instance()->ip()]);
  39. }
  40. }