WapBasic.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/11
  6. */
  7. namespace app\wap\controller;
  8. use app\wap\model\user\User;
  9. use app\wap\model\user\WechatUser;
  10. use behavior\wap\WapBehavior;
  11. use service\JsonService;
  12. use think\Controller;
  13. use behavior\wechat\UserBehavior;
  14. use service\HookService;
  15. use service\UtilService;
  16. use service\WechatService;
  17. use think\Cookie;
  18. use think\Request;
  19. use think\Session;
  20. use think\Url;
  21. class WapBasic extends Controller
  22. {
  23. protected function _initialize()
  24. {
  25. parent::_initialize(); // TODO: Change the autogenerated stub
  26. $spreadUid = Request::instance()->route('spuid',0);
  27. if($spreadUid
  28. && ($userInfo = User::getUserInfo(WechatUser::openidToUid($this->oauth())))
  29. && !$userInfo['spread_uid']
  30. && $userInfo['uid'] != $spreadUid
  31. ) User::edit(['spread_uid'=>$spreadUid],$userInfo['uid'],'uid');
  32. HookService::listen('wap_init',null,null,false,WapBehavior::class);
  33. }
  34. /**
  35. * 操作失败 弹窗提示
  36. * @param string $msg
  37. * @param int $url
  38. * @param string $title
  39. */
  40. protected function failed($msg = '操作失败', $url = 0, $title='信息提示')
  41. {
  42. if($this->request->isAjax()){
  43. exit(JsonService::fail($msg,$url)->getContent());
  44. }else {
  45. $this->assign(compact('title', 'msg', 'url'));
  46. exit($this->fetch('public/error'));
  47. }
  48. }
  49. /**
  50. * 操作成功 弹窗提示
  51. * @param $msg
  52. * @param int $url
  53. */
  54. protected function successful($msg = '操作成功', $url = 0, $title='成功提醒')
  55. {
  56. if($this->request->isAjax()){
  57. exit(JsonService::successful($msg,$url)->getContent());
  58. }else {
  59. $this->assign(compact('title', 'msg', 'url'));
  60. exit($this->fetch('public/success'));
  61. }
  62. }
  63. public function _empty($name)
  64. {
  65. $url = strtolower($name) == 'index' ? Url::build('Index/index','',true,true) : 0;
  66. return $this->failed('请求页面不存在!',$url);
  67. }
  68. /**
  69. * 微信用户自动登陆
  70. * @return string $openid
  71. */
  72. protected function oauth()
  73. {
  74. $openid = Session::get('loginOpenid','wap');
  75. if($openid) return $openid;
  76. if(!UtilService::isWechatBrowser()) exit($this->failed('请在微信客户端打开链接'));
  77. if($this->request->isAjax()) exit($this->failed('请登陆!'));
  78. $errorNum = (int)Cookie::get('_oen');
  79. if($errorNum && $errorNum > 3) exit($this->failed('微信用户信息获取失败!!'));
  80. try{
  81. $wechatInfo = WechatService::oauthService()->user()->getOriginal();
  82. }catch (\Exception $e){
  83. Cookie::set('_oen',++$errorNum,900);
  84. exit(WechatService::oauthService()->scopes(['snsapi_base'])
  85. ->redirect($this->request->url(true))->send());
  86. }
  87. if(!isset($wechatInfo['nickname'])){
  88. $wechatInfo = WechatService::getUserInfo($wechatInfo['openid']);
  89. if(!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  90. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  91. ->redirect($this->request->url(true))->send());
  92. if(isset($wechatInfo['tagid_list']))
  93. $wechatInfo['tagid_list'] = implode(',',$wechatInfo['tagid_list']);
  94. }else{
  95. if(isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  96. $wechatInfo['subscribe'] = 0;
  97. }
  98. Cookie::delete('_oen');
  99. $openid = $wechatInfo['openid'];
  100. HookService::afterListen('wechat_oauth',$openid,$wechatInfo,false,UserBehavior::class);
  101. Session::set('loginOpenid',$openid,'wap');
  102. Cookie::set('is_login',1);
  103. return $openid;
  104. }
  105. }