From be666aac7988203acb2c47bf546494746d4b98b3 Mon Sep 17 00:00:00 2001 From: Anyon Date: Tue, 3 Apr 2018 13:55:15 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E4=BF=AE=E6=94=B9=E5=85=AC?= =?UTF-8?q?=E4=BC=97=E5=8F=B7=E9=A1=B5=E9=9D=A2=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/wechat/controller/api/Js.php | 11 +++- application/wechat/controller/api/Tools.php | 2 +- application/wechat/view/api/script/index.js | 4 +- extend/service/WechatService.php | 63 +++++++++++---------- 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/application/wechat/controller/api/Js.php b/application/wechat/controller/api/Js.php index dbf3e567f..033fb4bc9 100644 --- a/application/wechat/controller/api/Js.php +++ b/application/wechat/controller/api/Js.php @@ -24,21 +24,26 @@ use think\facade\Response; */ class Js { + /** * jsSign签名 - * @throws \Exception + * @return mixed + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + * @throws \think\Exception + * @throws \think\exception\PDOException */ public function index() { $result = app('request'); - $wechat = WechatService::webOauth($result->get('mode', 1)); $url = $result->server('HTTP_REFERER', $result->url(true), null); + $wechat = WechatService::webOauth($url, $result->get('mode', 1), false); $assign = [ 'jssdk' => WechatService::webJsSDK($url), 'openid' => $wechat['openid'], 'fansinfo' => $wechat['fansinfo'], ]; return Response::create(env('APP_PATH') . 'wechat/view/api/script/index.js', 'view', 200, [ - 'Content-Type' => 'application/x-javascript;charset=utf-8', + 'Content-Type' => 'application/x-javascript', 'Cache-Control' => 'no-cache', 'Pragma' => 'no-cache', 'Expires' => '0', ])->assign($assign); } diff --git a/application/wechat/controller/api/Tools.php b/application/wechat/controller/api/Tools.php index 778f3bdec..aeef924d1 100644 --- a/application/wechat/controller/api/Tools.php +++ b/application/wechat/controller/api/Tools.php @@ -33,7 +33,7 @@ class Tools extends BasicAdmin */ public function oauth() { - $fans = WechatService::webOauth(1); + $fans = WechatService::webOauth($this->request->url(true), 1); return $this->fetch('', ['fans' => $fans]); } diff --git a/application/wechat/view/api/script/index.js b/application/wechat/view/api/script/index.js index d9c003067..5fb7b054f 100644 --- a/application/wechat/view/api/script/index.js +++ b/application/wechat/view/api/script/index.js @@ -1,8 +1,8 @@ if (typeof wx === 'object') { wx.openid = '{$fansinfo.openid|default=""}'; wx.unionid = '{$fansinfo.unionid|default=""}'; - wx.fansinfo = eval('{$fansinfo|default=[]|json_encode=###,256|raw}'); - wx.config(eval('{$jssdk|default=[]|json_encode=###,256|raw}')); + wx.fansinfo = eval('({$fansinfo|default=[]|json_encode=###,256|raw})'); + wx.config(eval('({$jssdk|default=[]|json_encode=###,256|raw})')); wx.ready(function () { wx.hideOptionMenu(); wx.hideAllNonBaseMenuItem(); diff --git a/extend/service/WechatService.php b/extend/service/WechatService.php index 2f16dde0e..cd8f28c38 100644 --- a/extend/service/WechatService.php +++ b/extend/service/WechatService.php @@ -41,8 +41,8 @@ use think\Exception; * @method \WeChat\Template template() static 模板消息 * @method \WeChat\User user() static 微信粉丝管理 * @method \WeChat\Wifi wifi() static 门店WIFI管理 - * @method object wechat() static 第三方微信工具 - * @method object config() static 第三方配置工具 + * @method void wechat() static 第三方微信工具 + * @method void config() static 第三方配置工具 */ class WechatService { @@ -86,9 +86,9 @@ class WechatService /** * 获取微信网页JSSDK - * @param null|string $url 签名SDK + * @param null|string $url JS签名地址 * @return array - * @throws \think\Exception + * @throws Exception * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException * @throws \think\exception\PDOException @@ -97,60 +97,65 @@ class WechatService { switch (strtolower(sysconf('wechat_type'))) { case 'api': - return WechatService::script()->getJsSign(is_null($url) ? request()->url(true) : $url); + return WechatService::script()->getJsSign(is_null($url) ? app('request')->url(true) : $url); case 'thr': default: - return WechatService::wechat()->jsSign(is_null($url) ? request()->url(true) : $url); + return WechatService::wechat()->jsSign(is_null($url) ? app('request')->url(true) : $url); } } /** * 初始化进入授权 + * @param string $url 授权页面URL地址 * @param int $fullMode 授权公众号模式 + * @param bool $isRedirect 是否进行跳转 * @return array * @throws \think\Exception * @throws \think\exception\PDOException */ - public static function webOauth($fullMode = 0) + public static function webOauth($url, $fullMode = 0, $isRedirect = true) { - list($appid, $request) = [self::getAppid(), app('request')]; + $appid = self::getAppid(); list($openid, $fansinfo) = [session("{$appid}_openid"), session("{$appid}_fansinfo")]; if ((empty($fullMode) && !empty($openid)) || (!empty($fullMode) && !empty($fansinfo))) { + empty($fansinfo) || FansService::set($fansinfo); return ['openid' => $openid, 'fansinfo' => $fansinfo]; } switch (strtolower(sysconf('wechat_type'))) { case 'api': $wechat = self::oauth(); - if ($request->get('state') !== $appid) { - $selfUrl = $request->url(true); - $typeSns = empty($fullMode) ? 'snsapi_base' : 'snsapi_userinfo'; - $params = (strpos($selfUrl, '?') === false ? '?' : '&') . 'rcode=' . encode($selfUrl); - redirect($wechat->getOauthRedirect($selfUrl . $params, $appid, $typeSns), [], 301)->send(); + if (request()->get('state') !== $appid) { + $snsapi = empty($fullMode) ? 'snsapi_base' : 'snsapi_userinfo'; + $param = (strpos($url, '?') !== false ? '&' : '?') . 'rcode=' . encode($url); + $OauthUrl = $wechat->getOauthRedirect($url . $param, $appid, $snsapi); + $isRedirect && redirect($OauthUrl, [], 301)->send(); + exit("window.location.href='{$OauthUrl}'"); } $token = $wechat->getOauthAccessToken(); - session("{$appid}_openid", empty($token['openid']) ? null : $token['openid']); - if (!empty($fullMode) && !empty($token['openid']) && !empty($token['access_token'])) { - $fansinfo = $wechat->getUserInfo($token['access_token'], $token['openid']); - session("{$appid}_fansinfo", empty($fansinfo) ? null : $fansinfo); + if (isset($token['openid'])) { + session("{$appid}_openid", $openid = $token['openid']); + if (empty($fullMode) && request()->get('rcode')) { + redirect(decode(request()->get('rcode')))->send(); + } + session("{$appid}_fansinfo", $fansinfo = $wechat->getUserInfo($token['access_token'], $openid)); empty($fansinfo) || FansService::set($fansinfo); } - if (($rcode = $request->get('rcode', false))) { - redirect(decode($rcode), [], 301)->send(); - } else { - throw new Exception('网页授权异常,请稍候再试!', '503'); - } + redirect(decode(request()->get('rcode')))->send(); + break; case 'thr': default: - $result = self::wechat()->oauth(session_id(), $request->url(true), $fullMode); - session("{$appid}_openid", empty($result['openid']) ? null : $result['openid']); - session("{$appid}_fansinfo", empty($result['fans']) ? null : $result['fans']); - if ((empty($fullMode) && !empty($result['openid'])) || (!empty($fullMode) && !empty($result['fans']))) { - empty($result['fans']) || FansService::set($result['fans']); - return ['openid' => $result['openid'], 'fansinfo' => $result['fans']]; + $service = self::wechat(); + $result = $service->oauth(session_id(), $url, $fullMode); + session("{$appid}_openid", $openid = $result['openid']); + session("{$appid}_fansinfo", $fansinfo = $result['fans']); + if ((empty($fullMode) && !empty($openid)) || (!empty($fullMode) && !empty($fansinfo))) { + empty($fansinfo) || FansService::set($fansinfo); + return ['openid' => $openid, 'fansinfo' => $fansinfo]; } - if (!empty($result['url'])) { // 授权跳转 + if ($isRedirect && !empty($result['url'])) { redirect($result['url'], [], 301)->send(); } + exit("window.location.href='{$result['url']}'"); } }