overtrue/laravel-wechat如何动态修改配置参数

业务场景描述

  1. 使用的扩展包是overtrue/laravel-wechat
  2. 目前多租户架构,小程序的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里面的参数

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

参考 easywechat.com/6.x/index.html 去用,每次new Application的时候再传 config 进去

1年前 评论
91it (楼主) 1年前
讨论数量: 14

参考 easywechat.com/6.x/index.html 去用,每次new Application的时候再传 config 进去

1年前 评论
91it (楼主) 1年前

没用过这个包 都是直接用 easywechat的 .. 看了下这个包的内容 主要初始化在 Overtrue\LaravelWeChat\ServiceProvider 这个文件里面,改下这个Provider就好了

1年前 评论
91it (楼主) 1年前
chowjiawei

中间件 用 config::set(‘pay.alipay.default.app_id’, ‘22’); 这样子的方式试下

并且 执行 php artisan optimize:clear 把缓存清一下

1年前 评论
91it (楼主) 1年前

看来大部分人已经被局限于框架里,被优雅所束缚,我们要尝试了解这个优雅背后的初衷。
本来laravel的服务提供者,就是希望提供一个开箱即用的容器给我们,现在这个无法满足我们的时候,而我们正好受限于框架的熟悉度感觉难以改造和利用它的时候,何不反本归源,自由的实现一个客户端类呢?该类紧密结合你的业务来选择灵活的构造你需要的服务提供容器,这不正是和框架做了一样的事情吗?

1年前 评论

既然是多租户,不建议使用配置文件的方式。缓存+数据库更加能满足这类灵活的需求。

1年前 评论

这种肯定是让后台有个可以配置的地方

1年前 评论

$config = [
“app_id”=>””,
“secret”=>””,
“token”=>””,
“aes_key”=>””,
];
$app = new Application($config);

easywechat.com/6.x/official-accoun...

1年前 评论

composer.json

"extra": {
        "laravel": {
            "dont-discover": [
                "overtrue/laravel-wechat"
            ]
        }
    }
<?php

namespace App\Providers;

use App\Models\Merchant;
use EasyWeChat\Pay\Application as Payment;
use Overtrue\LaravelWeChat\ServiceProvider;

class EasyWeChatServiceProvider extends ServiceProvider
{

    protected function bindPay(String $name, Merchant $merchant)
    {
        $this->app->bind("easywechat.pay.{$name}", function ($laravelApp) use ($merchant) {
            $app = new Payment([
                'app_id' => $merchant->app_id,
                'mch_id' => $merchant->mch_id,
                'v2_secret_key' => $merchant->api_v2_secret,
                'secret_key' => $merchant->api_v3_secret,
                'certificate' => $merchant->api_client_cert_pem,
                'private_key' => $merchant->api_client_key_pem,
                /* Todo 此处如果不知道序列号应该使用工具自动获取 */
                'certificate_serial_no' => $merchant->certificate_serial_no,
                'notify_url' => $merchant->callback_url,
            ]);

            if (\is_callable([$app, 'setRequestFromSymfonyRequest']))
                $app->setRequestFromSymfonyRequest($laravelApp['request']);

            return $app;
        });
    }

    public function boot()
    {
        if (!$this->app->runningInConsole()) {
            /** @var Merchant $merchant 获取数据库配置 */
            $merchants = Merchant::with('parent')->active()->where('type', 1)->get();

            foreach ($merchants as $merchant) {
                /* 未配置别名 */
                if (!$merchant->alias_name) continue;

                /* 未配置app_id 或者 mch_id*/
                if (!($merchant->app_id && $merchant->mch_id)) continue;

                /* 如果有服务商 */
                if($merchant->parent)
                    $this->bindPay("{$merchant->parent->alias_name}_{$merchant->alias_name}", $merchant->parent);

                /* 绑定商家自身 */
                $this->bindPay($merchant->alias_name, $merchant);
            }
        }
    }

}
1年前 评论
mouyong

有没有可能是你的 appid 不对嘞

1年前 评论

可以使用重新注册的方式

1年前 评论

easywechat ,外加数据表的形式

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!