فهرست منبع

公众号异常页面升级

sugar1569 7 سال پیش
والد
کامیت
150dae32b3
3فایلهای تغییر یافته به همراه171 افزوده شده و 1 حذف شده
  1. 15 1
      application/wap/config.php
  2. 115 0
      application/wap/controller/WapBasic.php
  3. 41 0
      application/wap/controller/WapException.php

+ 15 - 1
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'
 ];

+ 115 - 0
application/wap/controller/WapBasic.php

@@ -0,0 +1,115 @@
+<?php
+/**
+ *
+ * @author: xaboy<365615158@qq.com>
+ * @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;
+    }
+
+}

+ 41 - 0
application/wap/controller/WapException.php

@@ -0,0 +1,41 @@
+<?php
+/**
+ *
+ * @author: xaboy<365615158@qq.com>
+ * @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());
+        }
+    }
+}