UserBehavior.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace behavior\wechat;
  12. use app\wap\model\user\User;
  13. use app\wap\model\user\WechatUser;
  14. use think\Cookie;
  15. use think\Request;
  16. class UserBehavior
  17. {
  18. /**
  19. * 微信授权成功后
  20. * @param $userInfo
  21. */
  22. public static function wechatOauthAfter($openid,$wechatInfo)
  23. {
  24. Cookie::set('is_login',1);
  25. $spread_uid=$wechatInfo['spread_uid'];
  26. $wechatInfo['nickname']=filter_emoji($wechatInfo['nickname']);
  27. if(isset($wechatInfo['unionid']) && $wechatInfo['unionid'] != '' && WechatUser::be(['unionid'=>$wechatInfo['unionid']])){
  28. WechatUser::edit($wechatInfo,$wechatInfo['unionid'],'unionid');
  29. $uid = WechatUser::where('unionid',$wechatInfo['unionid'])->value('uid');
  30. User::updateWechatUser($wechatInfo,$uid);
  31. }else if(WechatUser::be(['openid'=>$wechatInfo['openid']])){
  32. WechatUser::edit($wechatInfo,$wechatInfo['openid'],'openid');
  33. User::updateWechatUser($wechatInfo,WechatUser::openidToUid($wechatInfo['openid']));
  34. }else{
  35. unset($wechatInfo['spread_uid']);
  36. if(isset($wechatInfo['subscribe_scene'])) unset($wechatInfo['subscribe_scene']);
  37. if(isset($wechatInfo['qr_scene'])) unset($wechatInfo['qr_scene']);
  38. if(isset($wechatInfo['qr_scene_str'])) unset($wechatInfo['qr_scene_str']);
  39. try{
  40. $userInfo=User::setWechatUser($wechatInfo,$spread_uid);
  41. WechatUser::setNewUserInfo($userInfo);
  42. }catch (\Exception $e){
  43. }
  44. }
  45. User::where('uid',WechatUser::openidToUid($openid))->limit(1)->update(['last_time'=>time(),'last_ip'=>Request::instance()->ip()]);
  46. }
  47. }