This commit is contained in:
尖叫 2023-12-07 16:11:53 +08:00
parent 7097c3be82
commit 86cd185669

View File

@ -48,44 +48,80 @@ class Index
$data = json_decode($data, true);
$openid = $data['FromUserName'];
$wx = new Driver;
$access_token = $wx->getWxAccessToken(10001, $openid);
if (!empty($data)) {
//首次关注
if ($data['Event'] == 'subscribe') {
$user = Db::name('food_user')->where('open_id', $openid)->value('is_new');
$user = Db::name('user_wx')->where('open_id', $openid)->value('is_new');
if (!empty($user) && $user !== 1) {
//不是新关注
die('success');
} else {
if (empty($user)) {
$wx_url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $access_token . 'openid=' . $openid . '&lang=zh_CN';
$info = Http::get($wx_url);
$unionid = $info['unionid'];
//插入一条新用户数据
$data = [
'open_id' => $openid,
'platform' => 10,
'applet_id' => 10001,
'wx_open_id' => $openid,
'unionid' => $unionid,
'create_time' => time()
];
Db::name('food_user')->insert($data);
//首次关注券
$data1 = [
'openid' => $openid,
'title' => '首次关注公众号免费券',
'rule' => '',
'create_time' => time(),
];
$data2 = [
'openid' => $openid,
'title' => '消费满15元免费券',
'rule' => '',
'type' => 2,
'create_time' => time(),
];
Db::startTrans();
try {
Db::name('user_wx')->insert($data);
Db::name('food_coupon_user_wx')->insert($data1);
Db::name('food_coupon_user_wx')->insert($data2);
Db::commit();
die('success');
} catch (\Exception $e) {
Db::rollback();
write_log(json_encode($e->getMessage()), __DIR__);
}
}
//发送公众号消息
$wx = new Driver;
$access_token = $wx->getWxAccessToken(10001, $openid);
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
$queryarr = [
'touser' => $openid,
'msgtype' => 'news',
'news' => [
'articles' => [
'title' => '您有一张新的优惠券,请及时领取',
'picurl' => '',
'url' => 'https://www.baidu.com',
'description' => '黄辛一新人优惠,首次关注赠送优惠券'
[
'title' => '您有一张新的优惠券,请及时领取',
'description' => '黄辛一新人优惠,首次关注赠送优惠券',
'url' => 'https://www.baidu.com',
'picurl' => 'https://www.baidu.com',
]
]
]
];
$result = Http::post($url, hema_json($queryarr));
write_log($result, __DIR__);
}