diff --git a/app/common.php b/app/common.php index ffad61d..04a1053 100755 --- a/app/common.php +++ b/app/common.php @@ -1,4 +1,3 @@ -<<<<<<< HEAD $applet_id, 'status' => 1])) { $access_token = $applet['access_token']; - if (empty($access_token)) { + if ($type == 1) { + $appid = 'wx3668302906e3894f'; + $app_secret = '3ef948febd085fb89908f22b4a79c52f'; + } else { + $appid = 'wx89c12dd426a55a2e'; + $app_secret = '33e66bcf944f9810abbb5ddd7825403d'; + } - if (time() > $applet['expires_in']) { - $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3668302906e3894f&secret=3ef948febd085fb89908f22b4a79c52f'; - $result = json_decode(Http::get($url), true); - if (isset($result['access_token'])) { - $access_token = $result['access_token']; - $applet->access_token = $result['access_token']; - $applet->expires_in = time() + 3600;//2个小时候过期,这里设置1小时获取一次 - $applet->save();//保存最新的令牌access_token和过期时间 - } - } else { - return $access_token; + if (empty($access_token) || time() > $applet['expires_in']) { + $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$app_secret"; + $result = json_decode(Http::get($url), true); + + if (isset($result['access_token'])) { + $access_token = $result['access_token']; + $applet->access_token = $result['access_token']; + $applet->expires_in = time() + 3600;//2个小时候过期,这里设置1小时获取一次 + $applet->save();//保存最新的令牌access_token和过期时间 } } + return $access_token; } return $access_token; diff --git a/extend/hema/wechat/Index.php b/extend/hema/wechat/Index.php index 8e6eeed..29b3157 100644 --- a/extend/hema/wechat/Index.php +++ b/extend/hema/wechat/Index.php @@ -44,30 +44,38 @@ class Index // $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']; + if (!empty($data)) { //首次关注 if ($data['Event'] == 'subscribe') { - $user = Db::name('food_user')->where('open_id', $data['FromUserName'])->value('is_new'); - if ($user !== 1) { + $user = Db::name('food_user')->where('open_id', $openid)->value('is_new'); + + if (!empty($user) && $user !== 1) { //不是新关注 die('success'); } else { + if (empty($user)) { + //插入一条新用户数据 + $data = [ + 'open_id' => $openid, + 'platform' => 10, + 'applet_id' => 10001, + 'create_time' => time() + ]; + Db::name('food_user')->insert($data); + } - //插入一条新用户数据 - $data = [ - 'open_id' => $data['FromUserName'], - 'platform' => 10, - 'applet_id' => 10001, - 'create_time' => time() - ]; - Db::name('food_user')->insert($data); //发送公众号消息 $wx = new Driver; - $access_token = $wx->getAccessToken(10001); + $access_token = $wx->getAccessToken(10001, 2); $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token; $queryarr = [ + 'touser' => $openid, 'msgtype' => 'news', 'news' => [ 'articles' => [ @@ -78,9 +86,8 @@ class Index ] ] ]; - $queryarr['touser'] = $data['FromUserName']; - Http::post($url, hema_json($queryarr)); - + $result = Http::post($url, hema_json($queryarr)); + write_log($result, __DIR__); } die('success');