135 lines
4.5 KiB
PHP
135 lines
4.5 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;
|
|
}
|
|
|
|
//接收微信推送数据
|
|
$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;
|
|
$access_token = $wx->getWxAccessToken(10001, $openid);
|
|
|
|
if (!empty($data)) {
|
|
|
|
//首次关注
|
|
if ($data['Event'] == 'subscribe') {
|
|
$user = Db::name('user_wx')->where('wx_openid', $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);
|
|
$info = json_decode($info, true);
|
|
|
|
|
|
$unionid = $info['unionid'];
|
|
|
|
|
|
//插入一条新用户数据
|
|
$data = [
|
|
'wx_openid' => $openid,
|
|
'unionid' => $unionid,
|
|
'create_time' => time()
|
|
];
|
|
|
|
//首次关注券
|
|
$data1 = [
|
|
'openid' => $openid,
|
|
'title' => '首次关注公众号免费券',
|
|
'rule' => '',
|
|
'create_time' => time(),
|
|
];
|
|
|
|
|
|
Db::startTrans();
|
|
try {
|
|
Db::name('user_wx')->insert($data);
|
|
Db::name('food_coupon_user_wx')->insert($data1);
|
|
Db::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
write_log(json_encode($e->getMessage()), __DIR__);
|
|
}
|
|
|
|
}
|
|
|
|
$appid='wx89c12dd426a55a2e';
|
|
$wx_redirect_uri = 'https://app.cxhxy.dev.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($result, __DIR__);
|
|
}
|
|
|
|
die('success');
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
} |