echostr=$echostr; $this->signature=$signature; $this->timestamp=$timestamp; $this->nonce =$nonce; $this->_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input"); } public function valid($token){ $this->token=$token; if($this->checkSignature()){ // echo $this->checkSignature(); libxml_disable_entity_loader(TRUE); $this->_POST = simplexml_load_string($this->_POST_DATA, 'SimpleXMLElement', LIBXML_NOCDATA); if($this->_POST){ $this->toUserName = strval($this->_POST->ToUserName); $this->fromUserName = strval($this->_POST->FromUserName); $this->msgType = $this->_POST->MsgType; } switch($this->msgType){ case 'event': $this->_parseEvent(); break; } }else{ echo '';exit; } } public function get($field){ $arr = (array)$this->_POST; if (isset($arr[$field])) { $result = $arr[$field]; } return $result; } public function _parseEvent(){ $this->event = $this->get('Event'); if($this->event){ $this->responseMsg($this->event); } } public function responseMsg($method='') { switch($method){ case 'subscribe': $this->_subscribe(); break; case 'SCAN': $this->_scan(); break; case 'CLICK': $this->_click($method); break; case 'VIEW': $this->_view(); break; } } /** * 用户直接关注的消息 * */ public function _text(){ $fromUsername = $this->fromUserName; $toUsername = $this->toUserName; $time = time(); $tpl= new wechattemplate('text'); $textTpl=$tpl->getTpl(); $contentStr = "欢迎关注漫行旅游租车,在这里您可以与我们分享您的骑行感受,或对我们的服务提出宝贵意见和建议,谢谢!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr); echo $resultStr; } private function setText($contentStr){ $fromUsername = $this->fromUserName; $toUsername = $this->toUserName; $time = time(); $tpl= new wechattemplate('text'); $textTpl=$tpl->getTpl(); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr); echo $resultStr; } private function _view(){ $this->_text(); } private function _click($method){ $fromUsername = $this->fromUserName; $toUsername = $this->toUserName; $time = time(); $tpl= new wechattemplate('text'); $textTpl=$tpl->getTpl(); $contentStr = "欢迎关注漫行旅游租车,在这里您可以与我们分享您的骑行感受,或对我们的服务提出宝贵意见和建议,谢谢!".$method; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr); echo $resultStr; } private function _scan(){ $fromUsername = $this->fromUserName; $EventKey=$this->get('EventKey'); if($EventKey){ $inform_config= $this->msg_config($fromUsername,$EventKey); switch($inform_config['status']){ case 20: $temapi= new wechattemplateapi($this->token,$inform_config['inform_config'],3); $temapi->send();//通知不成功,未做记录 break; case 21: $this->setText('此车已被借走,请借其它辆!'); break; case 22: $this->setText('此车不在锁柱,请借其它辆!'); break; case 30: $this->setText('此车属于你,请误借给他人使用!'); break; case 31: $this->setText('此车不属于你,请借其它辆或找回自己的车!'); break; case 40: $this->setText('此车不在借的状态,请借其它辆!'); break; case 60: $this->setText('此锁柱已坏,请借其它辆!'); break; case 61: $this->setText('非法锁柱,请借其它辆!'); break; case 62: $this->setText('此锁柱无自行车,请借其它辆!'); break; case 98: $temapi= new wechattemplateapi($this->token,$inform_config['inform_config'],3); $temapi->send();//通知不成功,未做记录 break; case 99: $this->setText('未找到此车!请借其它辆!'); break; default : $this->setText('未找到此车!请借其它辆!'); } }else{ $this->_text(); } } private function _subscribe(){ $fromUsername = $this->fromUserName; $toUsername = $this->toUserName; $EventKey=$this->get('EventKey'); $time = time(); if($EventKey){ $EventKey=$this->get('EventKey'); $key_arr=explode('_',$EventKey); if(isset($key_arr[2])){ $EventKey=$key_arr[1].'_'.$key_arr[2]; }else{ $EventKey=$key_arr[1]; } $inform_config= $this->msg_config($fromUsername,$EventKey); switch($inform_config['status']){ case 20: $temapi= new wechattemplateapi($this->token,$inform_config['inform_config'],3); $temapi->send();//通知不成功,未做记录 break; case 21: $this->setText('此车已被借走,请借其它辆!'); break; case 22: $this->setText('此车不在锁柱,请借其它辆!'); break; case 30: $this->setText('此车属于你,请误借给他人使用!'); break; case 31: $this->setText('此车不属于你,请借其它辆或找回自己的车!'); break; case 40: $this->setText('此车不在借的状态!请借其它辆!'); break; case 60: $this->setText('此锁柱已坏,请借其它辆!'); break; case 61: $this->setText('非法锁柱,请借其它辆!'); break; case 62: $this->setText('此锁柱无自行车,请借其它辆!'); break; case 98: $temapi= new wechattemplateapi($this->token,$inform_config['inform_config'],3); $temapi->send();//通知不成功,未做记录 break; case 99: $this->setText('未找到此车!请借其它辆!'); break; default : $this->setText('未找到此车!请借其它辆!'); } }else{ $tpl= new wechattemplate('text'); $textTpl=$tpl->getTpl(); $contentStr = "欢迎关注漫行旅游租车,在这里您可以与我们分享您的骑行感受,或对我们的服务提出宝贵意见和建议,谢谢!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr); echo $resultStr; } } /* * 文本消息 * * ToUserName: 开发者微信号 * FromUserName: 发送方帐号(一个OpenID) * CreateTime: 消息创建时间 (整型) * MsgType: text * Content: 文本消息内容 * MsgId: 消息id,64位整型 */ private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $this->signature; $timestamp = $this->timestamp; $nonce = $this->nonce; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } private function msg_config($fromUsername,$EventKey){ /** * 现在只有自行车编号 */ $data=array(); $bike_name='未知自行车'; $location_name='未知网点'; $bike_user_status=0; $bike_status=0; $lock_id=0; $bike_no=''; $lock_code=''; if($EventKey){ $type =explode('_',$EventKey); if(isset($type[1])){ if($type[0]=='L'){ //查锁柱 $lock_no= $type[1]; //查看锁柱是否存在。并查看锁柱状态 $l_lock_data= Yii::app()->db->createCommand()->from('lock_cylinder')->where('lock_cylinder_no=:lock_cylinder_no',array(':lock_cylinder_no'=>$lock_no))->queryRow(); //存在 if($l_lock_data){ $lock_id=$l_lock_data['lock_cylinder_id']; //锁柱已坏时 if($l_lock_data['status']==1){ $data=array(); $data['status']=60; $data['inform_config']=array(); return $data; } }else{ //不存在 $data=array(); $data['status']=61; $data['inform_config']=array(); return $data; } //锁柱存在时,查看锁上可有车 $l_bike_lock_data= Yii::app()->db->createCommand()->from('bike_lock_relation')->where('lock_cylinder_id=:lock_cylinder_id',array(':lock_cylinder_id'=>$lock_id))->queryRow(); if($l_bike_lock_data){ //存在时 $l_bike_data= Yii::app()->db->createCommand()->select('bike_id,bike_no,bike_category_id,status')->from('bike')->where('bike_id=:bike_id',array(':bike_id'=>$l_bike_lock_data['bike_id']))->queryRow(); if(!$l_bike_data){ $data=array(); $data['status']=99; $data['inform_config']=array(); return $data; } $bike_no=$l_bike_data['bike_no']; }else{ //不存在时 //获取管理器 $local_c_data= Yii::app()->db->createCommand()->select('location_controller_id,location_controller_no,status')->from('location_controller')->where('location_controller_id=:location_controller_id',array(':location_controller_id'=>$l_lock_data['location_controller_id']))->queryRow(); if($local_c_data){ $w_json= $this->getData(Yii::app()->params['hardware_api_url'].'query_state?code='.$local_c_data['location_controller_no']); $w_data=json_decode($w_json,1); if($w_data){ if(isset($w_data['stations'][0]['docks'])){ $docks=$w_data['stations'][0]['docks']; foreach($docks as $v) { if (isset($v['status']) && $v['status'] == 1) { if (isset($v['dockno']) && ($v['dockno'] == $lock_no)) { if(isset($v['bikecode']) && !empty($v['bikecode'])){ $bike_no = $v['bikecode']; } } } } if(empty($bike_no)){ $data=array(); $data['status']=62; $data['inform_config']=array(); return $data; } }else{ $data=array(); $data['status']=62; $data['inform_config']=array(); return $data; } }else{ $data=array(); $data['status']=62; $data['inform_config']=array(); return $data; } }else{ $data=array(); $data['status']=62; $data['inform_config']=array(); return $data; } } }else{ $bike_no=$type[1]; } }else{ $bike_no=$type[0]; } } $bike_data= Yii::app()->db->createCommand()->select('bike_id,bike_no,bike_category_id,status')->from('bike')->where('bike_no=:bike_no',array(':bike_no'=>$bike_no))->queryRow(); if($bike_data){ $bike_category_data= Yii::app()->db->createCommand()->select('category_name')->from('bike_category')->where('category_id=:category_id',array(':category_id'=>$bike_data['bike_category_id']))->queryRow(); if($bike_category_data){ $bike_name=$bike_category_data['category_name']; } /** * 车的状态 */ if($bike_data['status']==20){ //取出锁柱ID $bike_lock_data= Yii::app()->db->createCommand()->from('bike_lock_relation')->where('bike_id=:bike_id',array(':bike_id'=>$bike_data['bike_id']))->queryRow(); if($bike_lock_data){ //取网点id $lock_data= Yii::app()->db->createCommand()->select('lock_cylinder_id,lock_cylinder_no,location_id')->from('lock_cylinder')->where('lock_cylinder_id=:lock_cylinder_id',array(':lock_cylinder_id'=>$bike_lock_data['lock_cylinder_id']))->queryRow(); } if($lock_data){ $location_data= Yii::app()->db->createCommand()->select('location_id,location_no,location_name')->from('location')->where('location_id=:location_id',array(':location_id'=>$lock_data['location_id']))->queryRow(); if($location_data){ $location_name = $location_data['location_name']; } $url=Yii::app()->params['weixin_realm'].Yii::app()->createUrl('lend/index',array('openId'=>$fromUsername,'eventkey'=>$bike_no,'expires_time'=>time())); $account=$location_name; $data['status']=20; $data['inform_config']=array( 'url'=>$url, 'openId'=>$fromUsername, 'first'=>'恭喜您扫描成功', 'location'=>$location_name, 'bike_name'=>$bike_name, 'remark'=>'请在5分钟内完成租车,否则此链接将失效' ); }else{ $data['status']=22; $data['inform_config']=array(); } }elseif($bike_data['status']==30){ //判断 $w_json= $this->getData(Yii::app()->params['hardware_api_url'].'bike?bikecode='.$bike_no); $w_result=json_decode($w_json,1); if($w_result && isset($w_result['errcode']) && $w_result['errcode']){ $data['status']=21; } if($w_result && isset($w_result['isout']) && $w_result['isout']){ $lock_code=$w_result['dockno']; $location_no=$w_result['sitecode']; // $b_lock_data= Yii::app()->db->createCommand()->select('lock_cylinder_id,lock_cylinder_no,location_id')->from('lock_cylinder')->where('lock_cylinder_no=:lock_cylinder_no',array(':lock_cylinder_no'=>$lock_code))->queryRow(); if($b_lock_data){ $b_location_data= Yii::app()->db->createCommand()->select('location_id,location_no,location_name')->from('location')->where('location_id=:location_id',array(':location_id'=>$b_lock_data['location_id']))->queryRow(); } if($b_location_data){ $location_name=$b_location_data['location_name']; $url=Yii::app()->params['weixin_realm'].Yii::app()->createUrl('lend/index',array('openId'=>$fromUsername,'eventkey'=>$bike_no,'expires_time'=>time())); $account=$location_name; $data['status']=98; $data['inform_config']=array( 'url'=>$url, 'openId'=>$fromUsername, 'first'=>'恭喜您扫描成功', 'location'=>$location_name, 'bike_name'=>$bike_name, 'remark'=>'请在5分钟内完成租车,否则此链接将失效' ); } }else{ $data['status']=21; } }else{ $data['status']=40; $data['inform_config']=array(); } }else{ $data['status']=99; $data['inform_config']=array(); } /** * 验证人 */ $auth_data= Yii::app()->db->createCommand()->select('auth_id,auth_uid')->from('user_auth')->where('auth_uid=:auth_uid',array(':auth_uid'=>$fromUsername))->queryRow(); if($auth_data){ $user_auth_data= Yii::app()->db->createCommand()->from('user_auth_relation')->where('auth_id=:auth_id',array(':auth_id'=>$auth_data['auth_id']))->queryRow(); if($user_auth_data){ $bike_user_data= Yii::app()->db->createCommand()->from('bike_user_relation')->where('user_id=:user_id',array(':user_id'=>$user_auth_data['user_id']))->queryRow(); if($bike_user_data){ /** * 判断此车是否属于本人 * */ if($bike_data['bike_id']==$bike_user_data['bike_id']){ $data['status']=30; $data['inform_config']=array(); }else{ $data['status']=31; $data['inform_config']=array(); } }else{ $a_bike_data= Yii::app()->db->createCommand()->from('abnormal_bike_relation')->where('user_id=:user_id',array(':user_id'=>$user_auth_data['user_id']))->queryRow(); if($a_bike_data){ if($bike_data['bike_id']==$a_bike_data['bike_id']){ $data['status']=30; $data['inform_config']=array(); }else{ $data['status']=31; $data['inform_config']=array(); } } } } } //验检是否在锁柱上 return $data; } private function getData($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch); curl_close($ch); return $output; } }