config = config('user_wxMiniProgram'); } /** * 获取openid && session_key * @param string $code * @return mixed */ public function login(string $code) { $config = $this->config; $response = $this->get('/sns/jscode2session', [ 'appid' => $config['appid'], 'secret' => $config['secret'], 'js_code' => $code, 'grant_type' => 'authorization_code', ]); return $this->json($response); } /** * 获取手机号 * @param string $code * @return false|mixed */ public function getUserPhone(string $code) { $access_token = $this->accessToken(); if (empty($access_token['access_token'])){ return $access_token; } $response = $this->postJson("/wxa/business/getuserphonenumber?access_token={$access_token['access_token']}", [ 'code' => $code ]); return $this->json($response); } /** * 获取 access_token * @return false|mixed */ private function accessToken() { $config = $this->config; $response = $this->postJson('/cgi-bin/stable_token', [ 'grant_type' => 'client_credential', 'appid' => $config['appid'], 'secret' => $config['secret'], 'force_refresh' => false,// 默认false:普通模式false;强制刷新模式true; ]); return $this->json($response); } public function json($response) { return json_decode($response, true); } private function postJson(string $uri,array $params = []){ return CurlUtil::post($this->domain.$uri,json_encode($params)); } private function get(string $uri,array $params = []){ return CurlUtil::get($this->domain.$uri,$params); } }