[更新]增加公众号网页JSSDK支持

This commit is contained in:
Anyon 2018-04-03 13:01:52 +08:00
parent 3193ce668d
commit d35f045c73
6 changed files with 93 additions and 15 deletions

View File

@ -0,0 +1,63 @@
<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------
namespace app\wechat\controller\api;
use service\ToolsService;
use service\WechatService;
use think\facade\Response;
/**
* 微信公众号页面脚本
* Class Script
* @package app\wechat\controller\api
*/
class Script
{
/**
* 当前请求对象
* @var \think\Request
*/
protected $request;
/**
* Wechat constructor.
*/
public function __construct()
{
ToolsService::corsOptionsHandler();
$this->request = app('request');
}
/**
* jsSign签名
* @throws \Exception
*/
public function index()
{
$wechat = WechatService::webOauth($this->request->get('mode', 1));
$url = $this->request->server('HTTP_REFERER', $this->request->url(true), null);
$assign = [
'openid' => $wechat['openid'],
'fansinfo' => $wechat['fansinfo'],
'jssdk' => WechatService::webJsSDK($url),
];
return Response::create(env('APP_PATH') . 'wechat/view/api/script/index.js', 'view', 200, [
'content-type' => 'application/x-javascript;charset=utf-8',
'cache-control' => 'no-cache', 'pragma' => 'no-cache', 'expires' => '0',
])->assign($assign);
}
}

View File

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

View File

@ -86,20 +86,21 @@ class WechatService
/**
* 获取微信网页JSSDK
* @param null|string $url 签名SDK
* @return array
* @throws \think\Exception
* @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\exception\PDOException
*/
public static function webJsSDK()
public static function webJsSDK($url = null)
{
switch (strtolower(sysconf('wechat_type'))) {
case 'api':
return WechatService::script()->getJsSign(request()->url(true));
return WechatService::script()->getJsSign(is_null($url) ? request()->url(true) : $url);
case 'thr':
default:
return WechatService::wechat()->jsSign(request()->url(true));
return WechatService::wechat()->jsSign(is_null($url) ? request()->url(true) : $url);
}
}

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit664b5cbfe1e72b3e993dce3dea0bcb98::getLoader();
return ComposerAutoloaderInit95af81df6ac420fb6c658e7c9ee159af::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit664b5cbfe1e72b3e993dce3dea0bcb98
class ComposerAutoloaderInit95af81df6ac420fb6c658e7c9ee159af
{
private static $loader;
@ -19,15 +19,15 @@ class ComposerAutoloaderInit664b5cbfe1e72b3e993dce3dea0bcb98
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit664b5cbfe1e72b3e993dce3dea0bcb98', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit95af81df6ac420fb6c658e7c9ee159af', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit664b5cbfe1e72b3e993dce3dea0bcb98', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit95af81df6ac420fb6c658e7c9ee159af', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit664b5cbfe1e72b3e993dce3dea0bcb98::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit95af81df6ac420fb6c658e7c9ee159af::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
@ -48,19 +48,19 @@ class ComposerAutoloaderInit664b5cbfe1e72b3e993dce3dea0bcb98
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit664b5cbfe1e72b3e993dce3dea0bcb98::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit95af81df6ac420fb6c658e7c9ee159af::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire664b5cbfe1e72b3e993dce3dea0bcb98($fileIdentifier, $file);
composerRequire95af81df6ac420fb6c658e7c9ee159af($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire664b5cbfe1e72b3e993dce3dea0bcb98($fileIdentifier, $file)
function composerRequire95af81df6ac420fb6c658e7c9ee159af($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit664b5cbfe1e72b3e993dce3dea0bcb98
class ComposerStaticInit95af81df6ac420fb6c658e7c9ee159af
{
public static $files = array (
'1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php',
@ -257,9 +257,9 @@ class ComposerStaticInit664b5cbfe1e72b3e993dce3dea0bcb98
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit664b5cbfe1e72b3e993dce3dea0bcb98::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit664b5cbfe1e72b3e993dce3dea0bcb98::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit664b5cbfe1e72b3e993dce3dea0bcb98::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit95af81df6ac420fb6c658e7c9ee159af::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit95af81df6ac420fb6c658e7c9ee159af::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit95af81df6ac420fb6c658e7c9ee159af::$classMap;
}, null, ClassLoader::class);
}