关注公众号之后,回复内容

关注公众号之后,回复内容,比如说:
感谢关注!点击链接,参与活动.https://www.baidu.com

## 控制器代码

public function gzh_msg(Request $request)
    {
        $data = $request->all();
        $md = new AawxMini();
        $info = $md->gzh_msg_a($data);
        return $info;
}

## 模型代码

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Auth;
use App\Wxb\Wxm;//微信加密解密
class AawxMini extends Model
{
    public function gzh_msg_a($data)
    {
        if(isset($data['echostr'])){
            $res = $this->gzh_msg_0($data);
            return $res;
        }else{
            $res = $this->responseMsg2($data);
            return $res;
        }
    }
    /**
     * 
     * 验证消息的确来自微信服务器
     * 检验signature
     * */
    public function gzh_msg_0($data)
    {
        a_log('a_gzh_3','a',['a'=>$data]);
        $signature = $data["signature"];
        $timestamp = $data["timestamp"];
        $nonce = $data["nonce"];

        $token = env('wx_mini_msg');
        a_log('a_gzh_3','a',['a'=>$token]);
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if ($tmpStr == $signature ) {
            return $data['echostr'];
        } else {
            return false;
        }
    }
    /**
     * 回复消息
     * */
    public function responseMsg2($data)
    {
        $postStr = file_get_contents('php://input');//php7.0以上用这个接收参数、
        if(!empty($postStr)){
            //解密
            $md = new Wxm();
            $msg_sign = $data['msg_signature'] ?? '';
            $timeStamp = $data['timestamp'] ?? '';
            $nonce = $data['nonce'] ?? '';
            $res = $md->ab_decrypt($postStr,$msg_sign,$timeStamp,$nonce);
            if($res['code'] == 1){
                $postStr = $res['data'];
            }
            //解析post来的XML为一个对象$postObj
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName; //请求消息的用户
            $toUsername = $postObj->ToUserName; //"我"的公众号id
            $keyword = trim($postObj->Content); //消息内容
            $time = time(); //时间戳
            $msgtype = 'text'; //消息类型:文本
            $textTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            </xml>";
            if(strtolower($postObj->MsgType == 'event' )){ //如果XML信息里消息类型为event
                if($postObj->Event == 'subscribe'){ //如果是订阅事件
                    $contentStrq = "感谢关注!
<a href='https://www.baidu.com'>点击链接,打开百度</a>
客服电话:1234
关键词回复
1 今天天气怎么样?
2 明天会下雨吗?
";
                    $resultStrq = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStrq);
                    //加密
                    $md = new Wxm();
                    $res = $md->ab_encrypt($resultStrq,$nonce);
                    if($res['code'] == 1){
                        return $res['data'];
                    }
                    return $resultStrq;
                }
            }
            //关键词回复
            switch ($keyword) {
                case '1':
                    $contentStr = '不知道。';
                    break;
                case '2':
                    $contentStr = '你猜。';
                    break;
            }

            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
            //加密
            $md = new Wxm();
            $res = $md->ab_encrypt($resultStr,$nonce);
            if($res['code'] == 1){
                return $res['data'];
            }
            return $resultStr;
 
        }else {
            return false;
        }
    }
}

## 微信加密解密代码,定义一个单独的类

<?php
namespace App\Wxb;
require_once base_path('vendor/abv').'/wxBizMsgCrypt.php';
class Wxm
{
	protected $token;
	protected $encodingAesKey;
	protected $appId;
	protected $pc;
	public function __construct()
	{
		$this->token = env('gzh_token');//公众号的配置项,自定义
        $this->encodingAesKey = env('gzh_key');//公众号的配置项,自定义
        $this->appId = env('gzh_appid');//公众号的id
        $this->pc = new \WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
	}
	/**
	 * 加密,返回加密之后的内容
	 * text 自己要发送的数据,xml格式的
	 * nonce 微信发送过来的字符串,请求链接里的
	 * 
	 * */
	public function ab_encrypt($text,$nonce)
	{
		$timeStamp = time();
		$encryptMsg = '';
		$errCode = $this->pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
		if ($errCode == 0) {
			return ['code'=>1,'data'=>$encryptMsg];
		} else {
			return ['code'=>0,'msg'=>$errCode];
		}
	}
	/**
	 * 解密
	 * encryptMsg 微信发送的数据
	 * msg_sign 微信发送的数据,请求链接里的
	 * timeStamp 微信发送的数据,请求链接里的
	 * nonce 微信发送的数据,请求链接里的
	 * 解密之后,也是xml格式的,之后可以转化为obj的
	 * */
	public function ab_decrypt($encryptMsg,$msg_sign,$timeStamp,$nonce)
	{
		$xml_tree = new \DOMDocument();
		$xml_tree->loadXML($encryptMsg);
		$array_e = $xml_tree->getElementsByTagName('Encrypt');
		$array_s = $xml_tree->getElementsByTagName('MsgSignature');
		$encrypt = $array_e->item(0)->nodeValue;
		//$msg_sign = $array_s->item(0)->nodeValue;
		$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
		$from_xml = sprintf($format, $encrypt);
		$msg = '';
		$errCode = $this->pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
		if ($errCode == 0) {
			return ['code'=>1,'data'=>$msg];
		} else {
			return ['code'=>0,'msg'=>$errCode];
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值