多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
移步Saber文档 [https://github.com/swlib/saber](https://github.com/swlib/saber) # 案例 微信小程序登录,生成小程序码。 ~~~ <?php namespace app\Library; use app\Model\User; use ESD\Core\Plugins\Logger\GetLogger; use ESD\Go\Exception\ResponseException; use ESD\Plugins\Cache\Annotation\Cacheable; use function Swlib\Http\build_query; use Swlib\Http\ContentType; use function Swlib\Http\str; use Swlib\Saber; /** * 微信 */ class Wechat { use GetLogger; const WEIXIN_API = "https://api.weixin.qq.com"; private $app_info; /** * @var Saber */ private $http_client; /** * @param $app_id * @return $this * @throws \ESD\Plugins\Validate\ValidationException */ public function setAppid($app_id){ //此处获取appid 的方法自行实现。 $app_info = (new User())->app($app_id); $this->app_info = $app_info; $this->debug('wechat.setAppid', $this->app_info); $saber = Saber::create([ 'base_uri' => self::WEIXIN_API, 'use_pool' => true ]); $this->http_client = $saber; return $this; } /** * @param $code * @return mixed|null|\Psr\Http\Message\StreamInterface|\Swlib\Http\StreamInterface * @throws ResponseException */ public function getSession($code){ $param = [ 'appid' => $this->app_info['app_id'], 'secret' => $this->app_info['secret'], 'js_code' => $code, 'grant_type' => 'authorization_code', ]; $result = $this->http_client->get('/sns/jscode2session?' . build_query($param))->getBody(); $result = json_decode($result,true); if(isset($result['errcode'])){ throw new ResponseException(); } $this->debug('wechat.getSession' , $result); return $result; } /** * @param $scene * @param string $path * @param int $width * @param bool $is_hyaline * @return string * @throws ResponseException */ public function getQrcode($scene, $path='', $width=280, $is_hyaline=false){ $params = [ 'scene' => $scene, 'page' => $path, 'width' => $width, 'is_hyaline' => (bool) $is_hyaline, ]; $access_token = $this->getAccessToken($this->getAppid()); $response = $this->http_client->post('/wxa/getwxacodeunlimit?access_token='.$access_token, json_encode($params), [ 'headers' =>['content-type' => ContentType::JSON]] ); $contentType = $response->getHeader('content-type')[0]; $body = $response->getBody(); if($contentType != 'image/jpeg'){ $err = json_decode($body,true); $this->warn($err['errmsg']); throw new ResponseException($err['errmsg']); } return (string)$body; } public function getAppid(){ return $this->app_info['app_id']; } public function getSecret(){ return $this->app_info['secret']; } /** * @Cacheable(key="'wx_accesstoken'.$p[0]", time="7100") * @param $cacheKey * @return mixed * @throws ResponseException */ public function getAccessToken($cacheKey){ $query = [ 'grant_type' => 'client_credential', 'appid' => $this->getAppid(), 'secret' => $this->getSecret(), ]; $response = $this->http_client->get('/cgi-bin/token?' . build_query($query)); $token = json_decode($response->getBody(), true); if(isset($token['errcode'])){ throw new ResponseException(); } $this->debug('wechat.getAccessToken' , $token); return $token['access_token']; } } ~~~