// +---------------------------------------------------------------------- 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; } }