File: /web/data/weixin.tbbbearing.com/App/Home/Controller/WeixinController.class.php
<?php
/**
 * 微信控制器 WeixinController.class.php
 * ============================================================================
 * 版权所有 (C) 2015-2016 壹尚科技有限公司,并保留所有权利。
 * 网站地址:   http://www.ethank.com.cn
 * ----------------------------------------------------------------------------
 * 许可声明:这是一个开源程序,未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * Author: 勾国印 (gouguoyin@ethank.com.cn) 
 * Date: 2015年9月27日 下午9:12:19  
*/
namespace Home\Controller;
use Think\Controller;
use Think\Com\Wechat;
use Think\Com\WechatAuth;
class WeixinController extends BaseController {
    public function _empty(){
        redirect(SITE_URL);
    }
    public function index(){
        $appid = C('GLOBAL_CONFIG.WX_AppID'); //AppID(应用ID)
        $token = C('GLOBAL_CONFIG.WX_Token'); //微信后台填写的TOKEN
        $crypt = C('GLOBAL_CONFIG.WX_EncodingAESKey'); //消息加密KEY(EncodingAESKey)
        /* 加载微信SDK */
        $wechat = new Wechat($token, $appid, $crypt);
        /* 获取请求信息 */
        $data = $wechat->request();
        if($data && is_array($data)){
            /**
             * 你可以在这里分析数据,决定要返回给用户什么样的信息
             * 接受到的信息类型有10种,分别使用下面10个常量标识
             * Wechat::MSG_TYPE_TEXT       //文本消息
             * Wechat::MSG_TYPE_IMAGE      //图片消息
             * Wechat::MSG_TYPE_VOICE      //音频消息
             * Wechat::MSG_TYPE_VIDEO      //视频消息
             * Wechat::MSG_TYPE_SHORTVIDEO //视频消息
             * Wechat::MSG_TYPE_MUSIC      //音乐消息
             * Wechat::MSG_TYPE_NEWS       //图文消息(推送过来的应该不存在这种类型,但是可以给用户回复该类型消息)
             * Wechat::MSG_TYPE_LOCATION   //位置消息
             * Wechat::MSG_TYPE_LINK       //连接消息
             * Wechat::MSG_TYPE_EVENT      //事件消息
             *
             * 事件消息又分为下面五种
             * Wechat::MSG_EVENT_SUBSCRIBE    //订阅
             * Wechat::MSG_EVENT_UNSUBSCRIBE  //取消订阅
             * Wechat::MSG_EVENT_SCAN         //二维码扫描
             * Wechat::MSG_EVENT_LOCATION     //报告位置
             * Wechat::MSG_EVENT_CLICK        //菜单点击
             */
        
        
            /* 响应当前请求(自动回复) */
            //$wechat->response($content, $type);
        
            /**
             * 响应当前请求还有以下方法可以使用
             * 具体参数格式说明请参考文档
             *
             * $wechat->replyText($text); //回复文本消息
             * $wechat->replyImage($media_id); //回复图片消息
             * $wechat->replyVoice($media_id); //回复音频消息
             * $wechat->replyVideo($media_id, $title, $discription); //回复视频消息
             * $wechat->replyMusic($title, $discription, $musicurl, $hqmusicurl, $thumb_media_id); //回复音乐消息
             * $wechat->replyNews($news, $news1, $news2, $news3); //回复多条图文消息
             * $wechat->replyNewsOnce($title, $discription, $url, $picurl); //回复单条图文消息
             *
             */
            $type  = $data['MsgType'];
            
            if ($type == 'event') {
                $keyword = $data['Event'];
            } else if($type == 'text') {
                $keyword = $data['Content'];
            }
            //$wechat->replyText($keyword);
//            ob_start();
//            print_r($keyword);
//            print_r($data);
//            print_r($_GET);
//            print_r($_POST);
//            print_r(C('GLOBAL_CONFIG'));
//            print_r(C('GLOBAL_CONFIG.WX_welcome'));
//            $html = ob_get_contents();
//            ob_clean();
//            file_put_contents(C('ROOT_PATH').'/'.'-'.date('Y_m_d_H_i_s',C('TIME')).'.txt',$html);
            $domain = get_domain();
            if($keyword=='subscribe'){
                /* 加载微信SDK */
                $weChat2 = new WechatAuth(C('GLOBAL_CONFIG.WX_AppID'),C('GLOBAL_CONFIG.WX_AppSecret'));
                $weChat2->getAccessToken();
                $weChat2->sendText('oweY8uDeCxlANQwTC0IeCQ_s9Wc4','有新用户订阅');
                //图文
                if(C('GLOBAL_CONFIG.WX_welcome_type')==1){
                    //回复单条图文消息
                    $wechat->replyNewsOnce(C('GLOBAL_CONFIG.WX_welcome_title'), C('GLOBAL_CONFIG.WX_welcome_1'), C('GLOBAL_CONFIG.WX_welcome_url'), $domain.C('GLOBAL_CONFIG.WX_welcome_imgurl'));
                }else{
                    $content=C('GLOBAL_CONFIG.WX_welcome');
                    $wechat->replyText($content);
                }
                return ;
            }elseif($keyword == 'unsubscribe'){
                /* 加载微信SDK */
                $weChat2 = new WechatAuth(C('GLOBAL_CONFIG.WX_AppID'),C('GLOBAL_CONFIG.WX_AppSecret'));
                $weChat2->getAccessToken();
                $weChat2->sendText('ogllo1MguWIpoKgamSpYSO9VY8o4','有人取消订阅');
                return ;
            }elseif(trim($keyword)){
                if($data['Event']=='CLICK' && $data['EventKey']){
                    $keyword = $data['EventKey'];
                }
                //检测是否后台配置有关键字回复
                $res = M('weixin_keyword_reply')->where('keyword="'.$keyword.'"')->find();
                if($res){
                    $content=$res['reply_content'];
                    if($res['reply_type']==0){
                        $wechat->replyText($content);
                    }elseif($res['reply_type']==1){
                        $wechat->replyNewsOnce($res['title'], $res['reply_content'], $res['url'],
                            (substr_count($res['imgurl'],'http')>0)?$res['imgurl']:$domain.$res['imgurl']);
                    }elseif($res['reply_type']==2){
                        $wechat->replyImage($res['media_id']);
                    }
                    return;
                }
                //检测是否有订单
                //订单查询
                $order_row = M('weixin_orders')->where('order_id="'.trim($keyword).'"')->find();
                if($order_row){
                    $wechat->replyNewsOnce('订单信息',"【订单号】{$order_row['order_id']}\r\n"."【订单状态】".($order_row['order_status']?'已发货':'未发货')."\r\n【产品名称】".$order_row['pro_name']."\r\n"."【兑换数量】{$order_row['qty']}\r\n【收货人】{$order_row['shipping_name']}\r\n【收货地址】{$order_row['shipping_area']} {$order_row['shipping_address']}\r\n",$domain.U('/Points/pro_detail','proid='.($order_row['proid'])),$domain.$order_row['pro_picture']);
                }
                $keyword_list =  M('weixin_keyword_reply')->where('is_default=1')->field('keyword')->select();
                $msg = '';
                foreach((array)$keyword_list as $k=>$v){
                    $msg .= ($k+1).'、'.$v['keyword']."\r\n";
                }
                $msg && $msg = "\r\n请尝试回复:\r\n".$msg;
                $wechat->replyText('暂无相关搜索内容'.$msg);
                
            }
        }
        
    }
    public function send_message()
    {
        $appid = C('GLOBAL_CONFIG.WX_AppID'); //AppID(应用ID)
        $token = C('GLOBAL_CONFIG.WX_Token'); //微信后台填写的TOKEN
        $crypt = C('GLOBAL_CONFIG.WX_EncodingAESKey'); //消息加密KEY(EncodingAESKey)
        /* 加载微信SDK */
        $wechat = new Wechat($token, $appid, $crypt);
    }
}