Пример #1
0
 public function run()
 {
     global $_SGLOBAL;
     if (empty($this->token)) {
         exit('Access Denied');
     }
     if (!WX_Utility::check_sign($this->token)) {
         exit('Access Denied');
     }
     if (strtolower($_SERVER['REQUEST_METHOD']) == 'get') {
         exit($_GET['echostr']);
     }
     if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
         //消息防重
         if (WX_Utility::check_post($postStr)) {
             echo $resultStr;
             exit;
         }
         $this->message = WX_Utility::parse($postStr);
         if (empty($this->message)) {
             WX_Utility::logging('waring', 'Request Failed');
             exit('Request Failed');
         }
         //初始化
         $this->weixin['op_wxid'] = WX_Utility::get_weixin($this->message['to']);
         if (!$this->weixin['op_wxid']) {
             exit('cant find this weixin');
         } else {
             $this->weixin['op_uid'] = $_SGLOBAL['db']->getone('select op_uid from ' . tname('open_member_weixin') . ' where id=' . $this->weixin['op_wxid']);
         }
         //获取启用的微信模块
         $modules = $_SGLOBAL['db']->getall('select wm.*,m.name from ' . tname('weixin_module') . ' as wm inner join ' . tname('open_module') . ' as m on wm.mid=m.mid where wm.enabled=1 and wm.op_uid="' . $this->weixin['op_uid'] . '" and wm.op_wxid="' . $this->weixin['op_wxid'] . '" order by displayorder desc');
         foreach ($modules as $k => $v) {
             $this->weixin['account']['modules'][$v['name']] = $v;
         }
         $this->modules = array_keys($this->weixin['account']['modules']);
         $this->modules[] = 'welcome';
         $this->modules[] = 'default';
         $this->modules = array_unique($this->modules);
         WX_Utility::logging('trace', $this->message);
         $this->response = $this->matcher();
         //根据消息类型选择消息匹配模块
         $this->response['content'] = $this->process();
         //加载消息匹配模块
         //如果没消息匹配模块,加载default模块
         if (empty($this->response['content']) || is_array($this->response['content']) && $this->response['content']['type'] == 'text' && empty($this->response['content']['content']) || is_array($this->response['content']) && $this->response['content']['type'] == 'news' && empty($this->response['content']['items'])) {
             $this->response['module'] = 'default';
             $this->response['content'] = $this->process();
         }
         WX_Utility::logging('response', $this->response);
         $resp = WX_Utility::response($this->response['content']);
         $mapping = array('[from]' => $this->message['from'], '[to]' => $this->message['to']);
         echo str_replace(array_keys($mapping), array_values($mapping), $resp);
         exit;
     }
 }