[更新]修改公众号页面支持

This commit is contained in:
Anyon 2018-04-03 13:55:15 +08:00
parent 3e54dbd3ed
commit be666aac79
4 changed files with 45 additions and 35 deletions

View File

@ -24,21 +24,26 @@ use think\facade\Response;
*/ */
class Js class Js
{ {
/** /**
* jsSign签名 * jsSign签名
* @throws \Exception * @return mixed
* @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\Exception
* @throws \think\exception\PDOException
*/ */
public function index() public function index()
{ {
$result = app('request'); $result = app('request');
$wechat = WechatService::webOauth($result->get('mode', 1));
$url = $result->server('HTTP_REFERER', $result->url(true), null); $url = $result->server('HTTP_REFERER', $result->url(true), null);
$wechat = WechatService::webOauth($url, $result->get('mode', 1), false);
$assign = [ $assign = [
'jssdk' => WechatService::webJsSDK($url), 'jssdk' => WechatService::webJsSDK($url),
'openid' => $wechat['openid'], 'fansinfo' => $wechat['fansinfo'], 'openid' => $wechat['openid'], 'fansinfo' => $wechat['fansinfo'],
]; ];
return Response::create(env('APP_PATH') . 'wechat/view/api/script/index.js', 'view', 200, [ 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', 'Cache-Control' => 'no-cache', 'Pragma' => 'no-cache', 'Expires' => '0',
])->assign($assign); ])->assign($assign);
} }

View File

@ -33,7 +33,7 @@ class Tools extends BasicAdmin
*/ */
public function oauth() public function oauth()
{ {
$fans = WechatService::webOauth(1); $fans = WechatService::webOauth($this->request->url(true), 1);
return $this->fetch('', ['fans' => $fans]); return $this->fetch('', ['fans' => $fans]);
} }

View File

@ -1,8 +1,8 @@
if (typeof wx === 'object') { if (typeof wx === 'object') {
wx.openid = '{$fansinfo.openid|default=""}'; wx.openid = '{$fansinfo.openid|default=""}';
wx.unionid = '{$fansinfo.unionid|default=""}'; wx.unionid = '{$fansinfo.unionid|default=""}';
wx.fansinfo = eval('{$fansinfo|default=[]|json_encode=###,256|raw}'); wx.fansinfo = eval('({$fansinfo|default=[]|json_encode=###,256|raw})');
wx.config(eval('{$jssdk|default=[]|json_encode=###,256|raw}')); wx.config(eval('({$jssdk|default=[]|json_encode=###,256|raw})'));
wx.ready(function () { wx.ready(function () {
wx.hideOptionMenu(); wx.hideOptionMenu();
wx.hideAllNonBaseMenuItem(); wx.hideAllNonBaseMenuItem();

View File

@ -41,8 +41,8 @@ use think\Exception;
* @method \WeChat\Template template() static 模板消息 * @method \WeChat\Template template() static 模板消息
* @method \WeChat\User user() static 微信粉丝管理 * @method \WeChat\User user() static 微信粉丝管理
* @method \WeChat\Wifi wifi() static 门店WIFI管理 * @method \WeChat\Wifi wifi() static 门店WIFI管理
* @method object wechat() static 第三方微信工具 * @method void wechat() static 第三方微信工具
* @method object config() static 第三方配置工具 * @method void config() static 第三方配置工具
*/ */
class WechatService class WechatService
{ {
@ -86,9 +86,9 @@ class WechatService
/** /**
* 获取微信网页JSSDK * 获取微信网页JSSDK
* @param null|string $url 签名SDK * @param null|string $url JS签名地址
* @return array * @return array
* @throws \think\Exception * @throws Exception
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException * @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\exception\PDOException * @throws \think\exception\PDOException
@ -97,60 +97,65 @@ class WechatService
{ {
switch (strtolower(sysconf('wechat_type'))) { switch (strtolower(sysconf('wechat_type'))) {
case 'api': 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': case 'thr':
default: 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 int $fullMode 授权公众号模式
* @param bool $isRedirect 是否进行跳转
* @return array * @return array
* @throws \think\Exception * @throws \think\Exception
* @throws \think\exception\PDOException * @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")]; list($openid, $fansinfo) = [session("{$appid}_openid"), session("{$appid}_fansinfo")];
if ((empty($fullMode) && !empty($openid)) || (!empty($fullMode) && !empty($fansinfo))) { if ((empty($fullMode) && !empty($openid)) || (!empty($fullMode) && !empty($fansinfo))) {
empty($fansinfo) || FansService::set($fansinfo);
return ['openid' => $openid, 'fansinfo' => $fansinfo]; return ['openid' => $openid, 'fansinfo' => $fansinfo];
} }
switch (strtolower(sysconf('wechat_type'))) { switch (strtolower(sysconf('wechat_type'))) {
case 'api': case 'api':
$wechat = self::oauth(); $wechat = self::oauth();
if ($request->get('state') !== $appid) { if (request()->get('state') !== $appid) {
$selfUrl = $request->url(true); $snsapi = empty($fullMode) ? 'snsapi_base' : 'snsapi_userinfo';
$typeSns = empty($fullMode) ? 'snsapi_base' : 'snsapi_userinfo'; $param = (strpos($url, '?') !== false ? '&' : '?') . 'rcode=' . encode($url);
$params = (strpos($selfUrl, '?') === false ? '?' : '&') . 'rcode=' . encode($selfUrl); $OauthUrl = $wechat->getOauthRedirect($url . $param, $appid, $snsapi);
redirect($wechat->getOauthRedirect($selfUrl . $params, $appid, $typeSns), [], 301)->send(); $isRedirect && redirect($OauthUrl, [], 301)->send();
exit("window.location.href='{$OauthUrl}'");
} }
$token = $wechat->getOauthAccessToken(); $token = $wechat->getOauthAccessToken();
session("{$appid}_openid", empty($token['openid']) ? null : $token['openid']); if (isset($token['openid'])) {
if (!empty($fullMode) && !empty($token['openid']) && !empty($token['access_token'])) { session("{$appid}_openid", $openid = $token['openid']);
$fansinfo = $wechat->getUserInfo($token['access_token'], $token['openid']); if (empty($fullMode) && request()->get('rcode')) {
session("{$appid}_fansinfo", empty($fansinfo) ? null : $fansinfo); redirect(decode(request()->get('rcode')))->send();
}
session("{$appid}_fansinfo", $fansinfo = $wechat->getUserInfo($token['access_token'], $openid));
empty($fansinfo) || FansService::set($fansinfo); empty($fansinfo) || FansService::set($fansinfo);
} }
if (($rcode = $request->get('rcode', false))) { redirect(decode(request()->get('rcode')))->send();
redirect(decode($rcode), [], 301)->send(); break;
} else {
throw new Exception('网页授权异常,请稍候再试!', '503');
}
case 'thr': case 'thr':
default: default:
$result = self::wechat()->oauth(session_id(), $request->url(true), $fullMode); $service = self::wechat();
session("{$appid}_openid", empty($result['openid']) ? null : $result['openid']); $result = $service->oauth(session_id(), $url, $fullMode);
session("{$appid}_fansinfo", empty($result['fans']) ? null : $result['fans']); session("{$appid}_openid", $openid = $result['openid']);
if ((empty($fullMode) && !empty($result['openid'])) || (!empty($fullMode) && !empty($result['fans']))) { session("{$appid}_fansinfo", $fansinfo = $result['fans']);
empty($result['fans']) || FansService::set($result['fans']); if ((empty($fullMode) && !empty($openid)) || (!empty($fullMode) && !empty($fansinfo))) {
return ['openid' => $result['openid'], 'fansinfo' => $result['fans']]; empty($fansinfo) || FansService::set($fansinfo);
return ['openid' => $openid, 'fansinfo' => $fansinfo];
} }
if (!empty($result['url'])) { // 授权跳转 if ($isRedirect && !empty($result['url'])) {
redirect($result['url'], [], 301)->send(); redirect($result['url'], [], 301)->send();
} }
exit("window.location.href='{$result['url']}'");
} }
} }