C('WECHAT_APPID'), 'appsecret'=>C('WECHAT_APPSECRET'), 'token' =>'', 'token_cache_func'=>wx_token_cache, ); $weixin = new \Tencent\wechat($config); $callback = pb_abs_url('wxt_wechat/oauth2_userinfo'); $state = str2hex(I('get.uri')); $redirect_uri = $weixin->getOauthRedirect($callback,$state); header('Location: '.$redirect_uri); } public function callback( ){ $config = array( 'appid' =>C('WECHAT_APPID'), 'appsecret'=>C('WECHAT_APPSECRET'), 'token' => '', 'token_cache_func'=>wx_token_cache ); $weixin = new \Tencent\wechat($config); $type = $weixin->getRev()->getRevType(); $openid = $weixin->getRev()->getRevFrom(); $data = $weixin->getRevData(); $userinfo = $weixin->getUserInfo($openid); switch($type) { case 'text': $content = $weixin->getRev()->getRevContent(); $reply = $this->on_text($openid,$content,$userinfo); break; case 'event': $event = $weixin->getRev()->getRevEvent(); $reply = $this->on_event($event,$openid,$userinfo); break; /* case 'image': $content = $weixin->getRev()->getRevPic(); $reply = $this->on_image($content); break; case 'location': $content = $weixin->getRev()->getRevGeo(); $reply = $this->on_location($openid,$content['x'],$content['y'],$content['label']); break;*/ default: $reply = $this->on_other(); } if($reply){ echo $weixin->text($reply)->reply(); } else{ echo 'success'; } } public function oauth2_userinfo( ){ $config = array( 'appid' =>C('WECHAT_APPID'), 'appsecret'=>C('WECHAT_APPSECRET'), 'token' =>'', 'token_cache_func'=>wx_token_cache, ); $weixin = new \Tencent\wechat($config); $token = $weixin->getOauthAccessToken(); //echo json_encode($token);exit; if(!$token){ $this->on_oauth2_failed(); return; } $access_token = $token['access_token']; $openid = $token['openid']; $userinfo = $weixin->getOauthUserinfo($access_token,$openid); if(!$userinfo){ $this->on_oauth2_failed(); return; } $this->on_oauth2_userinfo($userinfo); $a = hex2str( I('get.state') ); if(strpos($a,'?') !== false){ $uri = htmlspecialchars_decode(hex2str( I('get.state') )) . '&openid='.$openid; }else{ $uri = htmlspecialchars_decode(hex2str( I('get.state') )) . '?openid='.$openid; } if(isset($userinfo['unionid']) && $userinfo['unionid']){ $uri .= "&unionid={$userinfo['unionid']}"; } header('Location: '.pb_abs_url($uri) ); } private function on_oauth2_userinfo( $userinfo ){ echo json_encode($userinfo); } public function on_oauth2_failed( ){ } private function on_event( $event, $openid, $userinfo ){ if($event['event'] == "subscribe") return $this->on_event_subscribe($openid,$userinfo); else if($event['event'] == "unsubscribe") return $this->on_event_unsubscribe($openid); else return $event['event']; } private function on_event_subscribe( $openid, $userinfo ){ // 添加映射关系 $this->add_union_map($openid,$userinfo['unionid']); $this->add_user_info($userinfo); return '欢迎关注O(∩_∩)O'; } private function on_event_unsubscribe( $openid ){ // 移除映射关系 $this->remove_union_map($openid); } private function on_text( $openid, $msg, $userinfo ){ if( $msg == '联盾行车卫士' ){ $res = $this->add_union_map($openid,$userinfo['unionid'],$msg); return $res['message']; } //return '欢迎关注O(∩_∩)O'; } public function send_text_msg( $openid, $msg ){ $config = array( 'appid' =>C('WECHAT_APPID'), 'appsecret'=>C('WECHAT_APPSECRET'), 'token' =>'', ); $weixin = new \Tencent\wechat($config); $data = array( "touser" => $openid, "msgtype"=> "text", "text" => array("content"=>$msg) ); return $weixin->sendCustomMessage($data); } private function on_other( ){ return 'other'; } public function on_image( ){ } public function on_location( ){ } private function add_union_map( $openid, $unionid, $text = "" ){ if(!$openid){ return array('success' => false, 'message' => 'missing openid'); } if(!$unionid){ return array('success' => false, 'message' => 'missing unionid'); } // 检查是否存在 $wx_union_map = M('wx_union_map'); $where = array( 'GzhOpenId' => $openid, 'UnionId' => $unionid ); if($wx_union_map->where($where)->count()){ return array('success' => true, 'message' => "已关联【{$text}】"); } // 不存在则添加一条记录 $add_data = array( 'ID' => create_guid(), 'GzhOpenId' => $openid, 'UnionId' => $unionid, 'AddTime' => date('Y-m-d H:i:s') ); $res = $wx_union_map->createAdd($add_data); if($res === false){ return array('success' => false, 'message' => '出错了,请稍后再试'); } return array('success' => true, 'message' => "关联【{$text}】成功"); } function site_and_qrcode( ){ $cityid = I('get.cityid'); if(!$cityid){ exit(""); } //预约单信息 $deviceNum = I('get.deviceNum'); if(empty($deviceNum)){ exit(""); } $hospitalId = I('get.hospitalId'); $qrCode = new \Endroid\QrCode\QrCode($deviceNum); $qrCode->setSize(200); $endroidQrCode = $qrCode->writeString(); $qrCode = base64_encode($endroidQrCode); //网点信息 $uc_user = M('uc_user'); $where = array('CityId'=>$cityid); if($hospitalId){ $where['ID'] = $hospitalId; } $places = M('pets_hospital')->where($where)->order('Qualification DESC')->select(); foreach($places as &$item){ $where = array('ID'=>$item['PrincipalId']); $item['Mobile'] = $uc_user->where($where)->getField('Mobile'); } $this->assign('deviceNum',$deviceNum); $this->assign('qrCode',$qrCode); $this->assign('places',$places); $this->display(); } private function remove_union_map( $openid, $text = "" ){ if(!$openid){ return array('success' => false, 'message' => 'missing openid'); } // 检查是否存在 $wx_union_map = M('wx_union_map'); $where = array( 'GzhOpenId' => $openid, ); if(!$wx_union_map->where($where)->count()){ return array('success' => true, 'message' => "已移除【{$text}】"); } // 存在则移除条记录 $res = $wx_union_map->where($where)->delete(); if($res === false){ return array('success' => false, 'message' => '出错了,请稍后再试'); } return array('success' => true, 'message' => "解除关联【{$text}】成功"); } private function add_user_info( $userinfo ){ $uc_user = M('uc_user'); $cond = array('UnionId'=>$userinfo['unionid']); $id = $uc_user->where($cond)->getField('ID'); // 用户不存在,添加信息 if(!$id){ $save_data = array( 'UnionId'=>$userinfo['unionid'], 'Avatar'=>$userinfo['headimgurl'], 'RealName'=>$userinfo['nickname'] ); $save_data['ID'] = create_guid(); $uc_user->createAdd($save_data); } return array('success' => true, 'message' => "添加成功"); } }