[ 'class' => LoginBehavior::className(), 'ignore' => [ 'wechat/index', ] ]*/ ]); } private $_msg_template = array( 'text' => '%s',//文本回复XML模板 'image' => '%s',//图片回复XML模板 'music' => '%s<![CDATA[%s]]>',//音乐模板 'news' => '%s%s%s',// 新闻主体 'news_item' => '<![CDATA[%s]]>',//某个新闻模板 ); /** * @微信入口 */ public function actionIndex(){ if(!empty($_GET["signature"]) && !empty($_GET['echostr'])){ return $this->checkSignature(); } return $this->responseMsg(); } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = "3dxh"; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return $_GET['echostr']; }else{ return false; } } public function responseMsg(){ $xml_str = file_get_contents("php://input"); /* 获得请求时POST:XML字符串 不能用$_POST获取,因为没有key */ \Yii::info("进入",'wxpay'); $msg = ""; if(!empty($xml_str)){ // 解析该xml字符串,利用simpleXML // libxml_disable_entity_loader(true); //禁止xml实体解析,防止xml注入 $request_xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA); \Yii::info(json_encode($request_xml),'wxpay'); // $msg = sprintf($this->_msg_template['text'], $request_xml->FromUserName, $request_xml->ToUserName, time(), ); \Yii::info($msg,'wxpay'); //判断该消息的类型,通过元素MsgType switch ($request_xml->MsgType){ case 'event': //判断具体的时间类型(关注、取消、点击) $event = $request_xml->Event; if ($event=='subscribe') { // 关注事件 // $this->_doSubscribe($request_xml); }elseif ($event=='CLICK') {//菜单点击事件 // $this->_doClick($request_xml); }elseif ($event=='VIEW') {//连接跳转事件 // $this->_doView($request_xml); }elseif ($event=='SCAN') { $id = $request_xml->EventKey; // 查找数据,并发送文本 $find = Dev::findOne([ 'id' => $id, ]); if(empty($find)){ return false; } // 查找门店 $find_store = Store::findOne([ 'id' => $find->store_id, ]); $wechat_mp = ""; $wechat_app = WechatApp::findOne([ 'cx_mch_id' => 0, 'is_delete' => 0 ]); if($wechat_app){ if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $cert_pem_file = null; if ($wechat_app->cert_pem) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->cert_pem); if (!file_exists($cert_pem_file)) file_put_contents($cert_pem_file, $wechat_app->cert_pem); } $key_pem_file = null; if ($wechat_app->key_pem) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->key_pem); if (!file_exists($key_pem_file)) file_put_contents($key_pem_file, $wechat_app->key_pem); } $wechat_mp = new Wechat([ 'appId' => $wechat_app->app_id, 'appSecret' => $wechat_app->app_secret, 'mchId' => $wechat_app->mch_id, 'apiKey' => $wechat_app->key, 'certPem' => $cert_pem_file, 'keyPem' => $key_pem_file, ]); // 推送客服消息 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$wechat_mp->getAccessToken(); $to_url = urlencode(SiteHelper::getCustomiseOptionByKey("siteDomain", "hump")."/site"); $state = $find->id."_".$find->store_id."_".time(); $res_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$wechat_app->app_id}&redirect_uri={$to_url}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect"; try{ $to_user = reset($request_xml->FromUserName); }catch (\Exception $e){ $to_user = $request_xml->FromUserName; } $arr = [ 'touser' => $to_user, 'msgtype' => 'news', 'news' => [ 'articles' => [ [ 'title' => '欢迎访问'.SiteHelper::getCustomiseOptionByKey("siteName", "hump"), 'description' => "请点击访问链接,配合工作人员完成步骤 - ".$find_store->name, 'url' => $res_url, 'picurl' => SiteHelper::getCustomiseOptionByKey("siteDomain", "hump").'/upload/0/1/upload/image/2023/1026/1698312821606159.jpg', ] ], ], ]; $res = $wechat_mp->curl->post($url,json_encode($arr,JSON_UNESCAPED_UNICODE)); \Yii::info(json_encode($res),'wxpay'); \Yii::info(json_encode($arr).$request_xml->FromUserName,'wxpay'); } $text = "欢迎来到英舒迪公众号"; $msg = sprintf($this->_msg_template['text'], $request_xml->FromUserName, $request_xml->ToUserName, time(),$text); $msg = ""; }else{ } break; case 'text'://文本消息 // $this->_doText($request_xml); break; case 'image'://图片消息 // $this->_doImage($request_xml); break; case 'voice'://语音消息 // $this->_doVoice($request_xml); break; case 'video'://视频消息 // $this->_doVideo($request_xml); break; case 'shortvideo'://短视频消息 // $this->_doShortvideo($request_xml); break; case 'location'://位置消息 // $this->_doLocation($request_xml); break; case 'link'://链接消息 // $this->_doLink($request_xml); break; } } return $msg; } /** * @ 更新菜单 */ public function actionUpMenu(){ $wechat_app = WechatApp::findOne([ 'cx_mch_id' => 0, 'is_delete' => 0 ]); if($wechat_app){ if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $cert_pem_file = null; if ($wechat_app->cert_pem) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->cert_pem); if (!file_exists($cert_pem_file)) file_put_contents($cert_pem_file, $wechat_app->cert_pem); } $key_pem_file = null; if ($wechat_app->key_pem) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->key_pem); if (!file_exists($key_pem_file)) file_put_contents($key_pem_file, $wechat_app->key_pem); } $wechat_mp = new Wechat([ 'appId' => $wechat_app->app_id, 'appSecret' => $wechat_app->app_secret, 'mchId' => $wechat_app->mch_id, 'apiKey' => $wechat_app->key, 'certPem' => $cert_pem_file, 'keyPem' => $key_pem_file, ]); // 推送客服消息 $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$wechat_mp->getAccessToken(); $to_url = urlencode(SiteHelper::getCustomiseOptionByKey("siteDomain", "hump")."/site"); $res_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$wechat_app->app_id}&redirect_uri={$to_url}&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect"; $arr = [ 'button' => [ [ 'type' => 'view', 'name' => '查看报告', 'url' => $res_url, ] ], ]; $res = $wechat_mp->curl->post($url,json_encode($arr,JSON_UNESCAPED_UNICODE)); var_dump($res); } } }