function __construct() { parent::__construct(); $this->JANUSKING_WECHAT = $this->WECHATCONFIG; }
/** * 微信方法 */ function __construct() { parent::__construct(); $this->employees = array('ouTetjpJvbB3o43-WzskTlKfHReQ' => 'VrWorking!'); $this->JANUSKING_WECHAT = $this->WECHATCONFIG; if (!array_key_exists('signature', $_GET) || !array_key_exists('nonce', $_GET) || !array_key_exists('timestamp', $_GET)) { // 外部调用微信方法API ///////////////////////////////////////////////////////////////////////////// // 外部调用微信处理必备四个固定参数 method, time, ver, hash if (!array_key_exists('method', $_GET)) { echo json_encode($this->_err[1000]); exit; } // check input params if (I('get.hash') == '') { echo json_encode($this->_err[1000]); exit; } if (strcmp($this->_version, I('get.ver')) != 0) { echo json_encode($this->_err[1001]); exit; } // verify params $params['method'] = I('get.method'); $params['time'] = I('get.time'); $params['ver'] = I('get.ver'); if (!$this->verifyHash(I('get.hash'), $params)) { echo json_encode($this->_err[1003]); exit; } // 业务参数检查 if (preg_match("/^[a-zA-Z]{2,}[a-zA-Z-]+[a-zA-Z]{2,}\$/", $params['method']) == false) { echo json_encode($this->_err[1001]); exit; } $str = explode('-', $params['method']); if (count($str) == 1) { $params['method'] = 'api' . strtoupper(substr($params['method'], 0, 1)) . substr($params['method'], 1); } else { foreach ($str as $k => $v) { if (strlen($v) > 0) { $str[$k] = strtoupper(substr($v, 0, 1)) . substr($v, 1); } } $params['method'] = 'api' . implode('', $str); } unset($str); $this->getToken(); // init wechat token // auto fire method if (method_exists($this, $params['method'])) { call_user_func(array(get_class($this), $params['method'])); } exit; } else { // 微信服务器回调方法API //////////////////////////////////////////////////////////////////////////// // valid message tt(array('unique_id' => 1, 'comment' => 'WX_GET:' . json_encode($_GET))); tt(array('unique_id' => 2, 'comment' => 'WX_HTTP_RAW_POST_DATA:' . json_encode($GLOBALS["HTTP_RAW_POST_DATA"]))); $tmpArr = array(self::MY_TOKEN, I('get.timestamp'), I('get.nonce')); sort($tmpArr, SORT_STRING); // 微信2014-03-04更正 $tmpStr = implode('', $tmpArr); $tmpStr = sha1($tmpStr); if ($tmpStr == I('get.signature')) { if (isset($_GET['echostr'])) { echo I('get.echostr'); // 来自微信的主动验证,直接返回 echostr 内容即可 tt(array('unique_id' => 3, 'comment' => '微信主动验证OK')); exit; } } else { tt(array('unique_id' => 4, 'comment' => 'signature ER: ' . $tmpStr . ' != ' . I('get.signature'))); exit; } // get message content $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (empty($postStr)) { exit; } // 解析消息 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $openID = (string) $postObj->FromUserName; // 发送方的openid $serviceID = (string) $postObj->ToUserName; // 服务号 $msgType = (string) $postObj->MsgType; // 消息类型 text event $content = (string) trim($postObj->Content); $wxCreateTime = (int) $postObj->CreateTime; // 过滤垃圾微信的事件通知 $cacheFilter = F('WX_FILTER_' . date('Ymd')); if ($cacheFilter === false) { $cacheFilter = array(); $cacheFilter[$openID]['event'] = ''; $cacheFilter[$openID]['msg'] = ''; $cacheFilter[$openID]['time'] = $wxCreateTime; } else { if (array_key_exists($openID, $cacheFilter)) { $lastCreateTime = (int) $cacheFilter[$openID]['time']; $lastEvent = (string) $cacheFilter[$openID]['event']; $lastMsg = (string) $cacheFilter[$openID]['msg']; if (strtolower($msgType) == 'event') { $event = strtolower((string) trim($postObj->EventKey)); if ($lastEvent == $event && $lastCreateTime == $wxCreateTime) { // 微信连续push相同的事件,忽略 echo ''; exit; } $cacheFilter[$openID]['event'] = $event; $cacheFilter[$openID]['msg'] = ''; $cacheFilter[$openID]['time'] = $wxCreateTime; } else { $msg = strtolower((string) trim($postObj->MsgId)); if ($lastMsg == $msg && $lastCreateTime == $wxCreateTime) { // 微信连续push相同的msg,忽略 echo ''; exit; } $cacheFilter[$openID]['event'] = ''; $cacheFilter[$openID]['msg'] = $msg; $cacheFilter[$openID]['time'] = $wxCreateTime; } } else { $cacheFilter[$openID]['msg'] = ''; $cacheFilter[$openID]['event'] = ''; $cacheFilter[$openID]['time'] = $wxCreateTime; } } F('WX_FILTER_' . date('Ymd'), $cacheFilter); $this->getToken(); // init wechat token // 以下是业务处理 $this->recordOpenUser($openID); // 尝试记录用户信息 $userID = 0; $userToken = ''; $userMobile = '0'; $userOS = 0; $checkUserBind = $this->checkUserBind($openID); // 检查此微信号是否绑定手机 if ($checkUserBind === false) { // unregist } else { $userID = $checkUserBind['user_id']; $userToken = $checkUserBind['user_token']; $userMobile = $checkUserBind['user_mobile']; $userOS = $checkUserBind['os']; } $model = M(); // 内部员工 $this->isEmployee = array_key_exists($openID, $this->employees); switch (strtolower($msgType)) { case 'event': // 接收 -> 事件推送 $event = (string) trim($postObj->Event); switch (strtolower($event)) { case 'subscribe': // 关注动作 $this->doSubscribe($openID, $userID, $userMobile, $this->time); $params = '{"touser": "******", "msgtype": "text", "text": {"content": "' . $this->lang['SUBSCRIBE'] . '"}}'; $this->push($openID, $params); break; case 'unsubscribe': // 取消关注动作 $this->doUnsubscribe($openID); break; case 'location': // 地理位置信息(用户自动上报) $lng = trim($postObj->Longitude); $lat = trim($postObj->Latitude); $address = $this->geodecode($lng, $lat, $userOS == 1 ? 'amap' : 'baidu'); // 逆向地理编码 $this->recordLocate($lng, $lat, $address, $openID, $userID); break; case 'click': // 自定义菜单的点击事件 $eventKey = (string) trim($postObj->EventKey); // 这个是自定义菜单中定义的 switch (strtoupper($eventKey)) { case 'K_PRICE': // 价格表 -> 回复文本消息 $this->response($openID, $serviceID, $priceText); break; } break; } break; case 'text': // 接收 -> 文本消息 $this->response($openID, $serviceID, $this->lang['DEFAULT']); // 默认消息 break; case 'image': // 接收 -> 图片消息 // 接收 -> 图片消息 case 'voice': // 接收 -> 语音消息 // 接收 -> 语音消息 case 'video': // 接收 -> 视频消息 // 接收 -> 视频消息 case 'link': // 接收 -> 链接消息 $this->response($openID, $serviceID, $this->lang['DEFAULT']); // 默认消息 break; case 'location': // 地理位置消息(用户手工上报) break; default: $this->response($openID, $serviceID, $this->lang['DEFAULT']); // 默认消息 } } exit; }