讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 25.6 群發實現 >

25.6 群發實現

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的時間戳。