request('get', $url, []); $body = $res->getBody(); $data = json_decode($body); $this->setLog(self::TYPENAME.'jscode2session 返回数据', $this->startTime, [$data]); return $this->response($data); } catch (\Exception $e) { $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } public function getAccessToken(string $code) { $url = str_replace(['APPID', 'SECRET', 'CODE'], [env('MP_APPID'), env('MP_APPSECRET'), $code], self::URL_ACCESS_TOKEN); try { $client = new httpClient(); $res = $client->request('get', $url, []); $body = $res->getBody(); $data = json_decode($body); if (!isset($data->openid)){ $this->setLog(self::TYPENAME.'getAccessToken 失败 没有找到openid', $this->startTime, [$data]); $this->error()->fail(); }else{ //成功,创建reids $redisKey = config('console.redis_key.auth_access_token').$data->openid; Redis::SET($redisKey, $data->access_token); Redis::EXPIRE($redisKey, $data->expires_in); return $this->response($data); } } catch (\Exception $e) { $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } public function getUserInfo(string $accessToken, string $openid) { $url = str_replace(['ACCESS_TOKEN', 'OPENID',], [$accessToken, $openid], self::URL_USER_INFO); try { $client = new httpClient(); $res = $client->request('get', $url, []); $body = $res->getBody(); $data = json_decode($body); if (!isset($data->openid)){ $this->setLog(self::TYPENAME.'getUserInfo 失败 没有找到openid', $this->startTime, [$data]); $this->error()->fail(); }else{ return $this->response($data); } } catch (\Exception $e) { $this->setLog(self::TYPENAME.'getUserInfo 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } }