This commit is contained in:
尖叫 2023-12-06 17:29:50 +08:00
parent 55e2f8c7e8
commit 0e6cb66fd5
2 changed files with 74 additions and 65 deletions

View File

@ -1,6 +1,8 @@
<?php <?php
namespace app\api\controller; namespace app\api\controller;
use hema\wechat\Index;
use hema\wechat\Pay as WxPay; use hema\wechat\Pay as WxPay;
use app\api\model\Record as RecordModel; use app\api\model\Record as RecordModel;
@ -14,9 +16,11 @@ class Wechat
/** /**
* 微信小程序充值成功异步通知 * 微信小程序充值成功异步通知
*/ */
public function wxapp() public function index()
{ {
$WxPay = new WxPay([]); $WxPay = new Index();
$WxPay->notify(new RecordModel,'','add'); $WxPay->index();
var_dump(2222222222);
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
namespace hema\wechat; namespace hema\wechat;
use app\applet\model\Setting as SettingModel; use app\applet\model\Setting as SettingModel;
@ -14,14 +15,20 @@ class Index
{ {
private $config; private $config;
private $error; private $error;
/** /**
* 构造函数 * 构造函数
*/ */
public function __construct() public function __construct()
{ {
$value = Setting::getItem('wxopen');
$value['component_access_token'] = Cache::get('component_access_token',''); $config = [
$this->config = $value; 'app_id' => 'wx89c12dd426a55a2e',
'appSecret' => '33e66bcf944f9810abbb5ddd7825403d',
'token' => 'cxhxy',
];
} }
@ -29,61 +36,59 @@ class Index
public function index() public function index()
{ {
if (isset($_GET['echostr'])) {
echo $_GET["echostr"];
exit;
}
die('success');
//接收微信推送数据 //接收微信推送数据
$nonce = empty ( $_GET ['nonce'] ) ?"" : trim ( $_GET ['nonce'] ); $nonce = empty ($_GET ['nonce']) ? "" : trim($_GET ['nonce']);
$signature = empty ( $_GET['signature'] ) ? "" : trim ( $_GET ['signature'] ); $signature = empty ($_GET['signature']) ? "" : trim($_GET ['signature']);
$timeStamp = empty ( $_GET ['timestamp']) ? "" : trim ( $_GET ['timestamp'] ); $timeStamp = empty ($_GET ['timestamp']) ? "" : trim($_GET ['timestamp']);
$msg_signature = empty ( $_GET['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] ); $msg_signature = empty ($_GET['msg_signature']) ? "" : trim($_GET ['msg_signature']);
$encryptMsg = file_get_contents ('php://input' ); $encryptMsg = file_get_contents('php://input');
$pc = new WxBizMsgCrypt();//创建解密类 $pc = new WxBizMsgCrypt();//创建解密类
$msg = ''; $msg = '';
$errCode = $pc->decryptMsg($msg_signature, $timeStamp, $nonce, $encryptMsg, $msg); $errCode = $pc->decryptMsg($msg_signature, $timeStamp, $nonce, $encryptMsg, $msg);
if($errCode == 0){ if ($errCode == 0) {
$data = _xml_to_arr($msg); //XML转换为数组 $data = _xml_to_arr($msg); //XML转换为数组
//首次关注公众号 //首次关注公众号
if($data['MsgType']=='event'){ if ($data['MsgType'] == 'event') {
if($data['Event']=='subscribe'){ if ($data['Event'] == 'subscribe') {
$wx = new Driver; $wx = new Driver;
if($wechat_user = $wx->getWechatUserInfo($data['FromUserName'],10001)){ if ($wechat_user = $wx->getWechatUserInfo($data['FromUserName'], 10001)) {
//用户操作 //用户操作
$model = new UserModel; $model = new UserModel;
if($user = $model->subscribe($wechat_user,$this->wechat['applet_id'])){ if ($user = $model->subscribe($wechat_user, $this->wechat['applet_id'])) {
if($this->wechat['applet_id'] == 0){ if ($this->wechat['applet_id'] == 0) {
$wx->sendServiceMsg([ $wx->sendServiceMsg([
'type' => 'text', 'type' => 'text',
'content' => '请妥善保管您的账号和密码~账号:' . $user['user_name'] . ',密码:' .$user['password'] . ',如忘记回复“找回账号”找回~' 'content' => '请妥善保管您的账号和密码~账号:' . $user['user_name'] . ',密码:' . $user['password'] . ',如忘记回复“找回账号”找回~'
],$data['FromUserName'],$this->wechat['applet_id']); ], $data['FromUserName'], $this->wechat['applet_id']);
} }
} }
if(isset($data['Ticket']) and $data['EventKey'] == 'qrscene_login'){ if (isset($data['Ticket']) and $data['EventKey'] == 'qrscene_login') {
//扫码关注公众号 //扫码关注公众号
Cache::set($data['Ticket'],$wechat_user,3600); Cache::set($data['Ticket'], $wechat_user, 3600);
} }
}else{ } else {
//返回文本提醒 //返回文本提醒
$wx->sendServiceMsg([ $wx->sendServiceMsg([
'type' => 'text', 'type' => 'text',
'content' => $wx->getError() 'content' => $wx->getError()
],$data['FromUserName'],$this->wechat['applet_id']); ], $data['FromUserName'], $this->wechat['applet_id']);
} }
//是否设置了关注回复 //是否设置了关注回复
if($subscribe = SettingModel::getItem('subscribe',$this->wechat['applet_id'])){ if ($subscribe = SettingModel::getItem('subscribe', $this->wechat['applet_id'])) {
$this->replyMsg($subscribe,$data);//回复信息 $this->replyMsg($subscribe, $data);//回复信息
} }
die('success'); die('success');
} }
} }
} }
} }
} }