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}
---|---|---|
- + |
- |
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"} |