[
'class' => LoginBehavior::className(),
'ignore' => [
'wechat/index',
]
]*/
]);
}
private $_msg_template = array(
'text' => '%s',//文本回复XML模板
'image' => '%s',//图片回复XML模板
'music' => '%s',//音乐模板
'news' => '%s%s%s',// 新闻主体
'news_item' => ' ',//某个新闻模板
);
/**
* @微信入口
*/
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;
}
$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(\Yii::$app->request->getHostInfo()."/site");
$state = $find->id."_".$find->store_id;
$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' => '333',
'description' => '23432432',
'url' => $res_url,
'picurl' => \Yii::$app->request->getHostInfo().'/upload/0/1/upload/image/2023/1026/1698312821606159.jpg',
]
],
],
];
$res = $wechat_mp->curl->post($url,json_encode($arr));
\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);
}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;
}
}