From 6e18f7ff679e972088ac20f12198783c56eac840 Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 19 Mar 2018 13:29:54 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=A8=A1=E5=9D=97=E6=8E=88=E6=9D=83=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=8F=8C=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/wechat/controller/Config.php | 24 ++- application/wechat/controller/api/Push.php | 38 +++- application/wechat/service/FansService.php | 4 +- application/wechat/service/MediaService.php | 2 +- application/wechat/service/TagsService.php | 2 +- application/wechat/view/block/index.html | 11 +- application/wechat/view/config/index.html | 182 ++++++++++++++------ application/wechat/view/fans/index.html | 3 - application/wechat/view/keys/index.html | 6 +- extend/service/WechatService.php | 55 +++++- vendor/autoload.php | 2 +- vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_real.php | 14 +- vendor/composer/autoload_static.php | 9 +- 14 files changed, 248 insertions(+), 105 deletions(-) diff --git a/application/wechat/controller/Config.php b/application/wechat/controller/Config.php index caacc43ee..333604aea 100644 --- a/application/wechat/controller/Config.php +++ b/application/wechat/controller/Config.php @@ -44,21 +44,29 @@ class Config extends BasicAdmin { if ($this->request->isGet()) { $code = encode(url('@admin', '', true, true) . '#' . $this->request->url()); - $assign = [ + return $this->fetch('', [ 'title' => '微信接口配置', 'appuri' => url("@wechat/api.push", '', true, true), - 'appid' => $this->request->get('appid', sysconf('wechat_appid')), - 'appkey' => $this->request->get('appkey', sysconf('wechat_appkey')), + 'appid' => $this->request->get('appid', sysconf('wechat_thr_appid')), + 'appkey' => $this->request->get('appkey', sysconf('wechat_thr_appkey')), 'authurl' => "http://wm.cuci.cc/wechat/api.push/auth/{$code}.html", 'wechat' => WechatService::instance('config')->getConfig(), - ]; - return $this->fetch('', $assign); + ]); } try { + // 接口对接类型 + sysconf('wechat_type', $this->request->post('wechat_type')); + // 直接参数对应 + sysconf('wechat_token', $this->request->post('wechat_token')); sysconf('wechat_appid', $this->request->post('wechat_appid')); - sysconf('wechat_appkey', $this->request->post('wechat_appkey')); - $apiurl = $this->request->post('wechat_appurl'); - if (!empty($apiurl)) { + sysconf('wechat_appsecret', $this->request->post('wechat_appsecret')); + sysconf('wechat_encodingaeskey', $this->request->post('wechat_encodingaeskey')); + // 第三方平台配置 + sysconf('wechat_thr_appid', $this->request->post('wechat_thr_appid')); + sysconf('wechat_thr_appkey', $this->request->post('wechat_thr_appkey')); + // 第三方平台时设置远程平台通知接口 + if ($this->request->post('wechat_type') === 'thr') { + $apiurl = url('@wechat/api.push', '', true, true); if (!WechatService::instance('config')->setApiNotifyUri($apiurl)) { $this->error('远程服务端接口更新失败,请稍候再试!'); } diff --git a/application/wechat/controller/api/Push.php b/application/wechat/controller/api/Push.php index c1a847b49..2a165a88f 100644 --- a/application/wechat/controller/api/Push.php +++ b/application/wechat/controller/api/Push.php @@ -50,26 +50,56 @@ class Push /** * 微信消息接口 + * @return string * @throws \think\Exception * @throws \think\exception\PDOException */ - public function __construct() + public function index() { $request = app('request'); $this->appid = $request->post('appid', '', null); $this->openid = $request->post('openid', '', null); $this->receive = unserialize($request->post('receive', '', null)); - p($this->receive); if (empty($this->appid) || empty($this->openid) || empty($this->receive)) { throw new Exception('微信API实例缺失必要参数[appid,openid,event].'); } - if ($this->appid !== sysconf('wechat_appid')) { + return $this->call($this->openid, $this->appid, $this->receive); + } + + /** + * 公众号直接对接 + * @return string + * @throws \think\Exception + * @throws \think\exception\PDOException + */ + public function notify() + { + $wechat = WechatService::receive(); + return $this->call(WechatService::getAppid(), $wechat->getOpenid(), $wechat->getReceive()); + } + + /** + * 初始化接口 + * @param string $appid 公众号APPID + * @param string $openid 公众号OPENID + * @param array $revice 消息对象 + * @return string + * @throws Exception + * @throws \think\exception\PDOException + */ + protected function call($appid, $openid, $revice) + { + $this->appid = $appid; + $this->openid = $openid; + $this->receive = $revice; + if ($this->appid !== WechatService::getAppid()) { throw new Exception('微信API实例APPID验证失败.'); } // text,event,image,location if (method_exists($this, ($method = $this->receive['MsgType']))) { - $this->$method(); + return $this->$method(); } + return 'success'; } /** diff --git a/application/wechat/service/FansService.php b/application/wechat/service/FansService.php index d7732cbb7..3c3b565b0 100644 --- a/application/wechat/service/FansService.php +++ b/application/wechat/service/FansService.php @@ -35,7 +35,7 @@ class FansService */ public static function set(array $user) { - $user['appid'] = sysconf('wechat_appid'); + $user['appid'] = WechatService::getAppid(); if (!empty($user['subscribe_time'])) { $user['subscribe_at'] = date('Y-m-d H:i:s', $user['subscribe_time']); } @@ -58,7 +58,7 @@ class FansService */ public static function get($openid) { - $map = ['openid' => $openid, 'appid' => sysconf('wechat_appid')]; + $map = ['openid' => $openid, 'appid' => WechatService::getAppid()]; $user = Db::name('WechatFans')->where($map)->find(); foreach (['country', 'province', 'city', 'nickname', 'remark'] as $k) { isset($user[$k]) && $user[$k] = ToolsService::emojiDecode($user[$k]); diff --git a/application/wechat/service/MediaService.php b/application/wechat/service/MediaService.php index 888f9caec..8dd14f7a1 100644 --- a/application/wechat/service/MediaService.php +++ b/application/wechat/service/MediaService.php @@ -84,7 +84,7 @@ class MediaService */ public static function uploadForeverMedia($local_url, $type = 'image', $video_info = []) { - $map = ['md5' => md5($local_url), 'appid' => sysconf('wechat_appid')]; + $map = ['md5' => md5($local_url), 'appid' => WechatService::getAppid()]; if (($media_id = Db::name('WechatNewsMedia')->where($map)->value('media_id'))) { return $media_id; } diff --git a/application/wechat/service/TagsService.php b/application/wechat/service/TagsService.php index 52ec49ffc..327884afe 100644 --- a/application/wechat/service/TagsService.php +++ b/application/wechat/service/TagsService.php @@ -54,7 +54,7 @@ class TagsService */ public static function sync() { - $appid = sysconf('wechat_appid'); + $appid = WechatService::getAppid(); $result = WechatService::tags()->getTags(); Db::name('WechatFansTags')->where(['appid' => $appid])->delete(); foreach (array_chunk($result['tags'], 100) as $list) { diff --git a/application/wechat/view/block/index.html b/application/wechat/view/block/index.html index f79abcfc8..220b24181 100644 --- a/application/wechat/view/block/index.html +++ b/application/wechat/view/block/index.html @@ -1,15 +1,12 @@ {extend name='admin@public/content'} {block name="button"} - - - {/block} {block name="content"} @@ -94,12 +91,12 @@

没 有 记 录 哦!

- + @@ -113,10 +110,10 @@ {foreach $list as $key=>$vo}
- + 用户昵称 性别
- + - + {$vo.nickname|default='未设置微信昵称'} diff --git a/application/wechat/view/config/index.html b/application/wechat/view/config/index.html index 3b67f7d96..f8bb15a59 100644 --- a/application/wechat/view/config/index.html +++ b/application/wechat/view/config/index.html @@ -3,79 +3,151 @@ {block name="content"}
- -
-
-
-
- -
-
-

微信昵称:{$wechat.nick_name}

-

微信类型:{$wechat.service_type_info == 2 ? '服务号' : '订阅号'} / - {$wechat.verify_type_info == -1 ? '未认证' : '已认证'}

-

注册公司:{$wechat.principal_name}

-

授权绑定:{$wechat.create_at|format_datetime}

-
-
-
-
-
-
-

------- 公众号功能测试 -------

-
- -

网页授权

-
-
- -

JSSDK签名

-
-
-
-
- -
- +
- -

点击连接将跳转到微信第三方平台进行公众号授权。

+ {php} + $wechat_type=sysconf('wechat_type')?sysconf('wechat_type'):'api'; + $wechat_type=request()->get('appkey')?'thr':'api'; + {/php} + {foreach ['api'=>'普通接口参数','thr'=>'第三方授权对接'] as $k=>$v} + + {/foreach} +

如果使用第三方授权对接,需要ThinkService项目的支持。

-
- -
- -

公众号 appid 通过微信第三方授权自动获取. 若没有值请进行微信第三方授权。

+
+
+ +
+ +

公众号应用ID是所有接口必要参数,可以在公众号平台 [ 开发 > 基本配置 ] 页面获取。

+
+
+ +
+ +

公众号应用密钥是所有接口必要参数,可以在公众号平台 [ 开发 > 基本配置 ] 页面授权后获取。

+
+
+
+ +
+ +

公众号平台与系统对接认证Token,请优先填写此参数并保存,然后再在微信公众号平台操作对接。

+
+
+
+ +
+ +

公众号平台接口设置为加密模式,消息加密密钥必需填写并保持与公众号平台一致。

+
+
+
+ +
+ +

公众号服务平台接口通知URL, 公众号消息接收与回复等。

+
+
+
-
- -
- -

公众号服务平台接口密钥, 通过微信第三方授权自动获取, 若没有值请进行微信第三方授权。

-
-
-
- -
- -

公众号服务平台接口通知URL, 公众号消息接收与回复等。

+
+ +
+
WechatQrc
公众号二维码
+
+
+ +
+
+

微信昵称:{$wechat.nick_name}

+

微信类型:{$wechat.service_type_info == 2 ? '服务号' : '订阅号'} / + {$wechat.verify_type_info == -1 ? '未认证' : '已认证'}

+

注册公司:{$wechat.principal_name}

+

授权绑定:{$wechat.create_at|format_datetime}

+
+
+
+
TestQrc
测试二维码
+
+

------- 公众号功能测试 -------

+
+ +

网页授权

+
+
+ +

JSSDK签名

+
+
+
+
+ +
+ +
+ +

点击连接将跳转到微信第三方平台进行公众号授权。

+
+
+
+
+ +
+ +

公众号 appid 通过微信第三方授权自动获取. 若没有值请进行微信第三方授权。

+
+
+
+ +
+ +

公众号服务平台接口密钥, 通过微信第三方授权自动获取, 若没有值请进行微信第三方授权。

+
+
+
+ +
+ +

公众号服务平台接口通知URL, 公众号消息接收与回复等。

+
+
+
-
-
+ + {/block} diff --git a/application/wechat/view/fans/index.html b/application/wechat/view/fans/index.html index c3cffc4dc..2a5dad7dd 100644 --- a/application/wechat/view/fans/index.html +++ b/application/wechat/view/fans/index.html @@ -1,15 +1,12 @@ {extend name='admin@public/content'} {block name="button"} - - - {/block} {block name="content"} diff --git a/application/wechat/view/keys/index.html b/application/wechat/view/keys/index.html index 42c4f161d..aa719723d 100644 --- a/application/wechat/view/keys/index.html +++ b/application/wechat/view/keys/index.html @@ -14,9 +14,9 @@ {block name='content'}
- {if empty($list)} +

没 有 记 录 哦!

- {else} + @@ -94,7 +94,7 @@
{if isset($page)}

{$page|raw}

{/if} - {/if} +
{/block} diff --git a/extend/service/WechatService.php b/extend/service/WechatService.php index 2690a23ad..40404007c 100644 --- a/extend/service/WechatService.php +++ b/extend/service/WechatService.php @@ -15,6 +15,7 @@ namespace service; use app\wechat\service\FansService; +use function Couchbase\defaultDecoder; /** * 微信数据服务 @@ -40,8 +41,8 @@ use app\wechat\service\FansService; * @method \WeChat\Template template() static 模板消息 * @method \WeChat\User user() static 微信粉丝管理 * @method \WeChat\Wifi wifi() static 门店WIFI管理 - * @method void wechat static 第三方微信工具 - * @method void config static 第三方配置工具 + * @method void wechat() static 第三方微信工具 + * @method void config() static 第三方配置工具 */ class WechatService { @@ -49,17 +50,35 @@ class WechatService /** * 获取微信实例ID * @param string $name 实例对象名称 - * @return SoapService + * @return SoapService|string * @throws \think\Exception * @throws \think\exception\PDOException */ public static function instance($name) { - list($appid, $appkey) = [sysconf('wechat_appid'), sysconf('wechat_appkey')]; - $token = strtolower("{$name}-{$appid}-{$appkey}"); - $location = config('wechat.service_url') . "/wechat/api.client/soap/param/{$token}.html"; - $params = ['uri' => strtolower($name), 'location' => $location, 'trace' => true]; - return new SoapService(null, $params); + switch (strtolower(sysconf('wechat_type'))) { + case 'api': + $config = [ + 'token' => sysconf('wechat_token'), + 'appid' => sysconf('wechat_appid'), + 'appsecret' => sysconf('wechat_appsecret'), + 'encodingaeskey' => sysconf('wechat_encodingaeskey'), + 'mch_id' => sysconf('wechat_mch_id'), + 'partnerkey' => sysconf('wechat_partnerkey'), + 'ssl_cer' => sysconf('wechat_cert_cert'), + 'ssl_key' => sysconf('wechat_cert_key'), + 'cachepath' => env('cache_path') . 'wechat' . DIRECTORY_SEPARATOR, + ]; + $type = '\\WeChat\\' . ucfirst(strtolower($name)); + return new $type($config); + case 'thr': + default: + list($appid, $appkey) = [sysconf('wechat_thr_appid'), sysconf('wechat_thr_appkey')]; + $token = strtolower("{$name}-{$appid}-{$appkey}"); + $location = config('wechat.service_url') . "/wechat/api.client/soap/param/{$token}.html"; + $params = ['uri' => strtolower($name), 'location' => $location, 'trace' => true]; + return new SoapService(null, $params); + } } /** @@ -71,7 +90,7 @@ class WechatService */ public static function webOauth($fullMode = 0) { - $appid = sysconf('wechat_appid'); + $appid = sysconf('wechat_thr_appid'); list($openid, $fansinfo) = [session("{$appid}_openid"), session("{$appid}_fansinfo")]; if ((empty($fullMode) && !empty($openid)) || (!empty($fullMode) && !empty($fansinfo))) { empty($fansinfo) || FansService::set($fansinfo); @@ -90,6 +109,24 @@ class WechatService } } + /** + * 获取当前公众号的AppId + * @return bool|string + * @throws \think\Exception + * @throws \think\exception\PDOException + */ + public static function getAppid() + { + switch (strtolower(sysconf('wechat_type'))) { + case 'api': + return sysconf('wechat_appid'); + case 'thr': + return sysconf('wechat_thr_appid'); + default: + return ''; + } + } + /** * 魔术静态方法实现对象 * @param string $name diff --git a/vendor/autoload.php b/vendor/autoload.php index eb05b85ef..d9e0dd263 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -4,4 +4,4 @@ require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInitf5ccf8306b43a082cf18a5c251d77c48::getLoader(); +return ComposerAutoloaderInitaf24f5f94ed079e6264991343eebb4cc::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 2d3310104..419ff198e 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -159,6 +159,7 @@ return array( 'app\\wechat\\controller\\Review' => $baseDir . '/application/wechat/controller/Review.php', 'app\\wechat\\controller\\Tags' => $baseDir . '/application/wechat/controller/Tags.php', 'app\\wechat\\controller\\api\\Push' => $baseDir . '/application/wechat/controller/api/Push.php', + 'app\\wechat\\controller\\api\\Tools' => $baseDir . '/application/wechat/controller/api/Tools.php', 'app\\wechat\\service\\FansService' => $baseDir . '/application/wechat/service/FansService.php', 'app\\wechat\\service\\MediaService' => $baseDir . '/application/wechat/service/MediaService.php', 'app\\wechat\\service\\TagsService' => $baseDir . '/application/wechat/service/TagsService.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index dc3c6e6ae..f3f2679a0 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInitf5ccf8306b43a082cf18a5c251d77c48 +class ComposerAutoloaderInitaf24f5f94ed079e6264991343eebb4cc { private static $loader; @@ -19,15 +19,15 @@ class ComposerAutoloaderInitf5ccf8306b43a082cf18a5c251d77c48 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInitf5ccf8306b43a082cf18a5c251d77c48', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInitaf24f5f94ed079e6264991343eebb4cc', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); - spl_autoload_unregister(array('ComposerAutoloaderInitf5ccf8306b43a082cf18a5c251d77c48', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInitaf24f5f94ed079e6264991343eebb4cc', '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\ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInitaf24f5f94ed079e6264991343eebb4cc::getInitializer($loader)); } else { $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { @@ -48,19 +48,19 @@ class ComposerAutoloaderInitf5ccf8306b43a082cf18a5c251d77c48 $loader->register(true); if ($useStaticLoader) { - $includeFiles = Composer\Autoload\ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48::$files; + $includeFiles = Composer\Autoload\ComposerStaticInitaf24f5f94ed079e6264991343eebb4cc::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { - composerRequiref5ccf8306b43a082cf18a5c251d77c48($fileIdentifier, $file); + composerRequireaf24f5f94ed079e6264991343eebb4cc($fileIdentifier, $file); } return $loader; } } -function composerRequiref5ccf8306b43a082cf18a5c251d77c48($fileIdentifier, $file) +function composerRequireaf24f5f94ed079e6264991343eebb4cc($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 5dfc1d439..57bc42524 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48 +class ComposerStaticInitaf24f5f94ed079e6264991343eebb4cc { public static $files = array ( '1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php', @@ -237,6 +237,7 @@ class ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48 'app\\wechat\\controller\\Review' => __DIR__ . '/../..' . '/application/wechat/controller/Review.php', 'app\\wechat\\controller\\Tags' => __DIR__ . '/../..' . '/application/wechat/controller/Tags.php', 'app\\wechat\\controller\\api\\Push' => __DIR__ . '/../..' . '/application/wechat/controller/api/Push.php', + 'app\\wechat\\controller\\api\\Tools' => __DIR__ . '/../..' . '/application/wechat/controller/api/Tools.php', 'app\\wechat\\service\\FansService' => __DIR__ . '/../..' . '/application/wechat/service/FansService.php', 'app\\wechat\\service\\MediaService' => __DIR__ . '/../..' . '/application/wechat/service/MediaService.php', 'app\\wechat\\service\\TagsService' => __DIR__ . '/../..' . '/application/wechat/service/TagsService.php', @@ -251,9 +252,9 @@ class ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInitf5ccf8306b43a082cf18a5c251d77c48::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInitaf24f5f94ed079e6264991343eebb4cc::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInitaf24f5f94ed079e6264991343eebb4cc::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInitaf24f5f94ed079e6264991343eebb4cc::$classMap; }, null, ClassLoader::class); }