From 150dae32b35566e752c0370a01b779749f72b255 Mon Sep 17 00:00:00 2001 From: sugar1569 Date: Thu, 14 Feb 2019 10:39:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/wap/config.php | 16 ++- application/wap/controller/WapBasic.php | 115 ++++++++++++++++++++ application/wap/controller/WapException.php | 41 +++++++ 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 application/wap/controller/WapBasic.php create mode 100644 application/wap/controller/WapException.php diff --git a/application/wap/config.php b/application/wap/config.php index a55e51e9..994103ea 100644 --- a/application/wap/config.php +++ b/application/wap/config.php @@ -10,6 +10,12 @@ // +---------------------------------------------------------------------- return [ + // 默认控制器名 + 'default_controller' => 'Index', + // 默认操作名 + 'default_action' => 'index', + // 自动搜索控制器 + 'controller_auto_search' => true, 'session' => [ // SESSION 前缀 'prefix' => 'wap', @@ -36,6 +42,14 @@ return [ // 标签库标签结束标记 'taglib_end' => '}', ], - 'exception_handle' =>\basic\WapException::class, + // 视图输出字符串内容替换 + 'view_replace_str' => [ + '{__PLUG_PATH}'=>PUBILC_PATH.'static/plug/', + '{__STATIC_PATH}'=>PUBILC_PATH.'static/', + '{__PUBLIC_PATH}'=>PUBILC_PATH, + '{__WAP_PATH}'=>PUBILC_PATH.'wap/first/' + ], + //wap异常处理 + 'exception_handle' => \app\wap\controller\WapException::class, 'empty_controller' =>'AuthController' ]; diff --git a/application/wap/controller/WapBasic.php b/application/wap/controller/WapBasic.php new file mode 100644 index 00000000..6365ef14 --- /dev/null +++ b/application/wap/controller/WapBasic.php @@ -0,0 +1,115 @@ + + * @day: 2017/12/11 + */ + +namespace app\wap\controller; + + +use app\wap\model\user\User; +use app\wap\model\user\WechatUser; +use behavior\wap\WapBehavior; +use service\JsonService; +use think\Controller; +use behavior\wechat\UserBehavior; +use service\HookService; +use service\UtilService; +use service\WechatService; +use think\Cookie; +use think\Request; +use think\Session; +use think\Url; + +class WapBasic extends Controller +{ + protected function _initialize() + { + + parent::_initialize(); // TODO: Change the autogenerated stub + $spreadUid = Request::instance()->route('spuid',0); + if($spreadUid + && ($userInfo = User::getUserInfo(WechatUser::openidToUid($this->oauth()))) + && !$userInfo['spread_uid'] + && $userInfo['uid'] != $spreadUid + ) User::edit(['spread_uid'=>$spreadUid],$userInfo['uid'],'uid'); + HookService::listen('wap_init',null,null,false,WapBehavior::class); + } + + /** + * 操作失败 弹窗提示 + * @param string $msg + * @param int $url + * @param string $title + */ + protected function failed($msg = '操作失败', $url = 0, $title='信息提示') + { + if($this->request->isAjax()){ + exit(JsonService::fail($msg,$url)->getContent()); + }else { + $this->assign(compact('title', 'msg', 'url')); + exit($this->fetch('public/error')); + } + } + + /** + * 操作成功 弹窗提示 + * @param $msg + * @param int $url + */ + protected function successful($msg = '操作成功', $url = 0, $title='成功提醒') + { + if($this->request->isAjax()){ + exit(JsonService::successful($msg,$url)->getContent()); + }else { + $this->assign(compact('title', 'msg', 'url')); + exit($this->fetch('public/success')); + } + } + + public function _empty($name) + { + $url = strtolower($name) == 'index' ? Url::build('Index/index','',true,true) : 0; + return $this->failed('请求页面不存在!',$url); + } + + /** + * 微信用户自动登陆 + * @return string $openid + */ + protected function oauth() + { + $openid = Session::get('loginOpenid','wap'); + if($openid) return $openid; + if(!UtilService::isWechatBrowser()) exit($this->failed('请在微信客户端打开链接')); + if($this->request->isAjax()) exit($this->failed('请登陆!')); + $errorNum = (int)Cookie::get('_oen'); + if($errorNum && $errorNum > 3) exit($this->failed('微信用户信息获取失败!!')); + try{ + $wechatInfo = WechatService::oauthService()->user()->getOriginal(); + }catch (\Exception $e){ + Cookie::set('_oen',++$errorNum,900); + exit(WechatService::oauthService()->scopes(['snsapi_base']) + ->redirect($this->request->url(true))->send()); + } + if(!isset($wechatInfo['nickname'])){ + $wechatInfo = WechatService::getUserInfo($wechatInfo['openid']); + if(!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname'])) + exit(WechatService::oauthService()->scopes(['snsapi_userinfo']) + ->redirect($this->request->url(true))->send()); + if(isset($wechatInfo['tagid_list'])) + $wechatInfo['tagid_list'] = implode(',',$wechatInfo['tagid_list']); + }else{ + if(isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']); + $wechatInfo['subscribe'] = 0; + } + Cookie::delete('_oen'); + $openid = $wechatInfo['openid']; + HookService::afterListen('wechat_oauth',$openid,$wechatInfo,false,UserBehavior::class); + Session::set('loginOpenid',$openid,'wap'); + Cookie::set('is_login',1); + return $openid; + } + +} \ No newline at end of file diff --git a/application/wap/controller/WapException.php b/application/wap/controller/WapException.php new file mode 100644 index 00000000..79ff4e12 --- /dev/null +++ b/application/wap/controller/WapException.php @@ -0,0 +1,41 @@ + + * @day: 2018/01/10 + */ + +namespace app\wap\controller; + + +use Exception; +use service\JsonService; +use think\exception\Handle; +use think\exception\HttpException; +use think\exception\ValidateException; +use think\Request; +use think\Url; + +class WapException extends Handle +{ + public function render(Exception $e){ + + //可以在此交由系统处理 + if(Request::instance()->get('_debug_info') == 'true') + return parent::render($e); + + // 参数验证错误 + if ($e instanceof ValidateException) { + return json($e->getError(), 422); + } + // 请求异常 + if ($e instanceof HttpException && request()->isAjax()) { + return JsonService::fail('系统错误'); + }else{ + $url = 0; + $title = '系统错误'; + $msg = addslashes($e->getMessage()); + exit(view('public/error',compact('title', 'msg', 'url'))->getContent()); + } + } +} \ No newline at end of file