This commit is contained in:
test_service 2023-12-07 09:58:44 +08:00
parent 36b4c5eac5
commit 6f762dc249
4 changed files with 1053 additions and 1038 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,91 +1,94 @@
<?php <?php
namespace hema\wechat; namespace hema\wechat;
use app\applet\model\Setting as SettingModel; use app\applet\model\Setting as SettingModel;
use app\applet\model\User as UserModel; use app\applet\model\User as UserModel;
use app\common\model\Setting; use app\common\model\Setting;
use app\common\model\Applet; use app\common\model\Applet;
use app\common\model\Wechat; use app\common\model\Wechat;
use Requests; use think\facade\Cache;
use think\facade\Cache; use hema\Http;
use hema\Http; use think\facade\Db;
class Index class Index
{ {
private $config; private $config;
private $error; private $error;
/** /**
* 构造函数 * 构造函数
*/ */
public function __construct() public function __construct()
{ {
$config = [ $config = [
'app_id' => 'wx89c12dd426a55a2e', 'app_id' => 'wx89c12dd426a55a2e',
'appSecret' => '33e66bcf944f9810abbb5ddd7825403d', 'appSecret' => '33e66bcf944f9810abbb5ddd7825403d',
'token' => 'cxhxy', 'token' => 'cxhxy',
]; ];
}
}
public function index()
public function index() {
{ if (isset($_GET['echostr'])) {
echo $_GET["echostr"];
if (isset($_GET['echostr'])) { exit;
echo $_GET["echostr"]; }
exit;
} //接收微信推送数据
$data = file_get_contents('php://input');
//接收微信推送数据 // $data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
$nonce = empty ($_GET ['nonce']) ? "" : trim($_GET ['nonce']); if (!empty($data)) {
$signature = empty ($_GET['signature']) ? "" : trim($_GET ['signature']); $data = json_decode($data, true);
$timeStamp = empty ($_GET ['timestamp']) ? "" : trim($_GET ['timestamp']); if (!empty($data)) {
$msg_signature = empty ($_GET['msg_signature']) ? "" : trim($_GET ['msg_signature']); //首次关注
$encryptMsg = file_get_contents('php://input'); if ($data['Event'] == 'subscribe') {
$user = Db::name('food_user')->where('open_id', $data['FromUserName'])->value('is_new');
write_log($encryptMsg, __DIR__); if ($user !== 1) {
//不是新关注
die('success'); die('success');
} else {
$pc = new WxBizMsgCrypt();//创建解密类 //插入一条新用户数据
$msg = ''; $data = [
$errCode = $pc->decryptMsg($msg_signature, $timeStamp, $nonce, $encryptMsg, $msg); 'open_id' => $data['FromUserName'],
if ($errCode == 0) { 'platform' => 10,
$data = _xml_to_arr($msg); //XML转换为数组 'applet_id' => 10001,
'create_time' => time()
//首次关注公众号 ];
if ($data['MsgType'] == 'event') { Db::name('food_user')->insert($data);
if ($data['Event'] == 'subscribe') { //发送公众号消息
$wx = new Driver;
if ($wechat_user = $wx->getWechatUserInfo($data['FromUserName'], 10001)) { $wx = new Driver;
//用户操作 $access_token = $wx->getAccessToken(10001);
$model = new UserModel; $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
if ($user = $model->subscribe($wechat_user, $this->wechat['applet_id'])) { $queryarr = [
'msgtype' => 'news',
} 'news' => [
} else { 'articles' => [
//返回文本提醒 'title' => '您有一张新的优惠券,请及时领取',
$wx->sendServiceMsg([ 'picurl' => '',
'type' => 'text', 'url' => 'https://www.baidu.com',
'content' => $wx->getError() 'description' => '黄辛一新人优惠,首次关注赠送优惠券'
], $data['FromUserName'], $this->wechat['applet_id']); ]
} ]
//是否设置了关注回复 ];
if ($subscribe = SettingModel::getItem('subscribe', $this->wechat['applet_id'])) { $queryarr['touser'] = $data['FromUserName'];
$this->replyMsg($subscribe, $data);//回复信息 Http::post($url, hema_json($queryarr));
}
die('success'); }
}
} die('success');
} }
} }
}
}
} }

View File

@ -0,0 +1,12 @@
[2023-12-06 18:11:51]
222222222222222222
[2023-12-06 18:11:51]
{"ToUserName":"gh_f3e7ab01d5bd","FromUserName":"oy5Wi6kx3rPhdeV001GbKcy-P9gk","CreateTime":"1701857511","MsgType":"event","Event":"unsubscribe","EventKey":{}}
[2023-12-06 18:11:54]
222222222222222222
[2023-12-06 18:11:54]
{"ToUserName":"gh_f3e7ab01d5bd","FromUserName":"oy5Wi6kx3rPhdeV001GbKcy-P9gk","CreateTime":"1701857514","MsgType":"event","Event":"subscribe","EventKey":{}}

View File

@ -1,4 +1,4 @@
<?php <?php
//000000000600 //000000000600
exit();?> exit();?>
1701848041 1701879149