Browse Source

[更新]微信模块开发中

邹景立 8 years ago
parent
commit
ffde6065da

+ 30 - 38
application/wechat/controller/Api.php

@@ -45,11 +45,12 @@ class Api extends Controller {
         /* 实例接口对象 */
         $this->wechat = &load_wechat('Receive');
         /* 验证接口请求 */
-        if ($this->wechat->valid() === FALSE) {
-            Log::error("微信被动接口验证失败,{$this->wechat->errMsg}[{$this->wechat->errCode}]");
-            exit($this->wechat->errMsg);
+        if ($this->wechat->valid() === false) {
+            $msg = "微信消息验证失败,{$this->wechat->errMsg}[{$this->wechat->errCode}]";
+            Log::error($msg);
+            exit($msg);
         }
-        /* 获取openid */
+        /* 获取消息openid */
         $this->openid = $this->wechat->getRev()->getRevFrom();
         /* 同步粉丝数据 */
         $this->_syncFans(true);
@@ -65,7 +66,7 @@ class Api extends Controller {
             case WechatReceive::MSGTYPE_LOCATION:
                 exit($this->_location());
             default:
-                exit($this->_default());
+                exit('success');
         }
     }
 
@@ -75,52 +76,51 @@ class Api extends Controller {
      * @param bool $default 是否启用默认模式
      * @return string
      */
-    protected function _keys($keys, $default = FALSE) {
+    private function _keys($keys, $default = false) {
         list($table, $field, $value) = explode('#', $keys . '##');
-        $info = Db::table($table)->where('appid', $this->appid)->where($field, $value)->find();
-        if ($info) {
-            # 转发给多客服
+        $info = Db::name($table)->where($field, $value)->find();
+        if ($info && is_array($info) && isset($info['type'])) {
+            // 转发给多客服
             if (!empty($info['type']) && $info['type'] === 'customservice') {
-                $this->wechat->sendCustomMessage(array('touser' => $this->openid, 'msgtype' => 'text', 'text' => array('content' => $info['content'])));
+                $this->wechat->sendCustomMessage(['touser' => $this->openid, 'msgtype' => 'text', 'text' => ['content' => $info['content']]]);
                 return $this->wechat->transfer_customer_service()->reply();
             }
-            # 无法给出回复时调用默认回复机制
+            // 无法给出回复时调用默认回复机制
             array_key_exists('status', $info) && empty($info['status']) && exit('success');
             switch ($info['type']) {
-                case 'keys':/* 关键字 */
+                case 'keys': /* 关键字 */
                     empty($info['content']) && empty($info['name']) && exit('success');
                     return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content']));
-                case 'text':/* 文本消息 */
+                case 'text': /* 文本消息 */
                     empty($info['content']) && exit('success');
                     return $this->wechat->text($info['content'])->reply();
-                case 'news':/* 图文消息 */
+                case 'news': /* 图文消息 */
                     empty($info['news_id']) && exit('success');
                     return $this->_news($info['news_id']);
-                case 'music':/* 音频消息 */
+                case 'music': /* 音频消息 */
                     (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) && exit('success');
                     $media_id = empty($info['music_image']) ? '' : WechatService::uploadForeverMedia($info['music_image'], 'image');
                     empty($media_id) && exit('success');
                     return $this->wechat->music($info['music_title'], $info['music_desc'], $info['music_url'], $info['music_url'], $media_id)->reply();
-                case 'voice':/* 语音消息 */
+                case 'voice': /* 语音消息 */
                     empty($info['voice_url']) && exit('success');
                     $media_id = WechatService::uploadForeverMedia($info['voice_url'], 'voice')->reply();
                     empty($media_id) && exit('success');
                     return $this->wechat->voice($media_id)->reply();
-                case 'image':/* 图文消息 */
+                case 'image': /* 图文消息 */
                     empty($info['image_url']) && exit('success');
                     $media_id = WechatService::uploadForeverMedia($info['image_url'], 'image');
                     empty($media_id) && exit('success');
                     return $this->wechat->image($media_id)->reply();
-                case 'video':/* 视频消息 */
+                case 'video': /* 视频消息 */
                     (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) && exit('success');
-                    $data = array('title' => $info['video_title'], 'introduction' => $info['video_desc']);
-                    $media_id = WechatService::uploadForeverMedia($info['video_url'], 'video', TRUE, $data);
+                    $data = ['title' => $info['video_title'], 'introduction' => $info['video_desc']];
+                    $media_id = WechatService::uploadForeverMedia($info['video_url'], 'video', true, $data);
                     return $this->wechat->video($media_id, $info['video_title'], $info['video_desc'])->reply();
             }
         }
-
         $default && exit('success');
-        return $this->_keys('wechat_keys#keys#default', TRUE);
+        return $this->_keys('wechat_keys#keys#default', true);
     }
 
 
@@ -130,7 +130,7 @@ class Api extends Controller {
      * @return bool|string
      */
     protected function _news($news_id = 0) {
-        if (is_array($newsinfo = WechatService::getNewsById($news_id))) {
+        if (is_array($newsinfo = WechatService::getNewsById($news_id)) && !empty($newsinfo['articles'])) {
             $newsdata = array();
             foreach ($newsinfo['articles'] as $vo) {
                 $newsdata[] = [
@@ -151,23 +151,23 @@ class Api extends Controller {
     protected function _event() {
         $event = $this->wechat->getRevEvent();
         switch (strtolower($event['event'])) {
+            /* 关注事件 */
             case 'subscribe':
-                /* 关注事件 */
                 $this->_syncFans(true);
                 if (!empty($event['key']) && stripos($event['key'], 'qrscene_') !== false) {
                     $this->_spread(preg_replace('|^.*?(\d+).*?$|', '$1', $event['key']));
                 }
                 return $this->_keys('wechat_keys#keys#subscribe');
+            /* 取消关注 */
             case 'unsubscribe':
-                /* 取消关注 */
                 $this->_syncFans(false);
                 exit('success');
+            /* 点击菜单 */
             case 'click':
-                /* 点击链接 */
                 return $this->_keys($event['key']);
+            /* 扫码推事件 */
             case 'scancode_push':
             case 'scancode_waitmsg':
-                /* 扫码推事件 */
                 $scanInfo = $this->wechat->getRev()->getRevScanInfo();
                 if (isset($scanInfo['ScanResult'])) {
                     return $this->_keys($scanInfo['ScanResult']);
@@ -186,7 +186,7 @@ class Api extends Controller {
      * @param string $key
      * @return mixed
      */
-    protected function _spread($key) {
+    private function _spread($key) {
         // 检测推荐是否有效
         $fans = Db::name('WechatFans')->where('id', $key)->find();
         if (!is_array($fans) || !isset($fans['openid']) || $fans['openid'] === $this->openid) {
@@ -204,22 +204,14 @@ class Api extends Controller {
      * 位置事情回复
      * @return string
      */
-    protected function _location() {
+    private function _location() {
         exit('success');
     }
 
     /**
      * 图片事件处理
      */
-    protected function _image() {
-        exit('success');
-    }
-
-    /**
-     * 默认事件处理
-     * @return string
-     */
-    protected function _default() {
+    private function _image() {
         exit('success');
     }
 

+ 32 - 0
application/wechat/controller/Notify.php

@@ -0,0 +1,32 @@
+<?php
+
+// +----------------------------------------------------------------------
+// | Think.Admin
+// +----------------------------------------------------------------------
+// | 版权所有 2016~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
+// +----------------------------------------------------------------------
+// | 官方网站: http://think.ctolog.com
+// +----------------------------------------------------------------------
+// | 开源协议 ( https://mit-license.org )
+// +----------------------------------------------------------------------
+// | github开源项目:https://github.com/zoujingli/Think.Admin
+// +----------------------------------------------------------------------
+
+namespace app\wechat\controller;
+
+use think\Controller;
+
+/**
+ * 微信支付通知处理控制器
+ * Class Notify
+ * @package app\wechat\controller
+ * @author Anyon <zoujingli@qq.com>
+ * @date 2017/04/05 14:02
+ */
+class Notify extends Controller {
+
+    public function index() {
+
+    }
+
+}

+ 1 - 1
extend/service/PayService.php

@@ -125,7 +125,7 @@ class PayService {
         $prepayinfo = Db::name('WechatPayPrepayid')->where($where, $map)->find();
         if (empty($prepayinfo) || empty($prepayinfo['prepayid'])) {
             $out_trade_no = DataService::createSequence(18, 'WXPAY-OUTER-NO');
-            $prepayid = $pay->getPrepayId($openid, $title, $out_trade_no, $fee, url("@store/api/notify", '', true, true), $trade_type);
+            $prepayid = $pay->getPrepayId($openid, $title, $out_trade_no, $fee, url("@wechat/notify", '', true, true), $trade_type);
             if (empty($prepayid)) {
                 Log::error("内部订单号{$order_no}生成预支付失败,{$pay->errMsg}");
                 return false;