账户设置-->API安全-->密钥设置 protected $payKey; /** * 设定配置项 * * @param 微信小程序配置文件读取 */ public function __construct() { $this->appId = config('wxconfig.appId'); $this->appSecret = config('wxconfig.appSecret'); $this->payMchId = config('wxconfig.payMchId'); $this->payKey = config('wxconfig.payKey'); } /** * 校验access_token是否过期 * @param string $token * @return bool */ public function valid_access_token($token) { return $token && isset($token['expires_in']) && ($token['expires_in'] > time() + 1200); } /** * 生成新的access_token * @return mixed */ public function new_access_token() { $url = self::API_DOMAIN . 'cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret; $res = HttpCurl::get($url, 'json'); // 异常处理: 获取access_token网络错误 if ($res === false) { @error_log('Http Get AccessToken Error.', 0); return false; } // 异常处理: access_token获取失败 if (!isset($res['access_token'])) { @error_log('Get AccessToken Error: ' . json_encode($res), 0); return false; } $res['expires_in'] += time(); $fp = fopen(dirname(__FILE__) . "/json/access_token.json", "w", 0777); fwrite($fp, json_encode($res)); fclose($fp); return $res; } /** * 获取access_token * @return string */ public function get_access_token() { $token = json_decode(file_get_contents(dirname(__FILE__) . "/json/access_token.json"), true); // 验证AccessToken是否有效 if (!$this->valid_access_token($token)) { // 生成新的AccessToken $token = $this->new_access_token(); if ($token === false) { return false; } } return $token['access_token']; } /** * MiniProgran - 登录 * 登录凭证校验 * @param code */ public function auth_code2_session($js_code) { $url = self::API_DOMAIN . 'sns/jscode2session?appid=' . $this->appId . '&secret=' . $this->appSecret . '&js_code=' . $js_code . '&grant_type=authorization_code'; $res = HttpCurl::get($url, 'json'); return $res; } /** * MiniProgran - 内容安全 * 检查一段文本是否含有违法违规内容 * @param string * @return bool */ public function security_msg_sec_check($content) { $access_token = $this->get_access_token(); $url = self::API_DOMAIN . 'wxa/msg_sec_check?access_token=' . $access_token; $postarray = ['content' => $content]; $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE); $res = HttpCurl::post($url, $postjson, 'json'); if ($res['errcode'] == 0) { return true; } else { return false; } } /** * MiniProgran - 小程序码 * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 * @param string * @return bool */ public function wxacode_get_unlimited($scene, $page = "", $width = 430, $filepath = '') { if (file_exists(root_path("/" . $filepath))) { return request()->domain() . "/" . $filepath; } $access_token = $this->get_access_token(); $url = self::API_DOMAIN . 'wxa/getwxacodeunlimit?access_token=' . $access_token; $postarray = [ 'scene' => $scene, 'page' => $page, 'width' => $width, ]; $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE); $res = HttpCurl::post($url, $postjson, 'text'); if (is_null(json_decode($res))) { $file = fopen($filepath, "w"); fwrite($file, $res); fclose($file); return request()->domain() . "/" . $filepath; } else { return false; } } }