1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Facades\UserFacade;
- use App\Repositories\Contracts\WxUserInterface;
- use GuzzleHttp\Client as httpClient;
- use Illuminate\Support\Facades\Redis;
- /**
- *
- * @author lilin
- *
- */
- class WxUserFacadeRepository extends BaseRepository implements WxUserInterface
- {
- public function getMPSession(string $code)
- {
- $url = str_replace(['APPID', 'SECRET', 'JSCODE'], [env('MP_APPID'), env('MP_APPSECRET'), $code], self::MP_JSCODE2SESSION_URL);
-
- try {
-
- $client = new httpClient();
- $res = $client->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();
- }
- }
-
- }
|