2024-01-03 14:19:05 +08:00

140 lines
5.8 KiB
PHP

<?php
namespace hema\wechat;
use app\applet\model\Setting as SettingModel;
use app\applet\model\User as UserModel;
use app\common\model\Setting;
use app\common\model\Applet;
use app\common\model\Wechat;
use think\facade\Cache;
use hema\Http;
use think\facade\Db;
class Index
{
private $config;
private $error;
/**
* 构造函数
*/
public function __construct()
{
$config = [
'app_id' => 'wx89c12dd426a55a2e',
'appSecret' => '33e66bcf944f9810abbb5ddd7825403d',
'token' => 'cxhxy',
];
}
public function index()
{
if (isset($_GET['echostr'])) {
echo $_GET["echostr"];
exit;
}
write_log(1111, __DIR__);
//接收微信推送数据
$data = file_get_contents('php://input');
$data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
if (!empty($data)) {
$data = json_decode(json_encode($data), true);
// $data = json_decode($data, true);
$openid = $data['FromUserName'];
$wx = new Driver;
if (!empty($data)) {
//首次关注
if (strtolower($data['Event']) == 'subscribe') {
$access_token = $wx->getAccessToken(10001, 2);
$appid='wx89c12dd426a55a2e';
$wx_redirect_uri = urlencode('https://app.cxhxy.1nww.com/api/food.user/wxLogin');
$redirect_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri={$wx_redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//发送公众号消息
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
$queryarr = [
'touser' => $openid,
'msgtype' => 'news',
'news' => [
'articles' => [
[
'title' => '您有一张新的优惠券,请及时领取',
'description' => '黄辛一新人优惠,首次关注赠送优惠券',
'url' => $redirect_uri,
'picurl' => 'https://www.baidu.com',
]
]
]
];
$result = Http::post($url, hema_json($queryarr));
write_log(json_encode($result), __DIR__);
$find_coupon_user_wx = Db::name('food_coupon_user_wx')->where('openid','=',$openid)->where('type','=',1)->find();
if(empty($find_coupon_user_wx)){
// 赠送一条关注公众号的卡券
//首次关注券
$data1 = [
'openid' => $openid,
'title' => '首次关注公众号免费券',
'rule' => '',
'create_time' => time(),
'type' => 1,
'update_time' => time(),
];
try{
Db::name('food_coupon_user_wx')->insert($data1);
}catch (\Exception $e){
write_log($e->getMessage(), __DIR__);
}
}
$user = Db::name('user_wx')->where('wx_openid', $openid)->value('is_new');
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);
$info = json_decode($info, true);
$unionid = $info['unionid'];
//插入一条新用户数据
$data = [
'wx_openid' => $openid,
'unionid' => $unionid,
'create_time' => time()
];
try {
Db::name('user_wx')->insert($data);
} catch (\Exception $e) {
write_log($e->getMessage(), __DIR__);
}
}
die('success');
}
if($data['Event'] == 'CLICK'){
// 点击菜单事件
if(strtolower($data['EventKey']) == 'hx_num'){
$find_all = [];
$start_time = strtotime(date('Y-m-d'));
$end_time = strtotime(date('Y-m-d'))+60*60*24-1;
$count_hx = Db::name('food_coupon_user_wx')->where('update_time','>=',$start_time)
->where('update_time','<=',$end_time)
->where('status','=',2)
->where('type','=',1)
->count()??0;
// 查询核销人员
$xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
$res_xml = sprintf($xml,$data['FromUserName'],$data['ToUserName'],time(),"今日关注公众号已核销人数:{$count_hx}");
write_log($xml, __DIR__);
echo $res_xml;
exit();
}
}
}
}
}
}