add miniprogram

This commit is contained in:
候学杰 2019-01-29 16:00:05 +08:00
parent aed1af638c
commit d49cfce08c
3 changed files with 43 additions and 14 deletions

View File

@ -3,37 +3,42 @@
namespace Wechat\ServiceProvider; namespace Wechat\ServiceProvider;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Wechat\Work\Miniprogram;
class WechatServiceProvider extends ServiceProvider class WechatServiceProvider extends ServiceProvider
{ {
public function setupConfig() public function boot()
{ {
$source = realpath(__DIR__ . '/../../config/wx.php'); }
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) { protected function setupConfig()
$this->publishes([$source => config_path('wx.php')], 'wx'); {
} elseif ($this->app instanceof LumenApplication) { $source = realpath(dirname(__DIR__) . '/../../config/wx.php');
$this->app->configure('wx');
}
$this->mergeConfigFrom($source, 'rpc'); // if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
// $this->publishes([$source => config_path('wx.php')], 'wx');
// } elseif ($this->app instanceof LumenApplication) {
$this->app->configure('wx');
// }
//
$this->mergeConfigFrom($source, 'wx');
} }
public function register() public function register()
{ {
$config = config['wx']; $this->setupConfig();
$config = config('wx');
if (isset($config['work'])) { if (isset($config['work'])) {
foreach ($config['work']['secrets'] as $key => $item) { foreach ($config['work']['secrets'] as $key => $item) {
$this->app->singleton('wx.work.' . $key, function () { $this->app->singleton('wx.work.' . $key, function () use ($key) {
return new ucwords($key); $class = ucwords($key);
return new $class ;
}); });
} }
} }
} }
} }

View File

@ -24,7 +24,13 @@ class Base
{ {
$uri = new Uri($uri); $uri = new Uri($uri);
$uri = Uri::withQueryValue($uri, 'access_token', $this->getAccessToken()); $uri = Uri::withQueryValue($uri, 'access_token', $this->getAccessToken());
$request = new Request($method, $uri, [], \GuzzleHttp\json_encode($params)); if (!empty($params)) {
$body = \GuzzleHttp\json_encode($params);
} else {
$body = null;
}
$request = new Request($method, $uri, [], $body);
return $request; return $request;
} }

View File

@ -0,0 +1,18 @@
<?php
namespace Wechat\Work;
class Miniprogram extends Base
{
protected $app = 'miniprogram';
public function code2Session($type = 'authorization_code')
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?js_code=$code&grant_type=$type");
$request = $this->client->transformForJsonRequest($url);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
}