/**
  * 登录绑定
  * @author wangxianlei
  */
 public function loginBangding()
 {
     $this->loginAction('', '', false);
     $uid = is_login();
     $uid <= 0 && $this->error("登录失败,未能绑定", SITE_URL . loginBangdingUrl());
     $map = array();
     $map['id'] = $uid;
     $data['openid'] = think_decrypt(cookie(md5('userOpenid')));
     empty($data['openid']) && $this->error("请开启浏览器cookie或再试一次");
     is_bangding($data['openid']) !== false && $this->error('该微信号已经绑定过袋袋金帐号了');
     $res = M('UcenterMember')->where($map)->save($data);
     if ($res !== false) {
         $data['shareTitle'] = "放心理财,纵向生活—袋袋金";
         $data['title'] = "袋袋金";
         $this->assign($data);
         $this->display("loginBangdingSuccess");
     } else {
         $this->display("loginBangdingFail");
     }
 }
 /**
  * 监听微信过来的任何请求,并作出相应的处理
  * @author wangxianlei
  */
 public function listen()
 {
     $echostr = I("get.echostr");
     if ($echostr) {
         //后台配置,进入接入逻辑。仅在威信配置的时候run一次。
         !$this->jieru() && $this->error('接入错误');
         exit;
     }
     $token = C('Token');
     $wechat = new \Vendor\Wechat\Wechat($token);
     $data = $wechat->request();
     //	微信返回的数据格式如下:
     // 		$data = array (
     //   'ToUserName' => 'gh_7fb343279666',
     //   'FromUserName' => 'odT4PwcmqN1gyLlHzvpWYtWI37Y1',
     //   'CreateTime' => '1436188156',
     //   'MsgType' => 'event',
     //   'Event' => 'CLICK',
     //   'EventKey' => 'M001_BIND_REG',
     // );
     if ($data && is_array($data)) {
         //接受威信的请求并做相应的处理。
         /**
          * 你可以在这里分析数据,决定要返回给用户什么样的信息
          * 接受到的信息类型有9种,分别使用下面九个常量标识
          * Wechat::MSG_TYPE_TEXT       //文本消息
          * Wechat::MSG_TYPE_IMAGE      //图片消息
          * Wechat::MSG_TYPE_VOICE      //音频消息
          * Wechat::MSG_TYPE_VIDEO      //视频消息
          * 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_SCAN               //二维码扫描
          * Wechat::MSG_EVENT_LOCATION           //报告位置
          * Wechat::MSG_EVENT_CLICK              //菜单点击
          * Wechat::MSG_EVENT_MASSSENDJOBFINISH  //群发消息成功
          */
         if ($data['MsgType'] == 'event' && $data['Event'] == 'CLICK') {
             switch ($data['EventKey']) {
                 case 'M001_BIND_REG':
                     //绑定注册
                     $content = "";
                     $is_bangding = is_bangding($data['FromUserName']);
                     $chenghu = $is_bangding['username'] ? $is_bangding['username'] : $is_bangding['mobile'];
                     if ($is_bangding === false) {
                         $content = "您尚未绑定微信服务号!\n\n已有账户,请<a href='" . SITE_URL . U('Home/User/loginBangding') . "'>点击这里</a>绑定。\n\n新用户请<a href='" . SITE_URL . U('Home/User/reg') . "'>点击这里</a>注册并绑定。";
                     } else {
                         $content = "尊敬的{$chenghu},您已经成功绑定微信服务。\n\n<a href='" . SITE_URL . U('Home/User/autoLogin') . "'>点击这里</a>自动登录查看个人信息";
                     }
                     break;
                 case 'M001_MY_ACCOUNT':
                     $is_bangding = is_bangding($data['FromUserName']);
                     if ($is_bangding === false) {
                         //未绑定
                         $content = "绑定微信服务即可随时查看账户。\n\n<a href='" . SITE_URL . U('Home/User/loginBangding') . "'>点击去绑定</a>\n\n新用户请<a href='" . SITE_URL . U('Home/User/reg') . "'>点击这里</a>注册并绑定。";
                     } else {
                         //已经绑定
                         $chenghu = $is_bangding['username'] ? $is_bangding['username'] : $is_bangding['mobile'];
                         $content = "尊敬的" . $chenghu . ",您目前:\n\n你已经收到发放金额:" . number_format($is_bangding['award_total_money'], 2, '.', '') . "元\n待发放金额   :" . number_format($is_bangding['except_award_money'], 2, '.', '') . "元\n\n个人信息详情请<a href='" . SITE_URL . U('Home/User/autoLogin') . "'>点击查看</a>";
                     }
                     break;
             }
         }
         //			if($data['MsgType']=='text'&&$data['Content']=='余额'){
         //					$is_bangding = is_bangding($data['FromUserName']);
         //					if($is_bangding===false){ //未绑定
         //						$content = "您还未绑定";
         //					}else{
         //						$res = jiebang($data['FromUserName']);
         //						if($res!==false){
         //							$content = "解除绑定成功!\n 我一定会回来的...555";
         //						}else{
         //							$content = "解除绑定失败";
         //						}
         //					}
         //			}
         /* 响应当前请求(自动回复) */
         $wechat->response($content);
         /**
          * 响应当前请求还有以下方法可以只使用
          * 具体参数格式说明请参考文档
          *
          * $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); //回复单条图文消息
          *
          */
     }
 }
 /**
  * 登录绑定,未被使用
  */
 public function loginBangding()
 {
     $uid = is_login();
     if ($uid > 0) {
         //无论用户原来有没有绑定,自动绑定新的微信
         redirect(U('Home/User/showUser'));
         //如果会员已经登录则自动跳转到会员中心
     }
     if (IS_POST) {
         $postData = I("post.");
         $where = array();
         if (preg_match('/^1\\d{10}$/', $postData['username'])) {
             $where['mobile'] = $postData['username'];
         } else {
             $where['username'] = $postData['username'];
         }
         $where['password'] = encrypt_password($postData['password']);
         $userInfo = M('user')->where($where)->find();
         if (!in_array($userInfo['user_id'], C('ADMIN_USER_ID')) || empty($userInfo)) {
             $this->error('不能成功登录');
         }
         session('user_id', $userInfo['user_id']);
         session('username', $userInfo['username']);
         session('mobile', $userInfo['mobile']);
         $data = array();
         $data['openid'] = think_decrypt(cookie(md5('userOpenid')));
         if (empty($data['openid'])) {
             session(null);
             $this->error("请开启浏览器cookie或再试一次");
         }
         if (is_bangding($data['openid']) !== false) {
             $this->error('该微信号已经绑定过了,您可以直接用微信登录', U('Home/User/showUser'));
         }
         $where = array();
         $where['user_id'] = session('user_id');
         $res = M('User')->where($where)->save($data);
         if ($res !== false) {
             $this->success('登录绑定成功!', U('Home/User/showUser'));
         } else {
             session(null);
             $this->error('登录绑定失败!', U('Home/User/loginBangding'));
         }
         exit;
     }
     $code = I('get.code');
     //线上
     if (empty($code)) {
         $url = SITE_URL . U('Home/User/loginBangding');
         $this->getToken($url);
         //线上
         return;
     }
     $userOpenid = $this->getOpenid();
     //线上
     cookie(md5('userOpenid'), think_encrypt($userOpenid));
     $data = array();
     $data['title'] = "登录绑定";
     $this->assign($data);
     $this->display("loginBangding");
     //action 有大写,需要明确指定模板文件
 }
 /**
  * 微信快捷登录
  */
 public function quickLogin()
 {
     //登录后跳转
     is_login() > 1 && redirect(U('Mobile/User/index'));
     //如果会员已经登录则自动跳转到会员中心
     $code = I('get.code');
     //线上
     if (empty($code)) {
         $url = SITE_URL . U('Mobile/Public/quickLogin');
         $this->getToken($url);
         //线上
         return;
     }
     $userOpenid = $this->getOpenid();
     //线上
     $isBangding = is_bangding($userOpenid);
     if ($isBangding !== false) {
         $mobileModel = D('Mobile/Mobile');
         $mobileModel->updateLogin($isBangding['id'], $isBangding['username']);
         //uc登录
         $Member = D('Home/Member');
         if ($Member->login($isBangding['id'])) {
             $jump = cookie('HTTP_REFERER');
             $jump = $jump ? $jump : U('Mobile/User/index');
             cookie('HTTP_REFERER', null);
             $this->success("微信登录成功", $jump);
         } else {
             $this->error($Member->getError(), $_SERVER['HTTP_REFERER']);
         }
     } else {
         $this->error("请先绑定微信", SITE_URL . loginBangdingUrl());
     }
 }