25.6.1 更新互动记录
项目的群发功能是使用客服接口实现的,而客服接口发送消息有48小时互动限制,上次互动超过48小时后,则无法再向该用户发送消息。因此在程序中需要记录用户最后一次互动的时间。其相应的代码如下。
1 // 响应 2 public function responseMsg 3 { 4 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 5 if (!empty($postStr)){ 6 $this->logger("R ".$postStr); 7 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 8 $RX_TYPE = trim($postObj->MsgType); 9 10 if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe" || $postObj->Event == "TEMPLATESENDJ- OBFINISH")){ 11 // 过滤关注、取消关注、模板消息等事件 12 }else{ 13 // 更新互动记录 14 Db::name('user')->where('openid',strval($postObj->FromUserName))-> setField('heartbeat', time); 15 } 16 // 消息类型分离 17 switch ($RX_TYPE) 18 { 19 case "event": 20 $result = $this->receiveEvent($postObj); 21 break; 22 case "text": 23 $result = $this->receiveText($postObj); 24 break; 25 default: 26 $result = "unknown msg type: ".$RX_TYPE; 27 break; 28 } 29 $this->logger("T ".$result); 30 echo $result; 31 }else { 32 echo ""; 33 exit; 34 } 35 }
对于一些特别的事件,需要进行过滤处理,如关注、取消关注以及发送模板消息后的上报。其他的正常用户互动接收到以后,则更新该用户的hearbeat的时间戳。