From 86cd1856694e6527bfaf2864e65bc6ff1b3a61cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=96=E5=8F=AB?= <392494244@qq.com> Date: Thu, 7 Dec 2023 16:11:53 +0800 Subject: [PATCH] 1 --- extend/hema/wechat/Index.php | 62 ++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/extend/hema/wechat/Index.php b/extend/hema/wechat/Index.php index 337a2ab..fcd75e4 100644 --- a/extend/hema/wechat/Index.php +++ b/extend/hema/wechat/Index.php @@ -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__); }