* @version : HuoMp 1.0 */ namespace huoMpMsg\controller; use huo\controller\common\Base; use huolib\constant\CacheConst; use think\Cache; use think\Log; class Common extends Base { /** * 获取 access_token * https://developers.weixin.qq.com/miniprogram/dev/api/token.html * * @param string $appid 第三方用户唯一凭证 * @param string $app_secret 第三方用户唯一凭证密钥,即appsecret * @param bool $read_cache * * @return string|bool */ public static function getAccessToken($appid, $app_secret, $read_cache = true) { $_cache_key = CacheConst::CACHE_ACCESS_TOKEN_PREFIX.$appid; $_data = Cache::get($_cache_key); if (!empty($_data)) { $_data = json_decode($_data, true); $_now_time = time(); if (isset($_data['expires_time']) && $_now_time < $_data['expires_time']) { if ($read_cache) { return $_data['access_token']; } } } $_url = 'https://api.weixin.qq.com/cgi-bin/token'; $_param['grant_type'] = 'client_credential'; $_param['appid'] = $appid; $_param['secret'] = $app_secret; $_rdata = self::curl($_url, http_build_query($_param)); $_rdata = json_decode($_rdata, true); if (!isset($_rdata['access_token'])) { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&step=1&appid=$appid&app_secret=$app_secret". json_encode($_rdata), Log::ERROR ); return false; } $_data['access_token'] = $_rdata['access_token']; $_data['expires_in'] = $_rdata['expires_in']; $_data['expires_time'] = time() + $_rdata['expires_in']; $_rs = Cache::set($_cache_key, json_encode($_data)); if (false == $_rs) { return false; } return $_data['access_token']; } /** * 获取 jsapi_ticket * https://developers.weixin.qq.com/miniprogram/dev/api/token.html * * @param $access_token $appid 第三方用户唯一凭证 * * @return string|bool */ public static function getJsapiTicket($access_token) { $_cache_key = CacheConst::CACHE_JSAPI_TICKET_PREFIX.$access_token; $_data = Cache::get($_cache_key); if (!empty($_data)) { $_data = json_decode($_data, true); $_now_time = time(); if (isset($_data['expires_time']) && $_now_time < $_data['expires_time']) { return $_data['ticket']; } } $_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; $_param['type'] = 'jsapi'; $_param['access_token'] = $access_token; $_rdata = self::curl($_url, http_build_query($_param)); $_rdata = json_decode($_rdata, true); if (!isset($_rdata['ticket'])) { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&step=1&access_token=$access_token". json_encode($_rdata), Log::ERROR ); return false; } $_data['ticket'] = $_rdata['ticket']; $_data['expires_in'] = $_rdata['expires_in']; $_data['expires_time'] = time() + $_rdata['expires_in']; $_rs = Cache::set($_cache_key, json_encode($_data)); if (false == $_rs) { return false; } return $_data['ticket']; } /** * CURL请求 * * @param string $url 请求的URL * @param string $params 请求的参数 * @param string $is_get 是否是get请求 * @param array $header 头 * * @return int|mixed */ public static function curl($url, $params, $is_get = "GET", $header = []) { $_header = $header; $ch = curl_init(); if ("GET" == $is_get) { if (!empty($params)) { $url = $url.'?'.$params; } curl_setopt($ch, CURLOPT_HEADER, 0); // 过滤HTTP头 } else { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上 /* https 请求 */ if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (!empty($_header)) { array_push($_header, 'Content-Length: '.strlen($params)); curl_setopt($ch, CURLOPT_HTTPHEADER, $_header); } $_rs = curl_exec($ch); if (false === $_rs) { $_rs = curl_errno($ch); } curl_close($ch); return $_rs; } /** * 判断是否json * * @param $string * * @return bool */ public static function isJson($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } }