overtrue/laravel-wechat如何动态修改配置参数
业务场景描述
- 使用的扩展包是overtrue/laravel-wechat
- 目前多租户架构,小程序的appid无法写在.env文件里面
尝试方案
我创建一个中间件文件wechat.php
然后在里面读取配置,通过config()的方式来修改
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class Wechat
{
public function handle(Request $request, Closure $next)
{
// 读取当前租户的微信小程序配置
config([
'easywechat.mini_app.default.app_id' => parameter('wechat_mini_app_appid'),
'easywechat.mini_app.default.secret' => parameter('wechat_mini_app_secret'),
'easywechat.mini_app.default.token' => parameter('wechat_mini_app_token'),
'easywechat.mini_app.default.aes_key' => parameter('wechat_mini_app_aes_key'),
]);
return $next($request);
}
}
业务报错:
业务代码
public function auth(Request $request)
{
$app = app('easywechat.mini_app');
$utils = $app->getUtils();;
$response = $utils->codeToSession($request->input('code'));
return $response;
}
出错提示:
production.ERROR: code2Session error: {"errcode":41002,"errmsg":"appid missing, rid: 63a00413-6446ae12-2e332dde"} {"exception":"[object] (EasyWeChat\\Kernel\\Exceptions\\HttpException(code: 0): code2Session error: {\"errcode\":41002,\"errmsg\":\"appid missing, rid: 63a00413-6446ae12-2e332dde\"} at D:\\wwwroot\\his\\vendor\\w7corp\\easywechat\\src\\MiniApp\\Utils.php:39)
请问这种情况下,如何才能动态配置config/easywechat.php
里面的参数
参考 easywechat.com/6.x/index.html 去用,每次new Application的时候再传 config 进去