This commit is contained in:
尖叫 2023-12-07 10:01:30 +08:00
commit d90717879c
4 changed files with 105 additions and 94 deletions

View File

@ -1,3 +1,4 @@
<<<<<<< HEAD
<?php <?php
/** /**
* 应用公共函数库文件 * 应用公共函数库文件

View File

@ -1,94 +1,92 @@
<?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 think\facade\Cache; use think\facade\Cache;
use hema\Http; use hema\Http;
use think\facade\Db; 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'])) { if (isset($_GET['echostr'])) {
echo $_GET["echostr"]; echo $_GET["echostr"];
exit; exit;
} }
//接收微信推送数据 //接收微信推送数据
$data = file_get_contents('php://input'); $data = file_get_contents('php://input');
// $data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA); // $data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
if (!empty($data)) { if (!empty($data)) {
$data = json_decode($data, true); $data = json_decode($data, true);
if (!empty($data)) { if (!empty($data)) {
//首次关注 //首次关注
if ($data['Event'] == 'subscribe') { if ($data['Event'] == 'subscribe') {
$user = Db::name('food_user')->where('open_id', $data['FromUserName'])->value('is_new'); $user = Db::name('food_user')->where('open_id', $data['FromUserName'])->value('is_new');
if ($user !== 1) { if ($user !== 1) {
//不是新关注 //不是新关注
die('success'); die('success');
} else { } else {
//插入一条新用户数据 //插入一条新用户数据
$data = [ $data = [
'open_id' => $data['FromUserName'], 'open_id' => $data['FromUserName'],
'platform' => 10, 'platform' => 10,
'applet_id' => 10001, 'applet_id' => 10001,
'create_time' => time() 'create_time' => time()
]; ];
Db::name('food_user')->insert($data); Db::name('food_user')->insert($data);
//发送公众号消息 //发送公众号消息
$wx = new Driver; $wx = new Driver;
$access_token = $wx->getAccessToken(10001); $access_token = $wx->getAccessToken(10001);
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
$queryarr = [ $queryarr = [
'msgtype' => 'news', 'msgtype' => 'news',
'news' => [ 'news' => [
'articles' => [ 'articles' => [
'title' => '您有一张新的优惠券,请及时领取', 'title' => '您有一张新的优惠券,请及时领取',
'picurl' => '', 'picurl' => '',
'url' => 'https://www.baidu.com', 'url' => 'https://www.baidu.com',
'description' => '黄辛一新人优惠,首次关注赠送优惠券' 'description' => '黄辛一新人优惠,首次关注赠送优惠券'
] ]
] ]
]; ];
$queryarr['touser'] = $data['FromUserName']; $queryarr['touser'] = $data['FromUserName'];
Http::post($url, hema_json($queryarr)); 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