From 1d8367885a649d4f2fe8de2d8cc627b817ad401a Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 30 Dec 2019 19:40:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BE=AE=E4=BF=A1=E6=8E=88?= =?UTF-8?q?=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/wechat/controller/api/Js.php | 40 ++++++++++++++++++++++---------- vendor/services.php | 2 +- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/wechat/controller/api/Js.php b/app/wechat/controller/api/Js.php index 3cdeb590e..8eeef59d1 100644 --- a/app/wechat/controller/api/Js.php +++ b/app/wechat/controller/api/Js.php @@ -38,25 +38,41 @@ class Js extends Controller */ public function index() { - $url = $this->request->server('http_referer', $this->request->url(true)); - $user = WechatService::instance()->getWebOauthInfo($url, $this->request->get('mode', 1), false); - $openid = isset($user['openid']) ? $user['openid'] : ''; - $unionid = empty($user['fansinfo']['unionid']) ? '' : $user['fansinfo']['unionid']; - $configJson = json_encode(WechatService::instance()->getWebJssdkSign($url), JSON_UNESCAPED_UNICODE); - $fansinfoJson = json_encode(isset($user['fansinfo']) ? $user['fansinfo'] : [], JSON_UNESCAPED_UNICODE); - $html = <<mode = $this->request->get('mode', 1); + $this->source = $this->request->server('http_referer', $this->request->url(true)); + $user = WechatService::instance()->getWebOauthInfo($this->source, $this->mode, false); + if (empty($user['openid'])) { + $content = 'alert("Wechat webOauth failed.")'; + } else { + $this->openid = $user['openid']; + $this->config = json_encode(WechatService::instance()->getWebJssdkSign($this->source)); + $this->fansinfo = json_encode(empty($user['fansinfo']) ? [] : $user['fansinfo'], JSON_UNESCAPED_UNICODE); + // 生成接口授权令牌 + $this->token = uniqid('oauth') . rand(10000, 99999); + $this->app->cache->set($this->openid, $this->token, 3600); + $content = $this->_buildContent(); + } + return Response::create($content)->contentType('application/x-javascript'); + } + + /** + * 生成授权内容 + * @return string + */ + private function _buildContent() + { + return <<fansinfo}; + wx.config({$this->config}); wx.ready(function(){ wx.hideOptionMenu(); wx.hideAllNonBaseMenuItem(); }); } EOF; - return Response::create($html)->contentType('application/x-javascript'); } } diff --git a/vendor/services.php b/vendor/services.php index 4ee8f752a..f630f3c14 100644 --- a/vendor/services.php +++ b/vendor/services.php @@ -1,5 +1,5 @@ 'think\\app\\Service',