Skip to content

Commit

Permalink
修复抽象AccessToken类获取http实例问题
Browse files Browse the repository at this point in the history
  • Loading branch information
qbhy committed Jun 11, 2020
1 parent 1ade721 commit bee283e
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/AbstractAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ abstract class AbstractAccessToken
*/
protected $http;

/**
* @var Foundation
*/
protected $app;

/**
* Token string.
*
Expand All @@ -69,8 +74,17 @@ abstract class AbstractAccessToken
protected $token;

/**
* @param mixed $token
* @param int $expires
* AbstractAccessToken constructor.
* @param Foundation $app
*/
public function __construct(Foundation $app)
{
$this->app = $app;
}

/**
* @param mixed $token
* @param int $expires
* @return $this
*/
public function setToken($token, $expires = 86400)
Expand All @@ -87,7 +101,7 @@ public function setToken($token, $expires = 86400)
/**
* Get token from cache.
*
* @param bool $forceRefresh
* @param bool $forceRefresh
*
* @return string
*/
Expand Down Expand Up @@ -128,7 +142,7 @@ abstract public function getTokenFromServer();
abstract public function checkTokenResponse($result);

/**
* @param mixed $appId
* @param mixed $appId
*/
public function setAppId($appId)
{
Expand All @@ -144,7 +158,7 @@ public function getAppId()
}

/**
* @param string $secret
* @param string $secret
*/
public function setSecret($secret)
{
Expand All @@ -162,7 +176,7 @@ public function getSecret()
/**
* Set cache instance.
*
* @param \Doctrine\Common\Cache\Cache $cache
* @param \Doctrine\Common\Cache\Cache $cache
*
* @return AbstractAccessToken
*/
Expand Down Expand Up @@ -204,13 +218,13 @@ public function getCacheKey()
*/
public function getHttp()
{
return $this->http ?: $this->http = new Http();
return $this->http ?? $this->app->http;
}

/**
* Set the http instance.
*
* @param Http $http
* @param Http $http
*
* @return $this
*/
Expand Down

3 comments on commit bee283e

@bolechen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hanson/youzan-sdk 升级到 php 8.0 以后配合 foundation-sdk 最新版会报错

Attempt to read property "http" on null

把 207 行改回来,就正常了

请教一下 @qbhy 是 youzan-sdk 的问题吗?应该怎么解决?非常感谢

@Hanson
Copy link
Owner

@Hanson Hanson commented on bee283e Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bolechen 这个问题要看为什么 app 这里是为空的

@bolechen
Copy link
Contributor

@bolechen bolechen commented on bee283e Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bolechen 这个问题要看为什么 app 这里是为空的

app/Youzan.php

namespace App;

use Hanson;
use Illuminate\Support\Str;

// 有赞 api 二次封装
class Youzan extends Hanson\Youzan\Youzan
{
    public function __construct($config = null)
    {
        $config ??= config('youzan');
        parent::__construct($config);
    }

    public function __call(string $name, array $params): array
    {
        $method = 'youzan.'.str_replace('_', '.', Str::snake($name));
        // dump($method, $params);

        return $this->request($method, $params[0]);
    }

这样调用的

$youzan = resolve(Youzan::class);
$response = $youzan->userOpenidGetbyopenid([
    'open_id' => $open_id,
    'wechat_type' => 1,
]);

Please sign in to comment.