UserBehavior.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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(isset($wechatInfo['unionid']) && $wechatInfo['unionid'] != '' && WechatUser::be(['unionid'=>$wechatInfo['unionid']])){
  22. WechatUser::edit($wechatInfo,$wechatInfo['unionid'],'unionid');
  23. $uid = WechatUser::where('unionid',$wechatInfo['unionid'])->value('uid');
  24. User::updateWechatUser($wechatInfo,$uid);
  25. }else if(WechatUser::be(['openid'=>$wechatInfo['openid']])){
  26. WechatUser::edit($wechatInfo,$wechatInfo['openid'],'openid');
  27. User::updateWechatUser($wechatInfo,WechatUser::openidToUid($wechatInfo['openid']));
  28. }else{
  29. $wechatInfo = WechatUser::set($wechatInfo);
  30. User::setWechatUser($wechatInfo);
  31. }
  32. User::where('uid',WechatUser::openidToUid($openid))
  33. ->limit(1)->update(['last_time'=>time(),'last_ip'=>Request::instance()->ip()]);
  34. }
  35. }