cxhxy/extend/hema/wechat/Index.php
2023-12-06 17:40:02 +08:00

91 lines
2.6 KiB
PHP

<?php
namespace hema\wechat;
use app\applet\model\Setting as SettingModel;
use app\applet\model\User as UserModel;
use app\common\model\Setting;
use app\common\model\Applet;
use app\common\model\Wechat;
use Requests;
use think\facade\Cache;
use hema\Http;
class Index
{
private $config;
private $error;
/**
* 构造函数
*/
public function __construct()
{
$config = [
'app_id' => 'wx89c12dd426a55a2e',
'appSecret' => '33e66bcf944f9810abbb5ddd7825403d',
'token' => 'cxhxy',
];
}
public function index()
{
if (isset($_GET['echostr'])) {
echo $_GET["echostr"];
exit;
}
//接收微信推送数据
$nonce = empty ($_GET ['nonce']) ? "" : trim($_GET ['nonce']);
$signature = empty ($_GET['signature']) ? "" : trim($_GET ['signature']);
$timeStamp = empty ($_GET ['timestamp']) ? "" : trim($_GET ['timestamp']);
$msg_signature = empty ($_GET['msg_signature']) ? "" : trim($_GET ['msg_signature']);
$encryptMsg = file_get_contents('php://input');
write_log($encryptMsg, __DIR__);
die('success');
$pc = new WxBizMsgCrypt();//创建解密类
$msg = '';
$errCode = $pc->decryptMsg($msg_signature, $timeStamp, $nonce, $encryptMsg, $msg);
if ($errCode == 0) {
$data = _xml_to_arr($msg); //XML转换为数组
//首次关注公众号
if ($data['MsgType'] == 'event') {
if ($data['Event'] == 'subscribe') {
$wx = new Driver;
if ($wechat_user = $wx->getWechatUserInfo($data['FromUserName'], 10001)) {
//用户操作
$model = new UserModel;
if ($user = $model->subscribe($wechat_user, $this->wechat['applet_id'])) {
}
} else {
//返回文本提醒
$wx->sendServiceMsg([
'type' => 'text',
'content' => $wx->getError()
], $data['FromUserName'], $this->wechat['applet_id']);
}
//是否设置了关注回复
if ($subscribe = SettingModel::getItem('subscribe', $this->wechat['applet_id'])) {
$this->replyMsg($subscribe, $data);//回复信息
}
die('success');
}
}
}
}
}