123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\service;
- use app\api\model\Wxapp as WxappModel;
- use think\cache\driver\Redis;
- class MpWxService extends BaseService
- {
- public static function delweixinTokenCache(){
- $key = 'weixin:token';
- $rds = new Redis(config('cache.stores.redis'));
- $rds->delete($key);
- }
- //weixin token
- //"{"access_token":"27_vQnXeiUakHGc462ipuJZuNpK45gOdNKflz7GYWT1cVyy1CC6fhDemrvfFQWX3gRaieYU8tOPcZV6qkSHjtQpuK3OhYEq5935HZUcVSinQC8Osls61X9gArFP1sMuyUtblnjBUYX_DoeK4enDGDTaAJAXUK","expires_in":7200}"
- public static function weixinTokenCache(){
- $key = 'weixin:token';
- $rds = new Redis(config('cache.stores.redis'));
- $tokenJson = $rds->get($key);
- $result = [];
- if($tokenJson){
- $result = json_decode($tokenJson,true);
- }
- else{
- $result = self::weixinToken();
- $expires_in = $result['expires_in'];
- $expires = $expires_in-7000;//设置一个比微信过期如7200更早的过期时间
- $rds->set($key,json_encode($result),$expires);
- }
- return $result;
- }
- //微信token
- public static function weixinToken(){
- // 获取当前小程序信息
- $wxConfig = WxappModel::find(10001);
- $params["grant_type"] = "client_credential";
- $params["appid"] = $wxConfig['app_id'];
- $params["secret"] = $wxConfig['app_secret'];
- $url = "https://api.weixin.qq.com/cgi-bin/token?";
- $url = $url.http_build_query($params);
- $ret = self::curl_request($url);
- $retArr = json_decode($ret,true);
- return $retArr;
- // $access_token = $retArr["access_token"];
- // $expires_in = $retArr["expires_in"];
- // $result = array($access_token,$expires_in);
- // return $result;
- }
- public static function curl_request($url,$post='',$cookie='', $returnCookie=0){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- //设置https
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
- if($post) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
- }
- if($cookie) {
- curl_setopt($curl, CURLOPT_COOKIE, $cookie);
- }
- curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- if (curl_errno($curl)) {
- return curl_error($curl);
- }
- curl_close($curl);
- if($returnCookie){
- list($header, $body) = explode("\r\n\r\n", $data, 2);
- preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
- $info['cookie'] = substr($matches[1][0], 1);
- $info['content'] = $body;
- return $info;
- }else{
- return $data;
- }
- }
- /**
- * 调用接口
- *
- * @param string $api_name 接口名称
- * @param array $params 请求参数
- * @return array|mixed
- */
- public static function doAnalysisAction($api_name, $params)
- {
- $weixinToken = MpWxService::weixinTokenCache();
- $accessToken = $weixinToken['access_token'];
- $url = "https://api.weixin.qq.com/datacube/{$api_name}?access_token={$accessToken}";
- $ret = curl_post($url, json_encode($params));
- $arr = json_decode($ret, true);
- if (isset($arr['errcode']) && $arr['errcode'] == 40001) {
- MpWxService::delweixinTokenCache();
- }
- return $arr;
- // $data = $arr['list'] ?? [];
- // return $data;
- }
- }
|